Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 2 years ago.
0

There may be no limit but i'm not sure the more relationships you have, the slower the query gets.

0

Thank you for giving me useful information. www.friv3new.com | www.kizi4new.com

Last updated 9 years ago.
0

thanks , not eager loading unless I need to and Ive doing a lot of advanced queries to make sure I dont suffer from the n+1 problem , I just wanted to make sure Im not setting my self up for pain latter down the track, I can not for see any issue but I thought I'd ask as there is always some one who know more.

0

Why would many relation definitions be a problem? Relations are only called when you specifically request them. You can write thousands, but if you don't you them, then there should be no problem.

0

ZERO if possible, normally two queries are better, this example isn't converted to blade/form helper yet but:
controller code to get a petowner and their pets using 2 queries:

public function ownerpet() {
            $ownerid = $_GET['ownerid'];
            $data['owners']=Powner::find($ownerid);
            $data['pets'] = DB::table('pets')
                        ->where('ownerid', '=', $ownerid)
                        ->orderBy('petname', 'asc')->get();

            return View::make('owner/ownerpet')->with('data',$data['owners'])->with('data2',$data['pets']);
        }

View showing owner first then listing of that owners pets:

<html>
<head>
</head>
<body>


<?php
echo $data->oname;  // the petowner
echo "<br>";
echo "======================";
echo "<br>";
foreach ($data2 as $pet){
    echo $pet->petid."   ".$pet->petname."<br>";  //pets owned by petowner

}    
?>
</body>
</html>

I used to run accounts payable and accounts receivable reports this way. This is 1000 times more
efficient than some of those crazy queries I see floating around. Some may be easy to write
using eloquent, but they become a mess trying to display results in the view.

Last updated 9 years ago.
0

jimgwhit said:

ZERO if possible, normally two queries are better, this example isn't converted to blade/form helper yet but:
controller code to get a petowner and their pets using 2 queries:

public function ownerpet() {
           $ownerid = $_GET['ownerid'];
           $data['owners']=Powner::find($ownerid);
           $data['pets'] = DB::table('pets')
                       ->where('ownerid', '=', $ownerid)
                       ->orderBy('petname', 'asc')->get();

           return View::make('owner/ownerpet')->with('data',$data['owners'])->with('data2',$data['pets']);
       }

View showing owner first then listing of that owners pets:

<html>
<head>
</head>
<body>


<?php
echo $data->oname;  // the petowner
echo "<br>";
echo "======================";
echo "<br>";
foreach ($data2 as $pet){
   echo $pet->petid."   ".$pet->petname."<br>";  //pets owned by petowner

}    
?>
</body>
</html>

I used to run accounts payable and accounts receivable reports this way. This is 1000 times more
efficient than some of those crazy queries I see floating around. Some may be easy to write
using eloquent, but they become a mess trying to display results in the view.

There's a number of things odd with your code, I'm on my phone so I'll keep it short.

  1. Using $_GET instead of using the variables in the route and having the controller function accept it as a variable?
  2. Pet Owner model is shortened to Powner which is only a few letters different but makes it harder to understand
  3. $data is never defined
  4. You assign the results to $data then send them back to the view outside of the array, what's the point?
  5. $data and $data2, especially when passed to a view, are extraordinary unhelpful, why not pass them back with proper names? $pets as $pet is much more understandable than $data2 as $pet.

The queries you run don't need to effect how the data is passed back to the view. If necessary you can always create an array or an stdclass object and format it as you please before passing it to the view.

Last updated 9 years ago.
0

I would use the request that's built into laravel if this was for real I whipped this out quick as just an example.

0

@apps4u Don't worry about it. Just use whatever your model needs. If you see any bottlenecks, then you optimize, not sooner.

Of course use eager loading, but from what you said I suppose you have that in mind already.

0

jimgwhit said:

ZERO if possible, normally two queries are better, this example isn't converted to blade/form helper yet but:
controller code to get a petowner and their pets using 2 queries:

public function ownerpet() {
           $ownerid = $_GET['ownerid'];
           $data['owners']=Powner::find($ownerid);
           $data['pets'] = DB::table('pets')
                       ->where('ownerid', '=', $ownerid)
                       ->orderBy('petname', 'asc')->get();

           return View::make('owner/ownerpet')->with('data',$data['owners'])->with('data2',$data['pets']);
       }

View showing owner first then listing of that owners pets:

<html>
<head>
</head>
<body>


<?php
echo $data->oname;  // the petowner
echo "<br>";
echo "======================";
echo "<br>";
foreach ($data2 as $pet){
   echo $pet->petid."   ".$pet->petname."<br>";  //pets owned by petowner

}    
?>
</body>
</html>

I used to run accounts payable and accounts receivable reports this way. This is 1000 times more
efficient than some of those crazy queries I see floating around. Some may be easy to write
using eloquent, but they become a mess trying to display results in the view.

Its off topic what you showing and its has many faults as others have said short names for classes make code harder to read and dont save you time as you have to stop and think when comming back to code as to what short name you end up using better to use full name (Look at Objective-C ) . And you could do the same amount of queries using Laravel Models as using DB raw and unless you are working with very large datasets then saving a micro second wont be noticed. I optimise queries but Im working with records in the ten's of thousands.

0

Your posts have been of great importance to me and I always come back here for new information. Congratulations for the work and wish you luck and success. Hope you like : www.friv4k.com - www.friv2015playonline.org

0

Sign in to participate in this thread!

Eventy

Your banner here too?

apps4u apps4u Joined 7 Apr 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.