php - 如何让用户 ID 登录并与他正在做的帖子一起注册?

标签 php mysql laravel

我正在开发一个 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/

相关文章:

php - 使用 2 个表合并具有相同 id 的 mysql 行

javascript - 想要通过在 codeigniter 中将数据导出到 Excel 工作表来设置表格边框

laravel - 如何在从 FormRequest 扩展的类中验证具有相同名称的多个输入

php - 使用 laravel forge 永远运行 artisan 命令?

laravel - 如何记录所有路由访问?

php - 使用先前值加上当前值更新每一行

php - PJAX 不工作

c# - 如何从特定列的表中获取不同的值?

Mysql触发查询并连接其他表

MySQL:如何手动引发 net_read_timeout 和 net_write_timeout 错误