简体   繁体   English

如何使用雄辩的方法在Laravel中获得复杂关系

[英]How to get Complex relation in a Laravel using eloquent

I have below tables 我有下表

Users 用户数

id
name

Companies 公司介绍

id
user_id
name

Product Categories 产品类别

id
company_id
name

Products 产品展示

id
product_category_id
name

Sales 营业额

id
product_id
price
quantity
total

Now i want to get all sales for specified user's products. 现在,我想获得指定用户产品的所有销售额。 I have tried with below query. 我尝试了以下查询。 But i cannot use where for this. 但是我不能在哪里使用。 It is not apply this filter. 它不应用此过滤器。 Also i want to filter it by few other fields like users.user_name, products.product_name ... etc 我也想通过其他一些字段来过滤它,例如user.user_name,products.product_name ...等

$sales = Sale::with(['product'=>function($q) use ($user_id) {
        $q->with(['product_category' => function($q) use ($user_id) {
            $q->with(['company'=> function($q) use ($user_id) {
                $sUserName = Input::get('sUserName');
                $q->with(['user'=> function($q) use ($sUserName,$user_id) {
                    $q->where('id', $user_id);
                }]);
            }]);
        }]);
    }]);

Is it possible to use Eloquent for these situations ? 在这些情况下是否可以使用Eloquent? Or should i use join queries ? 还是应该使用联接查询? Please advice me. 请给我建议。 thanks 谢谢

you can use laravel eloqunt like this. 您可以像这样使用laravel eloqunt。

$Sales = DB::table('Sales') ->join('Products', 'Sales.product_id', '=', 'Products.id') ->join('ProductCategories', 'ProductCategories.id', '=', 'Products.product_category_id') ->join('companies', 'ProductCategories.company_id', '=', 'companies.id') ->join('Users', 'companies.user_id', '=', 'Users.id') ->select('Sales.*') ->get(); $ Sales = DB :: table('Sales')-> join('Products','Sales.product_id','=','Products.id')-> join('ProductCategories','ProductCategories.id', '=','Products.product_category_id')-> join('companies','ProductCategories.company_id','=','companies.id')-> join('Users','companies.user_id','= ','Users.id')-> select('Sales。*')-> get();

After madalinivascu advise i could be able to resolve my problem as below. 经过madalinivascu的建议,我可以如下解决我的问题。 Now i can filter it by Ordered User, Product name & dates. 现在,我可以按订购用户,产品名称和日期过滤它。

$sUserName = Input::get('sUserName');
    $sProductName = Input::get('sProductName');
    $sFrom = Input::get('sFrom');
    $sTo = Input::get('sTo');

    $user_id = \Auth::User()->id;


    // Show sales only for his products
    $sales = Sale::with('sold_to_user')
        ->whereHas('sold_to_user', function($q) use ($sUserName) {
            if(!empty($sUserName)) $q->where('name','LIKE', "$sUserName%");
        })->with('product')
        ->whereHas('product', function($q) use ($sProductName) {
            if(!empty($sProductName)) $q->where('name','LIKE', "$sProductName%");
            });

    if(\Auth::User()->privilege!='administrator') $sales = $sales->where('user_id','=',$user_id);


    $sales = $sales->orderBy('updated_at','desc')->paginate(100);   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM