多个表数据与()一起使用

时间:2017-11-08 09:52:08

标签: php laravel laravel-5 eloquent laravel-5.5

在这里,我是雄辩的

$contacts = Contact::where('property_id',$commercial->id)->where('property_type','commercial_lease')
                ->with('contact_log')
                ->with('user_name')
                ->with('contact_log.contact_log_name')
                ->get();
  

->with('contact_log')   通过这种关系......我使用 - >(' contact_log')获取该表中的每个数据。但我希望最后通过与关系插入数据。

其他人将保持不变。但需要通过这种关系获得最后的数据。

1 个答案:

答案 0 :(得分:0)

You need to apply extra constraints on your contact_log relation in order to fetch only the last entry.

Replace

->with('contact_log')

with

->with('contact_log', function($query) {
  // order latest entries first
  $query->orderBy('created_at', 'DESC');
  // take only the first entry
  $query->take(1);
})