2
votes

I'm trying to get the name of the category to show in a table but I get the error: "Trying to get property 'category' of non-object (View: C:\xampp\htdocs\retro\resources\views\admin\games\index.blade.php)! instead

Here is the table for the code:

@foreach($games as $game)
    <tr>
        <td>{{ $game->title }}</td>
        <td>{{ $game->image }}</td>
        <td>£{{ $game->price }}</td>
        <td>{{ $game->category_id->category }}</td>
        <td>{{ $game->sold }}</td>
        <td>{{ $game->promote }}</td>
        <td>
            <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#edit">Edit</button>
        </td>
    </tr>
@endforeach

The Categories Model:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Categories extends Model
{
    public function games()
    {
        return $this->hasMany('App\Games');
    }
}

The Games model:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Games extends Model
{
    public function category()
    {
        return $this->hasOne('App\Categories');
    }
}

And here's the migration I'm using

public function up()
{
    Schema::create('games', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('title');
        $table->string('image');
        $table->integer('price');
        $table->integer('category_id')->index();
        $table->integer('sold');
        $table->integer('promote');
        $table->timestamps();
    });
}

I'm quite sure it's a relationship error but I can't see what that is.

2
What have you tried to debug that error? Have you checked what $game->category_id contains, maybe an integer (just as you defined it)?Nico Haase

2 Answers

4
votes

$game->category_id won't return a relationship, as you've called it public function category(). You need to use

<td>{{ $game->category->name }}</td>

(Not sure what column of category you're trying to display, guessed on name)

Also, follow Laravel conventions. Models names are singular, so it should be

class Game extends Model { ... }

class Category extends Model { ... }

Furthermore, you may need to provide the foreign key if the relationship doesn't quite work:

return $this->hasOne('App\Categories', 'category_id');

I see another issue. You can't have hasMany paired with hasOne; there needs to be a belongsTo somewhere in there. A Game belongs to a Category, while a Category can have many Games:

Games.php

class Games extends Model
{
    public function category()
    {
      return $this->belongsTo('App\Categories', 'category_id');
    }
}
3
votes

You should change this:

<td>{{$game->category_id->category}}</td>

into this:

<td>{{$game->category->id}}</td>

// or if you have name property in the category table

<td>{{$game->category->name}}</td>

Because in your Game model there is a category function that will return the relationship object for it, based on the category_id that is stored in the table.

In your Game model, you can probably use this relationship instead:

public function category()
{
  return $this->belongsTo('App\Categories', 'category_id');
}