如何将下面两行代码写入一行代码:
$service_review->user_id=\Auth::user();
$user_service->service_reviews()->save($service_review);
原因是包含Auth :: user()的行正在抛出错误,因为它是我的" service_reviews"中的外键。表等等" DOESN有默认价值"
问题在于我是否优先考虑:
\Auth::user()->service_reviews()->save($service_review);
然后在这种情况下,授权用户被提取,但我的user_service_id现在抛出错误,因为" DOESN'有默认值"。
我的商店方法的代码(这是来自我的基于嵌套路线的ReviewsController:
Route :: resource(" services.reviews"," ReviewsController");如下:
public function store(ReviewsRequest $request, $id){
$service_review = new Service_review($request->all());
$user_service = User_service::findOrFail($id);
$service_review->user_id=\Auth::user();
$user_service->service_reviews()->save($service_review);
return redirect("reviews");
我认为在一行代码中传递它们将解决此错误。
答案 0 :(得分:0)
是的,你的对@MinaAbadir
我最终按如下方式进行:
public function store(ReviewsRequest $request, $id){
$service_review = new Service_review($request->all());
$user_service = User_service::findOrFail($id);
$service_review->user_id = \Auth::user()->email;
$user_service->service_reviews()->save($service_review);
return redirect("reviews");
}
似乎调用要检索的特定列会否定外键问题。也许MySQL通过Eloquent计算出这种关系。如果我能进一步清理它,请告诉我。