SQLSTATE [42S22]:未找到列:1054未知列一对多(反)关系laravel

SQLSTATE [42S22]:未找到列:1054未知列一对多(反)关系laravel

问题描述:

I have two models Tour.php and 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');
}

My db tables are as follows:

tours table

id|title|category_id|content|

tcatgegories table

id|name

And I have a view to show the all tours belonging to a category with the following code:

    @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

With this above code I'm getting error of:

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-masteresources\
views\admin\categories\show.blade.php

I have used same code for my previous projects also but didn't had any errors. Or am I missing something ?

我有两个模型 Tour.php code>和 TourCategory.php code >: p>

Tour.php code> p>

  protected $ table =`tours`; 
 
public function  category()
 {
返回$ this-&gt; belongsTo('App \ TourCategory'); 
} 
  code>  pre> 
 
 

TourCategory.php p>

  protected $ table ='tcategories'; 
 
公共函数游览()
 {
返回$ this-&gt; hasMany('App \ Tour  '); 
} 
  code>  pre> 
 
 

我的数据库表格如下: p>

tours code>表 p>

id | title | category_id | content | code> p>

tcatgegories code> table p>

id | name code> p>

我可以使用以下代码显示属于某个类别的所有游览: p >

  @foreach($ category-&gt; tour as $ tour)
&lt; tr&gt; 
&lt; th&gt; {{$ tour-&gt; id}}&lt; / th&gt;  ; 
&lt; td&gt; {{$ tour-&gt; title}}&lt; / td&gt; 
&lt; td&gt; 
&lt; spa  n class =“label label-default”&gt; {{$ category-&gt; name}}&lt; / span&gt; 
&lt; / td&gt; 
&lt; / tr&gt; 
 @endforeach 
  code>   pre> 
 
 

使用上面的代码我收到错误: p>

  SQLSTATE [42S22]:找不到列:1054未知列'巡视 .tour_category_id'在
'where子句'(SQL:select * from`tours`其中`tours``tour_category_id` = 1和
`tours``tour_category_id`不为空)(查看:F:\ multiauth_tutorial  -master 
esources \
views \ admin \ categories \ show.blade.php 
  code>  pre> 
 
 

我以前的项目也使用了相同的代码,但没有任何代码 错误。 或者我错过了什么? p> div>

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

So, you need to 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

Or use tour_category_id instead of category_id in the tours table.

this column tour_category_id lis not found in your tours table, change your sql query to

select * from `tours` where `tours`.`category_id` = 1 and `tours`.`category_id` is not null

in Tour.php

 /**
 * @return \Illuminate\Database\Eloquent\Relations\HasMany
 */
public function tours()
{
    return $this->hasMany(Tour::class, 'category_id');
} 

in TourCategory.php

/**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function category()
    {
        return $this->belongsTo(TourCategory::class, 'category_id');
    }