Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Laravel:在视图中显示数据而无需雄辩_Laravel_View_Model_Load_Eloquent - Fatal编程技术网

Laravel:在视图中显示数据而无需雄辩

Laravel:在视图中显示数据而无需雄辩,laravel,view,model,load,eloquent,Laravel,View,Model,Load,Eloquent,假设我需要在视图中显示文章列表。因此,我运行一个复杂的查询来获取所有需要的数据,并得到一个结果表,例如,如下所示: $model = new Article(); $model.load(row); array_push($dataForView, $model); |id |物品名称|物品主体|物品标签| id |。。。(更多专栏) 还定义了一个模型:Article.php 问题:准备在Laravel中查看的数据的正确方法是什么?我还想补充一点,我正在寻找一种不用雄辩就能做到这一点的方法。当

假设我需要在视图中显示文章列表。因此,我运行一个复杂的查询来获取所有需要的数据,并得到一个结果表,例如,如下所示:

$model = new Article();
$model.load(row);
array_push($dataForView, $model);
|id |物品名称|物品主体|物品标签| id |。。。(更多专栏)

还定义了一个模型:Article.php

问题:准备在Laravel中查看的数据的正确方法是什么?我还想补充一点,我正在寻找一种不用雄辩就能做到这一点的方法。当然我可以只做$articles=
Article::all()
在上面的例子中,我想做的是创建一个模型,并将每一行传递给方法load(),如下所示:

$model = new Article();
$model.load(row);
array_push($dataForView, $model);
并最终将数据发送到视图

return view('articles')->with('articles', $dataForView);

这是我在拉威尔应该做的事吗?

我不确定你们是否想一起避免雄辩。但您可以将复杂查询抽象为模型上的方法:

// article.php

public function my_complex_query()
{

    // code here. Eloquent, or Query Builder or 
    // Raw SQL

    return $result;

}
然后从控制器中调用:

return view('the-view')
    ->withArticles(Article::my_complex_query());

// or you can inject your model into your controller and call $this->article->my_complex_query()
然后循环查看视图中的数据。这就是eloquent的美妙之处,因为它将数据库结果映射到一个对象

@foreach ($articles as $article)
{{ $article->name }}
// ...
@endforeach

我不确定你是否想一起避免雄辩。但您可以将复杂查询抽象为模型上的方法:

// article.php

public function my_complex_query()
{

    // code here. Eloquent, or Query Builder or 
    // Raw SQL

    return $result;

}
然后从控制器中调用:

return view('the-view')
    ->withArticles(Article::my_complex_query());

// or you can inject your model into your controller and call $this->article->my_complex_query()
然后循环查看视图中的数据。这就是eloquent的美妙之处,因为它将数据库结果映射到一个对象

@foreach ($articles as $article)
{{ $article->name }}
// ...
@endforeach