Laravel 版本升级影响你的控制器?
Laravel version upgrade affects your controller?
我的 ReportController
use App\Site;
use App\Report;
public function showSpecificSite($site_id){
$reports = Report::whereHas('site', function($query) use($site_id) {
$query->where('site_id', $site_id);
})->get(['email_date', 'url', 'recipient', 'report_id', 'site_id']);
$siteName = Site::find($site_id)->site_name;
return view('newsite', compact('site_id', 'siteName', 'reports'));
}
Route::get('sites/{site_id}',['as'=>'SpecificSite','uses'=>'ReportController@showSpecificSite']);
场地模型
public function report()
{
return $this->hasMany('App\Report');
}
报表模型
public function site()
{
return $this->belongsTo('App\Site');
}
我的blade视图
<a href="{{route('SpecificSite',['site_id'=>$record->site_id])}}">view</a>
这是我将 laravel 版本 5.2.36 升级到 5.4.36 的问题
这在 5.2 版本中运行良好,这里是 运行 的文章。
SQLSTATE[42S22]:未找到列:1054 'where clause' 中的未知列 'reports.site_site_id'(SQL:select email_date
, url
, recipient
, report_id
, site_id
from reports
where exists (select * from sites
where reports
.site_site_id
= sites
.site_id
和 site_id
= 1))
我的route:list和以前一样。我也已经有了 routes/web.app 文件夹。
升级 laravel 版本后,您的控制器功能是否受到影响?知道如何解决这个问题。提前致谢。
由于错误涉及在您的一个关系中使用的列,因此更有可能在如何 Laravel "guesses" 关系中的 FK 列名称(模型 class),而不是控制器中的某些东西。
您可以在 belongsTo
的可选参数中明确指定 FK 名称,这样它就不会猜测了。检查签名:
public function belongsTo($related, $foreignKey = null, $ownerKey = null, $relation = null)
鉴于您在报告中的 FK-table 被命名为 site_id
,您会这样做:
public function site()
{
return $this->belongsTo('App\Site', 'site_id');
}
我深入研究了一下,发现了重大变化(在 5.4 中引入):https://github.com/laravel/framework/pull/16847 .
然而,这已记录在 the 5.4 Upgrade Guide 中:
If the foreign key is not explicitly specified when defining a relationship, Eloquent will now use the table name and primary key name for the related model to build the foreign key. (...) [T]he behavior could be different from previous releases if you are overriding the $primaryKey
property or getKeyName
method of the [related] model.
我的 ReportController
use App\Site;
use App\Report;
public function showSpecificSite($site_id){
$reports = Report::whereHas('site', function($query) use($site_id) {
$query->where('site_id', $site_id);
})->get(['email_date', 'url', 'recipient', 'report_id', 'site_id']);
$siteName = Site::find($site_id)->site_name;
return view('newsite', compact('site_id', 'siteName', 'reports'));
}
Route::get('sites/{site_id}',['as'=>'SpecificSite','uses'=>'ReportController@showSpecificSite']);
场地模型
public function report()
{
return $this->hasMany('App\Report');
}
报表模型
public function site()
{
return $this->belongsTo('App\Site');
}
我的blade视图
<a href="{{route('SpecificSite',['site_id'=>$record->site_id])}}">view</a>
这是我将 laravel 版本 5.2.36 升级到 5.4.36 的问题
这在 5.2 版本中运行良好,这里是 运行 的文章。
SQLSTATE[42S22]:未找到列:1054 'where clause' 中的未知列 'reports.site_site_id'(SQL:select email_date
, url
, recipient
, report_id
, site_id
from reports
where exists (select * from sites
where reports
.site_site_id
= sites
.site_id
和 site_id
= 1))
我的route:list和以前一样。我也已经有了 routes/web.app 文件夹。 升级 laravel 版本后,您的控制器功能是否受到影响?知道如何解决这个问题。提前致谢。
由于错误涉及在您的一个关系中使用的列,因此更有可能在如何 Laravel "guesses" 关系中的 FK 列名称(模型 class),而不是控制器中的某些东西。
您可以在 belongsTo
的可选参数中明确指定 FK 名称,这样它就不会猜测了。检查签名:
public function belongsTo($related, $foreignKey = null, $ownerKey = null, $relation = null)
鉴于您在报告中的 FK-table 被命名为 site_id
,您会这样做:
public function site()
{
return $this->belongsTo('App\Site', 'site_id');
}
我深入研究了一下,发现了重大变化(在 5.4 中引入):https://github.com/laravel/framework/pull/16847 .
然而,这已记录在 the 5.4 Upgrade Guide 中:
If the foreign key is not explicitly specified when defining a relationship, Eloquent will now use the table name and primary key name for the related model to build the foreign key. (...) [T]he behavior could be different from previous releases if you are overriding the
$primaryKey
property orgetKeyName
method of the [related] model.