【问题标题】:Laravel 4 - get json list one table with relationsLaravel 4 - 获取 json 列表一张有关系的表
【发布时间】:2013-12-23 20:52:08
【问题描述】:

我有一张桌子customers 和一张桌子orders。 我想进行 ajax 调用以获取名称为 x 的所有客户及其订单的列表。 我得到了 ajax 函数,我检索客户的函数如下所示:

$customers = Customer::where('first_name', 'LIKE', '%'.$name.'%')
        ->orWhere('last_name', 'LIKE', '%'.$name.'%')
        ->where('user_id', Auth::user()->id)
        ->get()
            ->toJson();
return $customers;

这样我只能获取所有客户,我可以修改它以获取他们的订单吗? 我想我可以通过 join 做到这一点,还是有什么神奇的功能?

谢谢

【问题讨论】:

    标签: php json laravel laravel-4


    【解决方案1】:

    您可以使用关系:点击链接并创建一对多关系并在查询中使用该关系。 http://laravel.com/docs/eloquent#relationships

    $customers = Customer::with('orders')
            ->where('first_name', 'LIKE', '%'.$name.'%')
            ->orWhere('last_name', 'LIKE', '%'.$name.'%')
            ->where('user_id', Auth::user()->id)
            ->get();
    
    return $customers->toJson();
    

    【讨论】:

      猜你喜欢
      • 2020-02-21
      • 2016-04-25
      • 2020-09-16
      • 1970-01-01
      • 2013-05-22
      • 2015-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多