试图获得 属性 'category' 的非对象
Trying to get property 'category' of non-object
我试图获取要在 table 中显示的类别名称,但出现错误:
“正在尝试获取非对象的 属性 'category'(视图:C:\xampp\htdocs\retro\resources\views\admin\games\index.blade.php)!而不是
这里是 table 代码:
@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
Categories
模特:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Categories extends Model
{
public function games()
{
return $this->hasMany('App\Games');
}
}
Games
型号:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Games extends Model
{
public function category()
{
return $this->hasOne('App\Categories');
}
}
这是我正在使用的迁移
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();
});
}
我很确定这是一个关系错误,但我看不出那是什么。
你应该改变这个:
<td>{{$game->category_id->category}}</td>
进入这个:
<td>{{$game->category->id}}</td>
// or if you have name property in the category table
<td>{{$game->category->name}}</td>
因为在您的 Game
模型中有一个 category
函数将 return 它的关系对象,基于存储在 table.
在您的 Game
模型中,您可能可以改用此关系:
public function category()
{
return $this->belongsTo('App\Categories', 'category_id');
}
$game->category_id
不会 return 一段关系,正如你所说的 public function category()
。您需要使用
<td>{{ $game->category->name }}</td>
(不确定您要显示 category
的哪一列,在 name
上猜测)
此外,请遵循 Laravel 惯例。模型名称是单数,所以应该是
class Game extends Model { ... }
class Category extends Model { ... }
此外,如果关系不太正常,您可能需要提供外键:
return $this->hasOne('App\Categories', 'category_id');
我看到另一个问题。您不能将 hasMany
与 hasOne
配对;那里的某个地方需要有一个 belongsTo
。一个Game
属于一个Category
,而一个Category
可以有多个Game
:
Games.php
class Games extends Model
{
public function category()
{
return $this->belongsTo('App\Categories', 'category_id');
}
}
我试图获取要在 table 中显示的类别名称,但出现错误: “正在尝试获取非对象的 属性 'category'(视图:C:\xampp\htdocs\retro\resources\views\admin\games\index.blade.php)!而不是
这里是 table 代码:
@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
Categories
模特:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Categories extends Model
{
public function games()
{
return $this->hasMany('App\Games');
}
}
Games
型号:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Games extends Model
{
public function category()
{
return $this->hasOne('App\Categories');
}
}
这是我正在使用的迁移
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();
});
}
我很确定这是一个关系错误,但我看不出那是什么。
你应该改变这个:
<td>{{$game->category_id->category}}</td>
进入这个:
<td>{{$game->category->id}}</td>
// or if you have name property in the category table
<td>{{$game->category->name}}</td>
因为在您的 Game
模型中有一个 category
函数将 return 它的关系对象,基于存储在 table.
在您的 Game
模型中,您可能可以改用此关系:
public function category()
{
return $this->belongsTo('App\Categories', 'category_id');
}
$game->category_id
不会 return 一段关系,正如你所说的 public function category()
。您需要使用
<td>{{ $game->category->name }}</td>
(不确定您要显示 category
的哪一列,在 name
上猜测)
此外,请遵循 Laravel 惯例。模型名称是单数,所以应该是
class Game extends Model { ... }
class Category extends Model { ... }
此外,如果关系不太正常,您可能需要提供外键:
return $this->hasOne('App\Categories', 'category_id');
我看到另一个问题。您不能将 hasMany
与 hasOne
配对;那里的某个地方需要有一个 belongsTo
。一个Game
属于一个Category
,而一个Category
可以有多个Game
:
Games.php
class Games extends Model
{
public function category()
{
return $this->belongsTo('App\Categories', 'category_id');
}
}