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

5

16

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>

enter image description here

Upbeat answered 27/2, 2019 at 13:8 Comment(4)
The error is showing that $artikels is a Builder instance, which doesn't seem to correlate with your code sample. Your code sample would have $artikels as a Collection instance, which also wouldn't have a title property, but indicates that you're either posting the wrong code or looking at the wrong code.Actual
thanks i am going to look into that right nowUpbeat
Artikel::where('id', $id)->get(); Otherwise you get a query builder. Edit: that's still a colelction so you should actually do : Artikel::where('id', $id)->first();Archaism
Do you have the title field in Artikel? what does {{dd($artikels)}} give you if you add this in the blade?Archaism
C
21

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>
Crinoline answered 27/2, 2019 at 13:18 Comment(10)
thanks for sharing this atoui, but when i add this piece of code it still gives me the same errorUpbeat
did you want to show single or collection of articles ?Crinoline
only one single row out of my database but the only results on this problem are with foreachloopsUpbeat
good , replace get method with first method in this line : Artikel::where('category_id', '1')->get() become Artikel::where('category_id', '1')->first();Crinoline
it gives me this error: Trying to get property 'title' of non-objectUpbeat
i see , let's try to replace : return view('pages.wereld')->with('artikels', $artikels) become : return view('pages.wereld',compact('artikels'))Crinoline
still the same error Trying to get property 'title' of non-objectUpbeat
i think the article with id=1 not found , put this code :dd($artikels) befoe retrun view to see if there is recordCrinoline
check my post, i edited a screenshot in where i've got the object from DDUpbeat
i edited my answer and provide the full code ,can you try it (copy and past)Crinoline
T
13

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

Troat answered 26/8, 2020 at 5:47 Comment(1)
In my case it was ->first()Tribasic
M
2

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).

Militarism answered 3/3, 2020 at 6:19 Comment(0)
R
0

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.

Rehabilitate answered 7/11, 2019 at 12:30 Comment(0)
M
0

Are you sure it's not added in the hidden properties inside your model? if not then

$artikels = Artikel::where('id', $id)->get();

OR

$artikels = Artikel::where('id', $id)->first();

should work

Millen answered 6/2, 2023 at 7:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.