Laravel Property [title] does not exist on the Eloquent builder instance

浪子不回头ぞ 提交于 2021-01-18 12:49:51

问题


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.

article controller

public function showwereld($id)
{
    $artikels = Artikel::where('id', $id);
    return view('pages.show')->with('artikels', $artikels);
}

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>


回答1:


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>



回答2:


I think you are missing the get() $artikels = Artikel::where('id', $id)->get();




回答3:


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.

To enable search, we need to eager load the relationship we intend to use using Laravel's 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 posts relationship, we need to specify the relation.column_name as the name attribute in our javascript appropriately.

<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'},:

data: posts represents the data key (data.posts) that we are going to display on our table. name: posts.title represents the User model relationship (posts) and the column we are going to perform our search (title).




回答4:


Or basically 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 the plural.



来源:https://stackoverflow.com/questions/54906252/laravel-property-title-does-not-exist-on-the-eloquent-builder-instance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!