Laravel 5在刀片视图中调用模型函数
我想从3个表构建刀片视图:
I want to build a blade view from 3 tables:
- "inputs_details"-字段:article_type(值:"p"表示产品,服务是"s"),article_id,....
- 产品"-字段:ID,名称
- 服务"-字段:ID,名称
但是,在浏览器中,出现错误:未找到类'产品'".
But, in browser, I have the error: "Class 'Product' not found".
是否有解决方案可以将此功能传递给视图(根据article_type和article_id查找产品或服务的名称)?
Is there a solution to pass to the view this function (to find the name of the product or the service based on article_type and article_id)?
我也在尝试使用联接查询,但是我无法在单个联接查询中放置太多条件..其中article_type为"p",然后与"products"表联接...或.... article_type为"s",然后与服务"表联接.
I was trying also with join query, but I couldn't put so many conditions in a single join query .. where article_type is "p", then join with "products" table ... or .... where article_type is "s", then join with "services" table.
与答案中的问题有关:
您可以通过多种选择来达到更好的效果:
You have multiple options to achieve this that are way better:
假设您有一个传递给视图的模型:
Let's assume you have a model which you pass to the view:
$model = Model::find(1);
View::make('view')->withModel($model);
现在在您的模型中,您可以拥有一个功能:
Now in your Model you could have a function:
public function someFunction() {
// do something
}
在您看来,您可以直接调用该函数:
In your view you could call that function directly:
{{$model->someFunction()}}
如果您要对模型(数据集)做些事情,这很好.
This is nice if you want to do something with the model (the dataset).
如果没有,您仍然可以在模型中创建静态函数:
If not you can still make a static function in the model:
public static function someStaticFunction($var1, $var2) {
// do something
}
然后:
{{App\Model::someStaticFunction($yourVar1,$yourVar2)}}
希望有帮助.