Laravel 属性 [title] 在 Eloquent 构建器实例中不存在
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/54906252/
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
Laravel Property [title] does not exist on the Eloquent builder instance
提问by JohnSmith2521
I am getting an error while trying to display data from the database.This problem occurred by other people who have created posts on this website. but they have a foreach loop and I don't. so all the answers given for this problem do not work.
我在尝试显示数据库中的数据时遇到错误。这个问题是由在本网站上创建帖子的其他人发生的。但他们有一个 foreach 循环而我没有。所以针对这个问题给出的所有答案都不起作用。
article controller
物品控制器
public function showwereld($id)
{
$artikels = Artikel::where('id', $id);
return view('pages.show')->with('artikels', $artikels);
}
show.blade.php
show.blade.php
<div class="row">
<div class="artikeltitel marginauto">
<div class="col-md-6 offset-md-3">
<h2>{{$artikels->title}}</h2>
<p style="font-weight: bold;">{{$artikels->intro}}</p>
<p>{{$artikels->body}}</p>
</div>
</div>
</div>
回答by Ahmed Atoui
this on controller
这在控制器上
public function index()
{
$artikel = Artikel::where('category_id', '1')->first();
return view('pages.wereld',compact('artikel'));
}
in view:
鉴于:
<div class="row">
<div class="artikeltitel marginauto">
<div class="col-md-6 offset-md-3">
<h2>{{$artikel->title}}</h2>
<p style="font-weight: bold;">{{$artikel->intro}}</p>
<p>{{$artikel->body}}</p>
</div>
</div>
</div>
回答by Sahin S.
or basicly try this:
或者基本上试试这个:
$artikel = Artikel::find($id);
return view(pages.wereld, ['artikel' => $artikel]);
on view use this:
在视图中使用这个:
{{ $artikel->name }}
On your code: You are calling to get a record but trying to use plural.
在您的代码中:您打电话是为了获取记录,但试图使用复数。
回答by Aditya Tomar
Eager Loading Relationships(THIS WILL WORK JUST UNDERSTAND THIS)
热切加载关系(这将工作只是理解这一点)
DataTables support searching and sorting of eager loaded relationships when using Eloquent. this example will show you how to setup a eager loading search using Eloquent Engine.
使用 Eloquent 时,DataTables 支持搜索和排序预先加载的关系。本示例将向您展示如何使用 Eloquent 引擎设置预先加载搜索。
To enable search, we need to eager load the relationship we intend to use using Laravel's User::with('posts')api.
为了启用搜索,我们需要使用 Laravel 的User::with('posts')api预先加载我们打算使用的关系。
use DataTables;
Route::get('user-data', function() {
$model = App\User::with('posts');
return DataTables::eloquent($model)
->addColumn('posts', function (User $user) {
return $user->posts->map(function($post) {
return str_limit($post->title, 30, '...');
})->implode('<br>');
})
->toJson();
});
To trigger search on postsrelationship, we need to specify the relation.column_nameas the nameattribute in our javascriptappropriately.
要触发对帖子关系的搜索,我们需要在我们的javascript 中适当地指定relationship.column_name作为name属性。
<script>
$(document).ready(function() {
$('#users-table').DataTable({
processing: true,
serverSide: true,
ajax: '{{ url("collection/basic-object-data") }}',
columns: [
{data: 'id', name: 'id'},
{data: 'name', name: 'name'},
{data: 'email', name: 'email'},
{data: 'posts', name: 'posts.title'},
{data: 'created_at', name: 'created_at'},
{data: 'updated_at', name: 'updated_at'}
]
});
});
</script>
Looking at {data: 'posts', name: 'posts.title'},:
纵观十数据: '帖子',名称: 'posts.title'} :
data: postsrepresents the data key (data.posts)that we are going to display on our table. name: posts.titlerepresents the User model relationship (posts) and the column we are going to perform our search (title).
数据:posts表示我们将要显示在我们的表上的数据键(data.posts)。 名称:posts.title表示用户模型关系 (posts) 和我们要执行搜索的列 (title)。