如何解决如何在刀片上指定参数以访问表列之间的外键
我是 Laravel 的新手。现在,我有问题 使用我在下面 2 个表的关系之间使用的解决方案。
用户表
工作表
我将 user->id 存储在 created_by 和 updated_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 个角色)。
我遇到的问题是,当我尝试时,作业仪表板显示错误的用户名->名称 显示谁是创建作业的人(updated_by 也不正确)。
这是工作模型
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: {{$jobs->created_by->users['name']}}</div>
如果我使用 $jobs->['name'] 系统不知道哪个是 created_by 或 updated_by。 这里如何指定created_by和updated_by?
先谢谢你。如果你帮我解决这个问题,你就是救我一命的英雄。
附言如果我输入错误,请原谅我。我在日常生活中没有使用英语。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。