[英]How to fetch record from three table in laravel eloquent
I have four tables 我有四个桌子
jobposts
: jobposts
:
id | user_id | cat_id | job_title
1 | 1 | 1 | job 1
2 | 1 | 2 | job 2
3 | 2 | 3 | job 3
4 | 1 | 3 | job 4
categorymasters
: categorymasters
:
id | category_name
1 | cat1
2 | cat2
3 | cat3
4 | cat4
lastsubcategoryselectedbycompanies
: lastsubcategoryselectedbycompanies
:
id | jobposts_id | lastsubcategorymasters_id
1 | 1 | 1
2 | 1 | 2
3 | 2 | 3
4 | 1 | 3
lastsubcategorymasters
: lastsubcategorymasters
:
id | LastSubCategoryName
1 | lastsubcat1
2 | lastsubcat2
3 | lastsubcat3
4 | lastsubcat4
jobposts
have unique rows. jobposts
具有唯一的行。 lastsubcategoryselectedbycompanies
is a mapping of jobposts
and lastsubcategorymasters
. lastsubcategoryselectedbycompanies
是jobposts
和lastsubcategorymasters
的映射。 Now assume some user is logged in with their credentials (EX: take user_id
1 in jobposts
). 现在,假设某些用户使用其凭据登录(例如:
jobposts
user_id
1)。 Now I need to show LastSubCategoryName
in a comma separated list from the lastsubcategorymasters
table, grouped by the jobposts
, lastsubcategoryselectedbycompanies
and lastsubcategorymasters
tables. 现在,我需要在
lastsubcategorymasters
表中以逗号分隔的列表中显示LastSubCategoryName
,并按jobposts
, lastsubcategoryselectedbycompanies
和lastsubcategorymasters
表进行分组。
allpostedjob.blade.php
is: allpostedjob.blade.php
是:
@foreach($jobposteddetails as $jobposteddetail)
<tr>
<td>{{ $jobposteddetail->job_title }}</td>
</tr>
@endforeach
cotroller is: cotroller是:
public function index()
{
$user = Auth::user();
$jobposteddetails = jobpost::with('categorymaster')->where('user_id', '=', $user->id)->get();
return view('jobprovider.allpostedjob', compact('user','jobposteddetails'));
}
jobpost.php
model is: jobpost.php
模型是:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class jobpost extends Model
{
function categorymaster()
{
return $this->belongsTo(categorymaster::class, 'cat_id');
}
}
It is working proper. 它工作正常。
But I also need to show LastSubCategoryName
grouped by the tables jobposts
, lastsubcategoryselectedbycompanies
and lastsubcategorymasters
. 但我也需要显示
LastSubCategoryName
由表分组jobposts
, lastsubcategoryselectedbycompanies
和lastsubcategorymasters
。
function lastsubcategory()
{
return $this->belongsTo(lastsubcategoryselectedbycompanies::class);
}
It is not working. 它不起作用。 How can I fetch my result?
如何获取结果?
I am not very skilled at applying complex queries with eloquent, I prefer to use DB query builder with the join method 我不太擅长运用复杂的查询,我更喜欢将DB查询构建器与join方法一起使用
https://laravel.com/docs/5.8/queries https://laravel.com/docs/5.8/queries
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.