Reputation: 418
Following is the query which retrives all the order rows from the orders table
$orders = auth()->user()->orders;
or
$orders = Order::where('user_id',auth()->id())->get();
or
$orders = \DB::table('orders')->where('user_id',Auth::id())->get();
If orders table 100 000 rows then which of the above method is ideal to use?
Upvotes: 0
Views: 81
Reputation: 2936
compare to laravel eloquent DB:: class is faster way to retrieve data from database but in our case we manage a application in structure way and its depend on your coding standard and module management.
i suggest to use :
$sql=auth()->user()->orders()->toSql();
Upvotes: 1
Reputation: 1938
Actually in Laravel you can use toSql method to compare the queries and see which one is the most optimized query like this:
$sql=auth()->user()->orders()->toSql();
But in the example above we see generally all of them are the same:
//First
"select * from `orders` where `orders`.`user_id` = ? and `orders`.`user_id` is not null"
//Second
"select * from `orders` where `user_id` = ?"
//Third
"select * from `orders` where `user_id` = ?"
As you see the second one and the third one are totally the same but the first one has an extra and statement and it's more reliable so I think the execution times are not different and you'd better to use the first one.
Upvotes: 1