简体   繁体   English

select 很少从一个表中获取并基于从另一个表中获取的值

[英]select few from one table and based on value fetch from another table

I have three tables:我有三个表:

  • Products
    • id
    • other values其他值
  • 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 ID user_id用户身份 product_id产品编号
1 1个 7 7 10 10
2 2个 9 9 2 2个
3 3个 45 45 86 86
4 4个 88 88 85 85
5 5个 5 5个 2 2个
6 6个 7 7 7 7
7 7 5 5个 5 5个
8 8个 9 9 6 6个

But how can I take these values and put in view?但是我怎样才能接受这些价值观并加以考虑呢?

  1. Add the following methods to your 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');
}
  1. Change your controller.更改您的 controller。
'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(),
  1. Fetch it in your view file.在您的视图文件中获取它。
@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.

相关问题 根据另一个表中的值从一个表中选择值 - Select value from one table based on value from another 如何在一个 select 选项中获取选定值,并在此基础上,从 MySQL 表中获取数据,以相同形式显示另一个 select 选项 - How to get selected value in one select option and based on that, fetch data from MySQL table to show another select option in the same form 如何根据另一个表中的公共id值选择一个表中的记录? - How to select records in one table based on a common id value from another table? 根据另一个表的最小值选择表的属性 - Select attribute of table based on min value from another table 根据另一个表的值从一个表中选择 - Select from one table based on another tables values 从一个表中选择并包括来自另一个表的总和 - Select From One Table And Include Sum Value From Another 从一个表中取出一列,在另一个表中搜索相同的列值,最后从第三个表中取出并显示数据 - Fetch a column from one table, search the same column value in another table and finally fetch and display data from the third table 从一个表中选择,从另一个表中计数 - Select from one table and count from another 从一个表中选择所有行,并为另一表中的每一行选择一个特定值 - Select all rows from one table and one specific value for each row from another table Mysql - 如何根据我是否从另一个表中订阅它们来从一个表中选择条目? - Mysql - How to select entries from one table based on whether or not I subscribe to them from another table?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM