[英]how can i insert id value to a table from another table with laravel
i have the controller where i want to insert the id from table to another.我有控制器,我想将表中的 id 插入到另一个表中。
public function storecontact(Request $request)
{
$img_name=time() .'.' . $request->url->getClientOriginalExtension();
$this->validate($request,[
'content' => 'unique:emails,content,subject'
]);
$request->user()->id;
$email= new Email;
$email->name=request('name');
$email->user_id = auth()->user()->id;
$email->email=request('email');
$email->subject=request('subject');
$email->hood=request('hood');
$email->street=request('street');
$email->content=request('content');
$email->priority=request('priority');
$email->url=$img_name;
$email->status=("ממתין לטיפול");
//$email->subject_id= request($id);
// $email->subject_id=request($id);
$email->save();
$request->url->move(public_path('photos'),$img_name);
eturn redirect()->back();
}
the relationship for the model Email模型电子邮件的关系
public function subject()
{
return $this->belongsTo(Subject::class);
}
the relationship for the model Subject模型主题的关系
public function emails()
{
return $this->hasMany(Email::class);
}
i think the issue should be in controller it worked for me before maybe some lines of code deleted and i forgot how i did it.我认为问题应该出在控制器上,它对我有用,之前可能删除了一些代码行,我忘记了我是怎么做的。
You can just follow simple steps:您只需按照简单的步骤操作即可:
<?php
use App\User; \\table
use App\Email; \\table
//it will return single model
$user_id = User::select('id')->where('name','shewa')->first();
$create = Email::create([
'id' => $user_id,
'last_name' => 'Otwell',
'title' => 'Developer',
]);
//finally save
$create->save();
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.