【发布时间】:2016-04-15 22:08:19
【问题描述】:
我是 Traits 的新手,但我的函数中有很多重复的代码,我想使用 Traits 使代码不那么混乱。我在我的Http 目录中创建了一个Traits 目录,其中包含一个名为BrandsTrait.php 的特征。它所做的只是呼吁所有品牌。但是当我尝试在我的产品控制器中调用 BrandsTrait 时,如下所示:
use App\Http\Traits\BrandsTrait;
class ProductsController extends Controller {
use BrandsTrait;
public function addProduct() {
//$brands = Brand::all();
$brands = $this->BrandsTrait();
return view('admin.product.add', compact('brands'));
}
}
它给了我一个错误,说 方法 [BrandsTrait] 不存在。我是要初始化某些东西,还是以不同的方式调用它?
这是我的BrandsTrait.php
<?php
namespace App\Http\Traits;
use App\Brand;
trait BrandsTrait {
public function brandsAll() {
// Get all the brands from the Brands Table.
Brand::all();
}
}
【问题讨论】:
标签: php laravel laravel-5.2 traits