How to get parents data that only has a child data in laravel












0















I'm trying to get all the data from the parent that only has a child. Please see my code below.



$customers = Customer::with(['items' => function($query){
return $query->where('status', 2);
}])->get();

dd($customers);


But the code above returns all the customer. By the way, I'm using laravel 4.2.



Items Table:
enter image description here



Customer Table:
enter image description here










share|improve this question

























  • Have you created relationship between both customer and items? and show the customer model code.

    – Shail Paras
    Nov 23 '18 at 7:00
















0















I'm trying to get all the data from the parent that only has a child. Please see my code below.



$customers = Customer::with(['items' => function($query){
return $query->where('status', 2);
}])->get();

dd($customers);


But the code above returns all the customer. By the way, I'm using laravel 4.2.



Items Table:
enter image description here



Customer Table:
enter image description here










share|improve this question

























  • Have you created relationship between both customer and items? and show the customer model code.

    – Shail Paras
    Nov 23 '18 at 7:00














0












0








0








I'm trying to get all the data from the parent that only has a child. Please see my code below.



$customers = Customer::with(['items' => function($query){
return $query->where('status', 2);
}])->get();

dd($customers);


But the code above returns all the customer. By the way, I'm using laravel 4.2.



Items Table:
enter image description here



Customer Table:
enter image description here










share|improve this question
















I'm trying to get all the data from the parent that only has a child. Please see my code below.



$customers = Customer::with(['items' => function($query){
return $query->where('status', 2);
}])->get();

dd($customers);


But the code above returns all the customer. By the way, I'm using laravel 4.2.



Items Table:
enter image description here



Customer Table:
enter image description here







php laravel






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 26 '18 at 7:50







Jonjie

















asked Nov 23 '18 at 6:55









JonjieJonjie

3511529




3511529













  • Have you created relationship between both customer and items? and show the customer model code.

    – Shail Paras
    Nov 23 '18 at 7:00



















  • Have you created relationship between both customer and items? and show the customer model code.

    – Shail Paras
    Nov 23 '18 at 7:00

















Have you created relationship between both customer and items? and show the customer model code.

– Shail Paras
Nov 23 '18 at 7:00





Have you created relationship between both customer and items? and show the customer model code.

– Shail Paras
Nov 23 '18 at 7:00












4 Answers
4






active

oldest

votes


















3














with() is for eager loading. That basically means, along the main model, Laravel will preload the relationship(s) you specify. This is especially helpful if you have a collection of models and you want to load a relation for all of them. Because with eager loading you run only one additional DB query instead of one for every model in the collection.



has() is to filter the selecting model based on a relationship. So it acts very similarly to a normal WHERE condition. If you just use has('relation') that means you only want to get the models that have at least one related model in this relation.



e.g :



$users = Customer::has('items')->get();
// only Customer that have at least one item are contained in the collection


whereHas() works basically the same as has() but allows you to specify additional filters for the related model to check.



e.g



$users = Customer::whereHas('items', function($q){
$q->where('status', 2);
})->get();
// only customer that have item status 2


Adding group by to calculating sum
this is another example from my code :



Customer::select(['customer.name', DB::raw('sum(sale.amount_to_pay) AS total_amount'), 'customer.id'])
->where('customer.store_id', User::storeId())
->join('sale', 'sale.customer_id', '=', 'customer.id')
->groupBy('customer.id', 'customer.name')
->orderBy('total_amount', 'desc')
->take($i)
->get()


in your case :



Customer::select(['customer_id', DB::raw('sum(quantity) AS total')])
->whereHas('items', function ($q) {
$q->where('status', 2);
})
->groupBy('customer_id')
->get();


whereHas() allow you to filter data or query for the related model in your case
those customer that have items and it status is 2



afetr getting data we are perform ->groupBy('customer_id')



The GROUP BY statement is often used with aggregate functions (COUNT, MAX, MIN, SUM, AVG) to group the result-set by one or more columns.



select(['customer_id', DB::raw('sum(quantity) AS total')]) this will select customer id and calculate the sum of quantity column






