[英]select few from one table and based on value fetch from another table
I have three tables:我有三个表:
Products
id
Downloads
id
user_id
product_id
Users
id
name
I'd like to fetch last X Download table rows, get their product_id & user_id, product_id match Product table as user_id match User table, then make我想获取最后 X 下载表行,获取他们的 product_id 和 user_id,product_id 匹配产品表,因为 user_id 匹配用户表,然后制作
foreach ($values as $value) {
<div class="product_title"> {{$product->name}} </div>
<div class="who_downloaded"> {{$user->name}} </div>
}
I can pass in my controller Download model, but it doesn't get me anywhere close我可以通过我的 controller 下载 model,但这并没有让我接近
public function index() {
return view('nav/index', [
'trendings' => Template::orderBy('views', 'DESC')->where('is_active', 'yes')->take(8)->get(),
'latests_downloads' => Downloads::orderBy('id', 'DESC')->take(8)->get(),
]);
}
'latests_downloads' => Downloads::orderBy('id', 'DESC')->take(8)->get()
function just gives me: 'latests_downloads' => Downloads::orderBy('id', 'DESC')->take(8)->get()
function 只是给我:
id ![]() |
user_id![]() |
product_id![]() |
---|---|---|
1 ![]() |
7 ![]() |
10 ![]() |
2 ![]() |
9 ![]() |
2 ![]() |
3 ![]() |
45 ![]() |
86 ![]() |
4 ![]() |
88 ![]() |
85 ![]() |
5 ![]() |
5 ![]() |
2 ![]() |
6 ![]() |
7 ![]() |
7 ![]() |
7 ![]() |
5 ![]() |
5 ![]() |
8 ![]() |
9 ![]() |
6 ![]() |
But how can I take these values and put in view?但是我怎样才能接受这些价值观并加以考虑呢?
Download
model class (If they don't already exist.)Download
model class (如果它们不存在。)public function user()
{
return $this->belongsTo(User::class, 'user_id');
}
public function product()
{
return $this->belongsTo(Product::class, 'product_id');
}
'latests_downloads' => Downloads::orderBy('id', 'DESC')->take(8)->get(),
// replace with the following
'latests_downloads' => Downloads::with(['user', 'product'])->orderBy('id', 'DESC')->take(8)->get(),
@foreach($latest_downloads as $latest_download)
User Id: {{ $latest_download->user->id }}
Product Id: {{ $latest_download->product->id }}
@endforeach
Yes for this type of scenario you need to use a relationship-based Eloquent model query of Laravel and it is very powerful for this type of scenario.是的,对于此类场景,您需要使用基于关系的 Eloquent model 查询 Laravel,它对于此类场景非常强大。
First You need to add two relationships in the Download model:首先需要在下载model中添加两个关系:
public function user()
{
return $this->belongsTo(User::class, 'user_id');
}
public function product()
{
return $this->belongsTo(Product::class, 'product_id');
}
And rest of the things work well with this single query.并且 rest 的东西与这个单一查询一起工作。
// user this query // 用户这个查询
$latest_downloads = Downloads::with(['user', 'product'])->latest()->take(8)->get();
instead of => getting two different queries in two different variables.而不是 => 在两个不同的变量中获取两个不同的查询。
You only need to get a single collection of records in a minimum query and a minimum number of variables.您只需要在最少的查询和最少数量的变量中获取单个记录集合。 You can do it with Eloquent easily.
您可以使用 Eloquent 轻松完成。
And last you can use inside blade as like normal we use daily.最后,您可以像我们日常使用的一样使用内刀片。
@foreach($latest_downloads as $key => $latest_download)
User ID: {{ $latest_download->user->id ?? '' }}
Product ID: {{ $latest_download->product->id ?? '' }}
@endforeach
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.