[英]Copying Data from table to another table and applying one to many relationship
这是我的报告模型
protected $fillable = [
'site_url',
'reciepients',
'monthly_email_date'
];
public function site()
{
return $this->belongsTo('App\Site');
}
这是我的网站模型
public function report()
{
return $this->hasMany('App\Report');
}
这是我的ReportController
public function showSpecificSite($site_name)
{
$records = DB::table('reports')
->select('email_date','url','recipient')
->whereHas('sites', function($query){
$query->where('site_name',$site_name);
})
->get();
return view('newsite')->with('records',$records)
->with('site_name',$site_name);
}
我的控制器还无法正常工作。 事情是我想将所有三个文件从站点表复制到报告表。
在insertInto
可能吗?
我在ReportController上的代码向您展示了我正在从报告表中选择数据,但是我是将数据放入报告表中以查看输出的人,但由于它无法伸出site_name的值,因此它仍无法正常工作,即使我已经在两个表之间建立了关系。
您实际上并没有在控制器中使用Eloquent,而只是在使用查询生成器( DB
)。 这意味着您无权访问Eloquent模型中的任何内容。
尝试:
$records = \App\Report::whereHas('site', function($query) use($site_name) {
$query->where('site_name', $site_name);
})->get(['id', 'email_date', 'url', 'recipient']);
我已经将id
添加到列列表中,因为我很确定您需要使用whereHas
。
注意要在闭包内部使用来自父作用域的变量,您需要使用use()
传递它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.