如何在刀片上指定参数以访问表列之间的外键

问题描述

我是 Laravel 的新手。现在,我有问题 使用我在下面 2 个表的关系之间使用的解决方案。

我有 2 个表,分别是作业和用户

一个在这用户表中有 4 个角色。

用户

id 用户 姓名 角色
1 A 用户A 管理员
2 B 用户B 供应商
3 C 用户C 销售人员
4 D 用户名 客户

工作表

我将 user->id 存储在 created_byupdated_by

id 姓名 created_by updated_by
1 作业 1 2 1
2 作业 2 3 4
3 作业 3 1 3
4 作业 4 4 2

我必须在 createjob.blade.PHP 上展示它们

例如

工作 1

一个下拉菜单 谁是销售员? (在此处显示所有销售人员)

第二个下拉 谁是供应商? (在此处显示所有供应商)

我还存储了谁创建了这个作业,任何人都可以创建作业(用户表中的 4 个角色)。

enter image description here

我遇到的问题是,当我尝试时,作业仪表板显示错误用户名->名称 显示谁是创建作业的人(updated_by 也不正确)。

enter image description here

这是工作模型

public function users(){
    return $this->belongsTo('App\user','created_by');
}

我尝试通过将 created_by 放在用户函数中来显示谁创建了作业(我只是尝试先执行此操作)

public function vendors(){
    return $this->belongsTo('App\user','vendor_id');
}

public function salespersons(){
    return $this->belongsTo('App\user','user_id');
}

功能索引

public function index(){

    $users = DB::table('users')
    ->where('role','vendor')
    ->where('role','salesperson')
    ->where('role','admin')
    ->get();

    return view('admin.jobDashboard',['users'=> $users])
    ->with('jobs',JOB::orderBy('id','desc')->paginate(25))
    ->with('typecategories',TypeCategories::all())
    ->with('salesperson',DB::select('select * from users where role = ?',['salesperson']))
    ->with('vendors',['vendor']))
    ->with('provinces',Province::all())
    ->with('districts',district::all())
    ->with('subdistricts',Subdistrict::all())
    ->with('images',Images::all())
    ->with('status',Status::all());
}

功能编辑

public function edit($id){

    $jobs=Job::find($id);
    $categories = Categories::all();
    $selectedCategories = $jobs->categories;

    $shop_product_type_categories = ShopProductTypeCategories::all();
    $selectedShopProductTypeCategories = $jobs->shop_product_type_categories;

    $users = DB::table('users')
    ->where('role','admin')
    ->get();

    return view('admin.editJob',[
                'jobs'=> $jobs,'categories' => $categories,'selectedCategories' => $selectedCategories,'shop_product_type_categories' => $shop_product_type_categories,'selectedShopProductTypeCategories' => $selectedShopProductTypeCategories,'users'=> $users,])
                ->with('typecategories',TypeCategories::all())
                ->with('salespersons',['salesperson']))
                ->with('vendors',['vendor']))
                ->with('provinces',Province::all())
                ->with('districts',district::all())
                ->with('subdistricts',Subdistrict::all())
                ->with('images',Images::all())
                ->with('status',Status::all());

}

这是我喜欢这个问题的页面

我不知道显示作业表的 create_by 列的正确方法,然后 访问作为用户名称的关系列。

<div class="title mb-3">Created_by:&nbsp;{{$jobs->created_by->users['name']}}</div>

enter image description here

如果我使用 $jobs->['name'] 系统不知道哪个是 created_by 或 updated_by。 这里如何指定created_by和updated_by?

先谢谢你。如果你帮我解决这个问题,你就是救我一命的英雄。

enter image description here

附言如果我输入错误,请原谅我。我在日常生活中没有使用英语。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...