我需要一个示例来说明在何处准确创建文件、写入文件以及如何使用特征中声明的函数。我使用 Laravel 框架 5.4.18
-我没有更改框架中的任何文件夹,所有内容都在其对应的位置-
已经非常感谢了。
我在我的目录中创建了一个 Traits 目录Http
具有名为 Trait 的目录BrandsTrait.php
并像这样使用它:
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();
}
}
这是我的 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;
}
}
Note:就像用某种方式编写的普通函数一样namespace
, 您可以使用traits
as well
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)