Ho bisogno di un esempio di where creare esattamente il file, scrivere su di esso e come utilizzare le funzioni dichiarate nel tratto. Io uso Laravel Framework 5.4.18
-Non ho alterato alcuna cartella nel framework, tutto è where corrisponde-
Da già grazie mille.
Ho creato la directory Create a Traits nella mia directory Http
con un Trait chiamato BrandsTrait.php
e usalo come:
use App\Http\Traits\BrandsTrait; class YourController extends Controller { use BrandsTrait; public function addProduct() { //$brands = Brand::all(); // $brands = $this->BrandsTrait(); // this is wrong $brands = $this->brandsAll(); } }
Ecco il mio BrandsTrait.php
<?php namespace App\Http\Traits; use App\Brand; trait BrandsTrait { public function brandsAll() { // Get all the brands from the Brands Table. $brands = Brand::all(); return $brands; } }
Nota: proprio come una normale function scritta in un determinato namespace
, puoi usare anche i traits