如何在 Laravel 中的 Views/Blade 文件中查询表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38718460/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to query tables in Views/Blade file in Laravel
提问by Nitish Kumar
I'm having a set of array
which stores the id
of the blog post
, I want to display the blog
post with the id
present in the array
, I can't pass it through the controller
as I'm passing lot of data to the views and blog post view is called dynamically, In my controller I'm getting all the post and passing through the view:
我有一组array
其存储id
的blog post
,我想显示blog
与该帖子id
出现在array
,我无法通过它传递controller
为我传递大量数据的意见和博客文章的观点被称为动态地,在我的控制器中,我正在获取所有帖子并通过视图:
$blogpost = Post::all();
'blogpost' => $blogpost //data passed in return view
Now in view I'm having a foreach loop
and suppose I want to get the image of that particular post I'm trying to do:
现在看来,我有一个foreach loop
并且假设我想获得我正在尝试做的那个特定帖子的图像:
@foreach($contents->blogid as $blogid)
<?php
$blog = $blogpost->findOrFail($blogid);
?>
<div class="col-sm-4">
<article class="post post-masonry">
<div class="post-image">
<div class="image">
<img src="{{ $blog->featured_image }}" alt="">
<div class="image-extras">
<a href="#" class="post-gallery"></a>
</div>
</div>
</div>
</article>
</div>
@endforeach
I'm getting an error
我收到一个错误
Method findOrFail does not exist
方法 findOrFail 不存在
Moreover if I'm changing findOrFail
to:
此外,如果我要更改findOrFail
为:
$blog = $blogpost->find($blogid);
I'm getting an error:
我收到一个错误:
Trying to get property of non-object
试图获取非对象的属性
Help me out guys.
帮帮我吧伙计们。
Thanks.
谢谢。
回答by Rolf Pedro Ernst
You shouldn't be mixing native PHP and blade templates.
您不应该混合使用原生 PHP 和刀片模板。
Instead, let blade do the work for you! :)
相反,让刀片为您完成工作!:)
@foreach($contents->blogid as $blogid)
..
<img src="{!! \App\Post::findOrFail($blogid)->featured_image; !!}" alt="">
..
@endforeach
You don't even need to pass the 'blogpost' variable, as long as you have the correct route to your Post class. I used App\Post, but for you it may be different.
你甚至不需要传递 'blogpost' 变量,只要你有正确的路由到你的 Post 类。我使用了 App\Post,但对你来说可能会有所不同。
回答by Alfonz
Post::all()
will return an array of Post objects. So just pass that array to your blade template and loop through each Post
of that array. You should also change the variable name to blogposts.
Post::all()
将返回一组 Post 对象。所以只需将该数组传递给您的刀片模板并循环遍历Post
该数组中的每一个。您还应该将变量名称更改为 blogpost s。
PHP
PHP
$blogposts = Post::all();
return view('blog.index', ['blogposts' => $blogposts]);
Blade
刀刃
@foreach ($blogposts as $blogpost)
<p>{{ $blogpost->id }}</p>
<p>{{ $blogpost->featured_image }}</p>
@endforeach
回答by Jónathan Cuellar
findOrFail
or only find
aren't functions native PHP. You can try statically call, but I think that your problem is easily resolve with Laravel Relationship, your code will be more than clean.
findOrFail
或者只是find
不是原生 PHP 函数。您可以尝试静态调用,但我认为使用 Laravel 关系可以轻松解决您的问题,您的代码将不仅仅是干净的。
https://laravel.com/docs/5.1/eloquent-relationships
https://laravel.com/docs/5.1/eloquent-relationships
sorry, first steps with English.
对不起,第一步是英语。