我正在开发一个 web 应用程序,并且正在处理表之间的关系。我想做以下事情:用户将注册一台与之相关的打印机,程序将获取他在表单中输入的数据,并连同他的 ID 一起保存在数据库中,以便在我调用他时在另一个表中引用。
我尝试执行以下操作:
public function savePrinter(Request $request){
$user = $request->user()->id;
Printers::create($request->all(), $request->user()->id);
return redirect()->back();
}
打印机.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Printers extends Model
{
protected $fillable = [
'id',
'name_printer',
'link_printer',
'printer_id'
];
protected $table = 'printers';
}
我希望程序在打印机表中保存已登录用户的 ID 以供引用 观察
(the program is saving the correct printer name,I just can not save the logged user's id in
printer_id
)
最佳答案
在 Users
模型中添加此关系
public function Printer() {
return $this->hasMany(Printers::class,'printer_id','id');
}
now in your controller add this line
$user = $request->user()->Printer()->create($request->all());
关于php - 如何让用户 ID 登录并与他正在做的帖子一起注册?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56597651/