share|improve this answer


























  • It is working. One more question. How can I group it by customers, so I can sum up the quantity using quantity field each customer.

    – Jonjie
    Nov 23 '18 at 8:48











  • if the asnwer is working then accept.

    – Dhruv Raval
    Nov 23 '18 at 8:53











  • can you display table format so i can understand well.

    – Dhruv Raval
    Nov 23 '18 at 8:53











  • Please check the update.

    – Jonjie
    Nov 23 '18 at 8:59











  • use groupBY() before get(); e.g : ->groupBY('customer_id')

    – Dhruv Raval
    Nov 23 '18 at 9:05





















1














You should use whereHas not with to check child existence.



$customers = Customer::whereHas('items', function($query){
return $query->where('status', 2);
})->get();

dd($customers);


I assume you already defined proper relationship between Customer and Item.






share|improve this answer
























  • It is working. One more question. How can I group it by customers, so I can sum up the quantity using quantity field each customer.

    – Jonjie
    Nov 23 '18 at 8:47



















1














You should try this:



$customers = Customer::whereHas('items', function($query){
$query->where('status', 2);
})->get();

dd($customers);





share|improve this answer































    0














            Customer::select(['items.customer_id',DB::raw('count(items.id) AS total_qty')])
    ->join('items', 'items.user_id', '=', 'customer.customer_id')
    ->groupBy('items.customer_id')
    ->havingRaw('total_qty > 2')
    ->get();


    OR



    $data=DB::select("select `items`.`customer_id`, count(items.id) AS total_qty
    from `customers`
    inner join `items`
    on `items`.`customer_id` = `customers`.`customer_id`
    group by `items`.`customer_id` having total_qty >= 2");


    correct table name and column name.






    share|improve this answer


























    • Can you also add the explanation?

      – Jonjie
      Nov 26 '18 at 8:16











    • is this working or note ?

      – Dhruv Raval
      Nov 26 '18 at 8:17











    • Nope. Still not working

      – Jonjie
      Nov 26 '18 at 8:28











    • Why do we have count() in DB raw? isn't that sum?

      – Jonjie
      Nov 26 '18 at 8:29











    • sum() vs count() stackoverflow.com/questions/48684192/…

      – Dhruv Raval
      Nov 26 '18 at 8:56











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53441941%2fhow-to-get-parents-data-that-only-has-a-child-data-in-laravel%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    4 Answers
    4






    active

    oldest

    votes








    4 Answers
    4






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    3














    with() is for eager loading. That basically means, along the main model, Laravel will preload the relationship(s) you specify. This is especially helpful if you have a collection of models and you want to load a relation for all of them. Because with eager loading you run only one additional DB query instead of one for every model in the collection.



    has() is to filter the selecting model based on a relationship. So it acts very similarly to a normal WHERE condition. If you just use has('relation') that means you only want to get the models that have at least one related model in this relation.



    e.g :



    $users = Customer::has('items')->get();
    // only Customer that have at least one item are contained in the collection


    whereHas() works basically the same as has() but allows you to specify additional filters for the related model to check.



    e.g



    $users = Customer::whereHas('items', function($q){
    $q->where('status', 2);
    })->get();
    // only customer that have item status 2


    Adding group by to calculating sum
    this is another example from my code :



    Customer::select(['customer.name', DB::raw('sum(sale.amount_to_pay) AS total_amount'), 'customer.id'])
    ->where('customer.store_id', User::storeId())
    ->join('sale', 'sale.customer_id', '=', 'customer.id')
    ->groupBy('customer.id', 'customer.name')
    ->orderBy('total_amount', 'desc')
    ->take($i)
    ->get()


    in your case :



    Customer::select(['customer_id', DB::raw('sum(quantity) AS total')])
    ->whereHas('items', function ($q) {
    $q->where('status', 2);
    })
    ->groupBy('customer_id')
    ->get();


    whereHas() allow you to filter data or query for the related model in your case
    those customer that have items and it status is 2



    afetr getting data we are perform ->groupBy('customer_id')



    The GROUP BY statement is often used with aggregate functions (COUNT, MAX, MIN, SUM, AVG) to group the result-set by one or more columns.



    select(['customer_id', DB::raw('sum(quantity) AS total')]) this will select customer id and calculate the sum of quantity column






    share|improve this answer


























    • It is working. One more question. How can I group it by customers, so I can sum up the quantity using quantity field each customer.

      – Jonjie
      Nov 23 '18 at 8:48











    • if the asnwer is working then accept.

      – Dhruv Raval
      Nov 23 '18 at 8:53











    • can you display table format so i can understand well.

      – Dhruv Raval
      Nov 23 '18 at 8:53











    • Please check the update.

      – Jonjie
      Nov 23 '18 at 8:59











    • use groupBY() before get(); e.g : ->groupBY('customer_id')

      – Dhruv Raval
      Nov 23 '18 at 9:05


















    3














    with() is for eager loading. That basically means, along the main model, Laravel will preload the relationship(s) you specify. This is especially helpful if you have a collection of models and you want to load a relation for all of them. Because with eager loading you run only one additional DB query instead of one for every model in the collection.



    has() is to filter the selecting model based on a relationship. So it acts very similarly to a normal WHERE condition. If you just use has('relation') that means you only want to get the models that have at least one related model in this relation.



    e.g :



    $users = Customer::has('items')->get();
    // only Customer that have at least one item are contained in the collection


    whereHas() works basically the same as has() but allows you to specify additional filters for the related model to check.



    e.g



    $users = Customer::whereHas('items', function($q){
    $q->where('status', 2);
    })->get();
    // only customer that have item status 2


    Adding group by to calculating sum
    this is another example from my code :



    Customer::select(['customer.name', DB::raw('sum(sale.amount_to_pay) AS total_amount'), 'customer.id'])
    ->where('customer.store_id', User::storeId())
    ->join('sale', 'sale.customer_id', '=', 'customer.id')
    ->groupBy('customer.id', 'customer.name')
    ->orderBy('total_amount', 'desc')
    ->take($i)
    ->get()


    in your case :



    Customer::select(['customer_id', DB::raw('sum(quantity) AS total')])
    ->whereHas('items', function ($q) {
    $q->where('status', 2);
    })
    ->groupBy('customer_id')
    ->get();


    whereHas() allow you to filter data or query for the related model in your case
    those customer that have items and it status is 2



    afetr getting data we are perform ->groupBy('customer_id')



    The GROUP BY statement is often used with aggregate functions (COUNT, MAX, MIN, SUM, AVG) to group the result-set by one or more columns.



    select(['customer_id', DB::raw('sum(quantity) AS total')]) this will select customer id and calculate the sum of quantity column






    share|improve this answer


























    • It is working. One more question. How can I group it by customers, so I can sum up the quantity using quantity field each customer.

      – Jonjie
      Nov 23 '18 at 8:48











    • if the asnwer is working then accept.

      – Dhruv Raval
      Nov 23 '18 at 8:53











    • can you display table format so i can understand well.

      – Dhruv Raval
      Nov 23 '18 at 8:53











    • Please check the update.

      – Jonjie
      Nov 23 '18 at 8:59











    • use groupBY() before get(); e.g : ->groupBY('customer_id')

      – Dhruv Raval
      Nov 23 '18 at 9:05
















    3












    3








    3







    with() is for eager loading. That basically means, along the main model, Laravel will preload the relationship(s) you specify. This is especially helpful if you have a collection of models and you want to load a relation for all of them. Because with eager loading you run only one additional DB query instead of one for every model in the collection.



    has() is to filter the selecting model based on a relationship. So it acts very similarly to a normal WHERE condition. If you just use has('relation') that means you only want to get the models that have at least one related model in this relation.



    e.g :



    $users = Customer::has('items')->get();
    // only Customer that have at least one item are contained in the collection


    whereHas() works basically the same as has() but allows you to specify additional filters for the related model to check.



    e.g



    $users = Customer::whereHas('items', function($q){
    $q->where('status', 2);
    })->get();
    // only customer that have item status 2


    Adding group by to calculating sum
    this is another example from my code :



    Customer::select(['customer.name', DB::raw('sum(sale.amount_to_pay) AS total_amount'), 'customer.id'])
    ->where('customer.store_id', User::storeId())
    ->join('sale', 'sale.customer_id', '=', 'customer.id')
    ->groupBy('customer.id', 'customer.name')
    ->orderBy('total_amount', 'desc')
    ->take($i)
    ->get()


    in your case :



    Customer::select(['customer_id', DB::raw('sum(quantity) AS total')])
    ->whereHas('items', function ($q) {
    $q->where('status', 2);
    })
    ->groupBy('customer_id')
    ->get();


    whereHas() allow you to filter data or query for the related model in your case
    those customer that have items and it status is 2



    afetr getting data we are perform ->groupBy('customer_id')



    The GROUP BY statement is often used with aggregate functions (COUNT, MAX, MIN, SUM, AVG) to group the result-set by one or more columns.



    select(['customer_id', DB::raw('sum(quantity) AS total')]) this will select customer id and calculate the sum of quantity column






    share|improve this answer















    with() is for eager loading. That basically means, along the main model, Laravel will preload the relationship(s) you specify. This is especially helpful if you have a collection of models and you want to load a relation for all of them. Because with eager loading you run only one additional DB query instead of one for every model in the collection.



    has() is to filter the selecting model based on a relationship. So it acts very similarly to a normal WHERE condition. If you just use has('relation') that means you only want to get the models that have at least one related model in this relation.



    e.g :



    $users = Customer::has('items')->get();
    // only Customer that have at least one item are contained in the collection


    whereHas() works basically the same as has() but allows you to specify additional filters for the related model to check.



    e.g



    $users = Customer::whereHas('items', function($q){
    $q->where('status', 2);
    })->get();
    // only customer that have item status 2


    Adding group by to calculating sum
    this is another example from my code :



    Customer::select(['customer.name', DB::raw('sum(sale.amount_to_pay) AS total_amount'), 'customer.id'])
    ->where('customer.store_id', User::storeId())
    ->join('sale', 'sale.customer_id', '=', 'customer.id')
    ->groupBy('customer.id', 'customer.name')
    ->orderBy('total_amount', 'desc')
    ->take($i)
    ->get()


    in your case :



    Customer::select(['customer_id', DB::raw('sum(quantity) AS total')])
    ->whereHas('items', function ($q) {
    $q->where('status', 2);
    })
    ->groupBy('customer_id')
    ->get();


    whereHas() allow you to filter data or query for the related model in your case
    those customer that have items and it status is 2



    afetr getting data we are perform ->groupBy('customer_id')



    The GROUP BY statement is often used with aggregate functions (COUNT, MAX, MIN, SUM, AVG) to group the result-set by one or more columns.



    select(['customer_id', DB::raw('sum(quantity) AS total')]) this will select customer id and calculate the sum of quantity column







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 26 '18 at 7:18

























    answered Nov 23 '18 at 7:10









    Dhruv RavalDhruv Raval

    1,0661313




    1,0661313













    • It is working. One more question. How can I group it by customers, so I can sum up the quantity using quantity field each customer.

      – Jonjie
      Nov 23 '18 at 8:48











    • if the asnwer is working then accept.

      – Dhruv Raval
      Nov 23 '18 at 8:53











    • can you display table format so i can understand well.

      – Dhruv Raval
      Nov 23 '18 at 8:53











    • Please check the update.

      – Jonjie
      Nov 23 '18 at 8:59











    • use groupBY() before get(); e.g : ->groupBY('customer_id')

      – Dhruv Raval
      Nov 23 '18 at 9:05





















    • It is working. One more question. How can I group it by customers, so I can sum up the quantity using quantity field each customer.

      – Jonjie
      Nov 23 '18 at 8:48











    • if the asnwer is working then accept.

      – Dhruv Raval
      Nov 23 '18 at 8:53











    • can you display table format so i can understand well.

      – Dhruv Raval
      Nov 23 '18 at 8:53











    • Please check the update.

      – Jonjie
      Nov 23 '18 at 8:59











    • use groupBY() before get(); e.g : ->groupBY('customer_id')

      – Dhruv Raval
      Nov 23 '18 at 9:05



















    It is working. One more question. How can I group it by customers, so I can sum up the quantity using quantity field each customer.

    – Jonjie
    Nov 23 '18 at 8:48





    It is working. One more question. How can I group it by customers, so I can sum up the quantity using quantity field each customer.

    – Jonjie
    Nov 23 '18 at 8:48













    if the asnwer is working then accept.

    – Dhruv Raval
    Nov 23 '18 at 8:53





    if the asnwer is working then accept.

    – Dhruv Raval
    Nov 23 '18 at 8:53













    can you display table format so i can understand well.

    – Dhruv Raval
    Nov 23 '18 at 8:53





    can you display table format so i can understand well.

    – Dhruv Raval
    Nov 23 '18 at 8:53













    Please check the update.

    – Jonjie
    Nov 23 '18 at 8:59





    Please check the update.

    – Jonjie
    Nov 23 '18 at 8:59













    use groupBY() before get(); e.g : ->groupBY('customer_id')

    – Dhruv Raval
    Nov 23 '18 at 9:05







    use groupBY() before get(); e.g : ->groupBY('customer_id')

    – Dhruv Raval
    Nov 23 '18 at 9:05















    1














    You should use whereHas not with to check child existence.



    $customers = Customer::whereHas('items', function($query){
    return $query->where('status', 2);
    })->get();

    dd($customers);


    I assume you already defined proper relationship between Customer and Item.






    share|improve this answer
























    • It is working. One more question. How can I group it by customers, so I can sum up the quantity using quantity field each customer.

      – Jonjie
      Nov 23 '18 at 8:47
















    1














    You should use whereHas not with to check child existence.



    $customers = Customer::whereHas('items', function($query){
    return $query->where('status', 2);
    })->get();

    dd($customers);


    I assume you already defined proper relationship between Customer and Item.






    share|improve this answer
























    • It is working. One more question. How can I group it by customers, so I can sum up the quantity using quantity field each customer.

      – Jonjie
      Nov 23 '18 at 8:47














    1












    1








    1







    You should use whereHas not with to check child existence.



    $customers = Customer::whereHas('items', function($query){
    return $query->where('status', 2);
    })->get();

    dd($customers);


    I assume you already defined proper relationship between Customer and Item.






    share|improve this answer













    You should use whereHas not with to check child existence.



    $customers = Customer::whereHas('items', function($query){
    return $query->where('status', 2);
    })->get();

    dd($customers);


    I assume you already defined proper relationship between Customer and Item.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 23 '18 at 7:03









    Ijas AmeenudeenIjas Ameenudeen

    6,89732948




    6,89732948













    • It is working. One more question. How can I group it by customers, so I can sum up the quantity using quantity field each customer.

      – Jonjie
      Nov 23 '18 at 8:47



















    • It is working. One more question. How can I group it by customers, so I can sum up the quantity using quantity field each customer.

      – Jonjie
      Nov 23 '18 at 8:47

















    It is working. One more question. How can I group it by customers, so I can sum up the quantity using quantity field each customer.

    – Jonjie
    Nov 23 '18 at 8:47





    It is working. One more question. How can I group it by customers, so I can sum up the quantity using quantity field each customer.

    – Jonjie
    Nov 23 '18 at 8:47











    1














    You should try this:



    $customers = Customer::whereHas('items', function($query){
    $query->where('status', 2);
    })->get();

    dd($customers);





    share|improve this answer




























      1














      You should try this:



      $customers = Customer::whereHas('items', function($query){
      $query->where('status', 2);
      })->get();

      dd($customers);





      share|improve this answer


























        1












        1








        1







        You should try this:



        $customers = Customer::whereHas('items', function($query){
        $query->where('status', 2);
        })->get();

        dd($customers);





        share|improve this answer













        You should try this:



        $customers = Customer::whereHas('items', function($query){
        $query->where('status', 2);
        })->get();

        dd($customers);






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 23 '18 at 7:06







        user10186369






























            0














                    Customer::select(['items.customer_id',DB::raw('count(items.id) AS total_qty')])
            ->join('items', 'items.user_id', '=', 'customer.customer_id')
            ->groupBy('items.customer_id')
            ->havingRaw('total_qty > 2')
            ->get();


            OR



            $data=DB::select("select `items`.`customer_id`, count(items.id) AS total_qty
            from `customers`
            inner join `items`
            on `items`.`customer_id` = `customers`.`customer_id`
            group by `items`.`customer_id` having total_qty >= 2");


            correct table name and column name.






            share|improve this answer


























            • Can you also add the explanation?

              – Jonjie
              Nov 26 '18 at 8:16











            • is this working or note ?

              – Dhruv Raval
              Nov 26 '18 at 8:17











            • Nope. Still not working

              – Jonjie
              Nov 26 '18 at 8:28











            • Why do we have count() in DB raw? isn't that sum?

              – Jonjie
              Nov 26 '18 at 8:29











            • sum() vs count() stackoverflow.com/questions/48684192/…

              – Dhruv Raval
              Nov 26 '18 at 8:56
















            0














                    Customer::select(['items.customer_id',DB::raw('count(items.id) AS total_qty')])
            ->join('items', 'items.user_id', '=', 'customer.customer_id')
            ->groupBy('items.customer_id')
            ->havingRaw('total_qty > 2')
            ->get();


            OR



            $data=DB::select("select `items`.`customer_id`, count(items.id) AS total_qty
            from `customers`
            inner join `items`
            on `items`.`customer_id` = `customers`.`customer_id`
            group by `items`.`customer_id` having total_qty >= 2");


            correct table name and column name.






            share|improve this answer


























            • Can you also add the explanation?

              – Jonjie
              Nov 26 '18 at 8:16











            • is this working or note ?

              – Dhruv Raval
              Nov 26 '18 at 8:17











            • Nope. Still not working

              – Jonjie
              Nov 26 '18 at 8:28











            • Why do we have count() in DB raw? isn't that sum?

              – Jonjie
              Nov 26 '18 at 8:29











            • sum() vs count() stackoverflow.com/questions/48684192/…

              – Dhruv Raval
              Nov 26 '18 at 8:56














            0












            0








            0







                    Customer::select(['items.customer_id',DB::raw('count(items.id) AS total_qty')])
            ->join('items', 'items.user_id', '=', 'customer.customer_id')
            ->groupBy('items.customer_id')
            ->havingRaw('total_qty > 2')
            ->get();


            OR



            $data=DB::select("select `items`.`customer_id`, count(items.id) AS total_qty
            from `customers`
            inner join `items`
            on `items`.`customer_id` = `customers`.`customer_id`
            group by `items`.`customer_id` having total_qty >= 2");


            correct table name and column name.






            share|improve this answer















                    Customer::select(['items.customer_id',DB::raw('count(items.id) AS total_qty')])
            ->join('items', 'items.user_id', '=', 'customer.customer_id')
            ->groupBy('items.customer_id')
            ->havingRaw('total_qty > 2')
            ->get();


            OR



            $data=DB::select("select `items`.`customer_id`, count(items.id) AS total_qty
            from `customers`
            inner join `items`
            on `items`.`customer_id` = `customers`.`customer_id`
            group by `items`.`customer_id` having total_qty >= 2");


            correct table name and column name.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 26 '18 at 9:48

























            answered Nov 26 '18 at 8:10









            Dhruv RavalDhruv Raval

            1,0661313




            1,0661313













            • Can you also add the explanation?

              – Jonjie
              Nov 26 '18 at 8:16











            • is this working or note ?

              – Dhruv Raval
              Nov 26 '18 at 8:17











            • Nope. Still not working

              – Jonjie
              Nov 26 '18 at 8:28











            • Why do we have count() in DB raw? isn't that sum?

              – Jonjie
              Nov 26 '18 at 8:29











            • sum() vs count() stackoverflow.com/questions/48684192/…

              – Dhruv Raval
              Nov 26 '18 at 8:56



















            • Can you also add the explanation?

              – Jonjie
              Nov 26 '18 at 8:16











            • is this working or note ?

              – Dhruv Raval
              Nov 26 '18 at 8:17











            • Nope. Still not working

              – Jonjie
              Nov 26 '18 at 8:28











            • Why do we have count() in DB raw? isn't that sum?

              – Jonjie
              Nov 26 '18 at 8:29











            • sum() vs count() stackoverflow.com/questions/48684192/…

              – Dhruv Raval
              Nov 26 '18 at 8:56

















            Can you also add the explanation?

            – Jonjie
            Nov 26 '18 at 8:16





            Can you also add the explanation?

            – Jonjie
            Nov 26 '18 at 8:16













            is this working or note ?

            – Dhruv Raval
            Nov 26 '18 at 8:17





            is this working or note ?

            – Dhruv Raval
            Nov 26 '18 at 8:17













            Nope. Still not working

            – Jonjie
            Nov 26 '18 at 8:28





            Nope. Still not working

            – Jonjie
            Nov 26 '18 at 8:28













            Why do we have count() in DB raw? isn't that sum?

            – Jonjie
            Nov 26 '18 at 8:29





            Why do we have count() in DB raw? isn't that sum?

            – Jonjie
            Nov 26 '18 at 8:29













            sum() vs count() stackoverflow.com/questions/48684192/…

            – Dhruv Raval
            Nov 26 '18 at 8:56





            sum() vs count() stackoverflow.com/questions/48684192/…

            – Dhruv Raval
            Nov 26 '18 at 8:56


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53441941%2fhow-to-get-parents-data-that-only-has-a-child-data-in-laravel%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

            Alcedinidae

            RAC Tourist Trophy