masterhunter
masterhunter

Reputation: 559

Laravel model relationship issues

Currently i having two model, Roundtable and Table Category, i created a relationship between them with the code below.

Roundtables

roundtables

table_categories

table_categories

Table category model

public function roundtables(){
    return $this->hasMany('App\Roundtable');
}

Roundtable Model

public function table_category()
{
    return $this->belongsTo('App\Table_Category');
}

My controller

public function show(Request $request){
    $id=Auth::user()->id; //Logged user id is 1
    $table= Roundtable::where('user_id',$id)->get();
    return view('users.tables.show')->withTables($table);

}

This should connect them right?

Then when i try to show

 @foreach($tables as $table)
                    <tr>
                        <td><a href="{{ route('table_page',['id'=>$table->id]) }}">{{$table->name}}</a></td>
                        <td>
                            <a href="#" class="btn btn-info btn-xs">View</a>
                            <a href="#" class="btn btn-primary btn-xs">Edit</a>
                            <a href="#" class="btn btn-danger btn-xs">Delete</a>
                        </td>
                    </tr>
                        <tr>
                            <td>{{ $table->table_category->name }}</td>
                        </tr>
                    @endforeach

It will occur error "Trying to get property of non-object".

Can i know what is the problems?

Upvotes: 2

Views: 71

Answers (4)

xhulio
xhulio

Reputation: 1103

You can do this in 2 ways. First is by using relationships. At your Controller try something like this:

public function show(Request $request){
    $id=Auth::user()->id; //Logged user id is 1
    $table= Roundtable::with('table_category')->where('user_id',$id)->get();
    return view('users.tables.show', compact('table'));
}

users/tables/show.blade.php should remain the same in this scenario.

The second way:

On your Roundtable Model add the following method:

public function getCategoryAttribute(){
        $category = CategoryModel::where('id', $this->attributes['category_id'])->first();
        if(!$category ){
            return "";
        }
        return $category->name;
    }

and in your view you have to change from:

{{ $table->table_category->name }}

to

{{ $table->category }}

Upvotes: 1

Rama Durai
Rama Durai

Reputation: 750

You are using get() in the query. It is an eloquent collection.

You need to use foreach or first for the query.

like,

foreach($table as $item){
   $item->table_category->name
}

or

$table= Roundtable::where('user_id',$id)->first();
$table->table_category->name

I hope, it will help you.

Upvotes: 0

Alexey Mezenin
Alexey Mezenin

Reputation: 163748

get() will return collection. Use first() to get an object:

$table = Roundtable::where('user_id', $id)->first();

Upvotes: 1

AshMenhennett
AshMenhennett

Reputation: 1095

You want to call first:

$table->table_category()->first()->name

Upvotes: 0

Related Questions