There may be no limit but i'm not sure the more relationships you have, the slower the query gets.
Thank you for giving me useful information. www.friv3new.com | www.kizi4new.com
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.
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.
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.
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.
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.
I would use the request that's built into laravel if this was for real I whipped this out quick as just an example.
@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.
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.
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
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community