Laravel 5 在刀片视图中调用模型函数 [英] Laravel 5 call a model function in a blade view
问题描述
我想从 3 个表构建一个刀片视图:
I want to build a blade view from 3 tables:
- "inputs_details" - 字段:article_type(值:'p' 表示产品,'s' 表示服务)、article_id、....
- "products" - 字段:id、name
- "services" - 字段:id、name
但是,在浏览器中,我遇到错误:找不到产品"类.
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"表连接...或.... wherearticle_type 为s",然后加入services"表.
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.
推荐答案
与您回答中的问题相关:
Related to the question in your answer:
您有多种选择可以更好地实现这一目标:
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
}
然后:
{{AppModel::someStaticFunction($yourVar1,$yourVar2)}}
希望有帮助.
这篇关于Laravel 5 在刀片视图中调用模型函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!