SQLSTATE[42S22]: 未找到列:1054 未知列一对多(反向)关系 laravel
SQLSTATE[42S22]: Column not found: 1054 Unknown column one to many(inverse) relation laravel
我有两个模型 Tour.php
和 TourCategory.php
:
Tour.php
protected $table = `tours`;
public function category()
{
return $this->belongsTo('App\TourCategory');
}
TourCategory.php
protected $table = 'tcategories';
public function tours()
{
return $this->hasMany('App\Tour');
}
我的数据库table如下:
tours
table
id|title|category_id|content|
tcatgegories
table
id|name
我想用以下代码显示属于某个类别的所有游览:
@foreach ($category->tours as $tour)
<tr>
<th>{{ $tour->id}}</th>
<td>{{ $tour->title}}</td>
<td>
<span class="label label-default">{{$category->name}}</span>
</td>
</tr>
@endforeach
使用上面的代码,我得到以下错误:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'tours.tour_category_id' in
'where clause' (SQL: select * from `tours` where `tours`.`tour_category_id` = 1 and
`tours`.`tour_category_id` is not null) (View: F:\multiauth_tutorial-master\resources\
views\admin\categories\show.blade.php
我以前的项目也使用过相同的代码,但没有出现任何错误。还是我遗漏了什么?
在您的 tours
table 中找不到此列 tour_category_id
,请将您的 sql 查询更改为
select * from `tours` where `tours`.`category_id` = 1 and `tours`.`category_id` is not null
Eloquent determines the default foreign key name by examining the name of the relationship method and suffixing the method name with _id
. However, if the foreign key on the model is not *_id
, you may pass a custom key name as the second argument to the belongsTo
method
因此,您需要 specify a foreign key:
public function category()
{
return $this->belongsTo('App\TourCategory', 'category_id');
}
You may also override the foreign and local keys by passing additional arguments to the hasMany
method.
public function tours()
{
return $this->hasMany('App\Tour', 'category_id');
}
https://laravel.com/docs/5.4/eloquent-relationships#one-to-many
或者在游览 table.
中使用 tour_category_id
而不是 category_id
在 Tour.php
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function tours()
{
return $this->hasMany(Tour::class, 'category_id');
}
在 TourCategory.php
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function category()
{
return $this->belongsTo(TourCategory::class, 'category_id');
}
我有两个模型 Tour.php
和 TourCategory.php
:
Tour.php
protected $table = `tours`;
public function category()
{
return $this->belongsTo('App\TourCategory');
}
TourCategory.php
protected $table = 'tcategories';
public function tours()
{
return $this->hasMany('App\Tour');
}
我的数据库table如下:
tours
table
id|title|category_id|content|
tcatgegories
table
id|name
我想用以下代码显示属于某个类别的所有游览:
@foreach ($category->tours as $tour)
<tr>
<th>{{ $tour->id}}</th>
<td>{{ $tour->title}}</td>
<td>
<span class="label label-default">{{$category->name}}</span>
</td>
</tr>
@endforeach
使用上面的代码,我得到以下错误:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'tours.tour_category_id' in
'where clause' (SQL: select * from `tours` where `tours`.`tour_category_id` = 1 and
`tours`.`tour_category_id` is not null) (View: F:\multiauth_tutorial-master\resources\
views\admin\categories\show.blade.php
我以前的项目也使用过相同的代码,但没有出现任何错误。还是我遗漏了什么?
在您的 tours
table 中找不到此列 tour_category_id
,请将您的 sql 查询更改为
select * from `tours` where `tours`.`category_id` = 1 and `tours`.`category_id` is not null
Eloquent determines the default foreign key name by examining the name of the relationship method and suffixing the method name with
_id
. However, if the foreign key on the model is not*_id
, you may pass a custom key name as the second argument to thebelongsTo
method
因此,您需要 specify a foreign key:
public function category()
{
return $this->belongsTo('App\TourCategory', 'category_id');
}
You may also override the foreign and local keys by passing additional arguments to the
hasMany
method.
public function tours()
{
return $this->hasMany('App\Tour', 'category_id');
}
https://laravel.com/docs/5.4/eloquent-relationships#one-to-many
或者在游览 table.
中使用tour_category_id
而不是 category_id
在 Tour.php
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function tours()
{
return $this->hasMany(Tour::class, 'category_id');
}
在 TourCategory.php
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function category()
{
return $this->belongsTo(TourCategory::class, 'category_id');
}