To add to tip 1. If you want to avoid "if" blocks, you can use the eloquent "when" method. return Property::query() ->when($request->get('rent'), function ($query) use ($request) { return $query->where('rent', 'when($request->get('rent'), fn($query) => $query->where('rent', '
@aschmelyun4 жыл бұрын
Just learned about the when() method from someone on dev.to! I'll definitely be keeping this in mind, as that's a way cleaner syntax.
@featurive4 жыл бұрын
Also I would recommend using $request->has() instead of get for if statements.
@CyanidePierce904 жыл бұрын
@@aschmelyun yeh it's much nicer, but we don't know these things until we know them :)
4 жыл бұрын
KZbin should add code formatting in the comment system... By the way I've learnt about $builder->when() from Reinink (Inertiajs).
@randomguy59224 жыл бұрын
nice.
@nightpegasus262 жыл бұрын
Thank you Andrew! Just what I was looking for, but couldn't find anywhere else!
@aschmelyun2 жыл бұрын
Thanks! I'm glad you liked it!
@ameerhamza43734 жыл бұрын
I haven't watched complete video but still i believe that this is going to be one of the best video on eloquent... Much love for you keep up the good work
@thebirdhasbeencharged4 жыл бұрын
Have you seen Jonathan Reinink's course. Gold! Not to take away from this video however. Wish these existed a couple years ago.
@laybyyourside4 жыл бұрын
Tip number 4 is what I’ve been seeking for a long time. Thanks a lot
@sumitsharma-us2sl3 жыл бұрын
Remember: sortBy won't work with pagination if you are looking for appended column sorted
@AnastasVartanyan4 жыл бұрын
The Tip four that adds requests count attribute will cause extra db call for each technician since to compute the model attributes aggregate query will be executed. To deal with this problem you can use Laravel Eloquent Query Builder `withCount` method like->withCount('requests'). The method will add requests_count attribute for each model with single query
@Shez-dc3fn4 жыл бұрын
alternatively he could just do $this->requests->count() in the accessor since your method means laravel might make two calls one for withCount and one for with?
@AnastasVartanyan4 жыл бұрын
@@Shez-dc3fn surely you can do this way too, but in that case you need to make sure that your relation is always loaded, if not the accessor will be querying the db. Also this way you force the count to be calculated in the collection, this means that php will count your relations and that can take some resources and memory if the count is huge, so I would prefer using withCount that will count the relations using SQL SUM aggregation, it will take a lot less resources with big data.
@tannercampbell4 жыл бұрын
Great video! I am constantly dealing with dates and expirations within my App, tip 5 is gonna be super helpful!
@MohanSingh-pl1mi3 жыл бұрын
I really like the way you are using to elaborate
@AbrahamChavez4 жыл бұрын
Great tips Andrew, I'm wondering what keyboard are you using, there is an audible click that I find very satisfying.
@sumitsharma-us2sl3 жыл бұрын
Not sure but it 'TVS Gold' sounds same, please check with your hardware and OS compatibility as some models are still mechanical.
@abiyouth_3 жыл бұрын
Agreed
@epslks4 жыл бұрын
I'd looking for tip-two to make a streamlined code for a long time , thanks a lot. Extraordinary !
@gergelygrusz7132 жыл бұрын
This is brilliant, thank you!
@randomguy59224 жыл бұрын
Very useful things..........i almost know all of them but its surely some parts where new. Thanks
@bf-xi3om4 жыл бұрын
Found your channel recently. Good Stuff, keep going.
@cultureofnepal20244 жыл бұрын
Awesome tips. Please post more video on eloquent tips Andrew.
@aschmelyun4 жыл бұрын
I'll have to compile some more and make a part 2!
@cultureofnepal20244 жыл бұрын
@@aschmelyun please share code too.
@brokoli57974 жыл бұрын
Great tips! saved my time so much.
@jecoy94134 жыл бұрын
what text editor are you using ?
@simbarashemaunga55752 жыл бұрын
This was an amazing tutorial!
@aibarra113 жыл бұрын
for the first optimization, there is also ->when for eloquent queries. No need to write out IF statements
@JohnnyBigodes4 жыл бұрын
And another great and useful examples. Thank you
@stefanbogdanovic5904 жыл бұрын
Awesome video, I have suggestion you could make a new custom Request for example TenantSearchRequest and you should make all query parameters nullable and add string boolean validation etc, so you could get only those that are inside the request so you don't have to be scared if someone sends you a parameter you don't expect in you API for an example, and you get those parameters with $request->validated() and apply same logic. And one more since you are using PHP storm use laravel ide helper to generate models annotations for intellisense, and you can access requests_count dynamic fields. Cheers mate!
@sayedahmadnaweed12 жыл бұрын
You are amazing. Thank you
@DANJ93Mort3 ай бұрын
Nice job, thanks
@197syahnur4 жыл бұрын
tip 2 is super brand new to me! Thankss
@jailsoncarneiro49363 жыл бұрын
Great Job! Thanks a lot!
@guilhermemoraes40553 жыл бұрын
What's ".test" on your URL project? Is this somehow online?
@FranckMercado3 жыл бұрын
It's a local custom domain he setup in his computer. In Linux you can do it by adding your domain to /etc/hosts file.
@vaibhavdeokar76424 жыл бұрын
nice video Which extention you use for suggetion
@CyanidePierce904 жыл бұрын
I believe that is PHPStorm, autocomplete is built into it.
@aschmelyun4 жыл бұрын
Correct, my IDE is PHPStorm. If you're asking about the suggestions in the terminal window, I'm using a ZSH plugin called zsh-autosuggestions.
@hariharan-wt6qk3 жыл бұрын
Thanks a lot❤️
@yacobee3 жыл бұрын
Thank you, how queries from relations e.g, products belong to a category and products also belongs to a type, how to get query of categories that their products have a specific type?
@lawrencediokno48832 жыл бұрын
how are you formatting your return data to look like that?
@namachivaaya4 жыл бұрын
Nice tips 👍👍👍
@sebastianromerolaguna74083 жыл бұрын
Thanks, it help me.
@pankajbokdia2 жыл бұрын
Super helpful!
@iShah3003 жыл бұрын
Hi andrew Great tips however i have a question Hoe about including join if conditiions is true Like if a request comes so i want to join a new table into existing eloquent query ...
@javieru58712 жыл бұрын
I think for tip 4, we can also use withCount from Lavaravel 5.2.32 or higher.
@delita00573 жыл бұрын
are you doing nuxt js tutorials?
@aschmelyun3 жыл бұрын
It's on my list, and I'm hoping to have a video showing how to get started with Nuxt pretty soon!
@mikevazovsky92113 жыл бұрын
Good!
@Chitte0113 жыл бұрын
You are awesome man, great respect for you! (y)
@rizkihutama56334 жыл бұрын
do you have a video that store json text to database, i'm still strugling with that?
@sretksor86892 жыл бұрын
what is the name of IDE that you using in video?
@mithicherbaro93944 жыл бұрын
Great tips 👍
@sebastianromerolaguna74083 жыл бұрын
Have you think in do an tutorial explaining queries for statistics. I haven seen videos about!. Have a good day
@mastago32264 жыл бұрын
thank you Andrew
@ehSamurai34833 жыл бұрын
In the first tip what is ::query() method?
@aschmelyun4 жыл бұрын
If you'd like to see the full source code behind this video, I've created a repo for it here: github.com/aschmelyun/video-5-laravel-eloquent-tips Also, if you'd like to directly support these videos and open-source projects, consider sponsoring me on GitHub: github.com/sponsors/aschmelyun
@indeveloperid60614 жыл бұрын
Why we have to write like "uri" or "action" at route code ?
@androidbornofficial51484 жыл бұрын
Amazing video. Please make video Vue js pagination with later with filter queue string.
@DRMOHAMEDKHDR3 жыл бұрын
Thanks alot
@davidmucioca64234 жыл бұрын
Very helpful
@arty43 жыл бұрын
For tip 1 u can use ->when($expr, $callback) function
@emadaldeenmukhtar4 жыл бұрын
perfect bro, keep it up
@sudheertripathi38823 жыл бұрын
tip3 is fire
@paulmimicry91474 жыл бұрын
How about the amount of query executed?
@hossamsalim44264 жыл бұрын
Could you help please - I have inventory system with 2 models (invoice) with relationship items morphMany Item model I need to get item record with MAX date of purchase
@johnmarkenriquez88084 жыл бұрын
Super helpful..
@sheenismhaellim22154 жыл бұрын
How does the object $poperties know what table to query just by initializing it with Property::query()?
@wsqplm4 жыл бұрын
Grabbed from laravel docs: By convention, the "snake case", plural name of the class will be used as the table name unless another name is explicitly specified. So, in this case, Eloquent will assume the Property model stores records in the properties table.
@grayaahammed9174 жыл бұрын
What IDE is used in this video please ?
@gobanq4 жыл бұрын
It’s phpstorm
@truongpm2144 Жыл бұрын
amazing !!!!
@edwinencomienda3 жыл бұрын
what’s your keyboard 😅?
@aschmelyun3 жыл бұрын
It's a DIY kit I bought and assembled. A GK64 PCB with Kailh Jade Box switches and thick PBT keycaps!
@Akimb3214 жыл бұрын
I believe there is no information about ::query method in Laravel docs (your first tip). It's an awesome tip, but I can't believe there's no info about this. Or I missed it?
@AdiSK4 жыл бұрын
Hi, thanks for the tips. I have one doubt, why not use built in withCount method to get the count on tip 4 How is your method better than the built in method. Great videos by the way🙂
@tannercampbell4 жыл бұрын
I think creating a model attribute is better in most scenarios to do this, it’s a bit more reusable if your response is returning more then the count. But you make good point that method works well too!
@TheMessixaviniesta4 жыл бұрын
The built in method is way better because with tip 4 you have to ALWAYS eager load the relationship or you will end up with a bunch of queries.
@TheMessixaviniesta4 жыл бұрын
For this particular case.
@TheMessixaviniesta4 жыл бұрын
Actually it would still make one query for each model even with eager loading because he's using the method "$this->requests()->count()" it would only be acceptable if replaced with "$this->requests->count()"
@naungyehtet12312 жыл бұрын
Subscribed
@loremipsum56423 жыл бұрын
Hi, can you provide a github repo for this?
@ethanj15334 жыл бұрын
What ide is that?
@etokafrancis57004 жыл бұрын
Tip 1: You could also use Laravel pipelines. It abstracts the 'ifs' into classes. If you need to add more filters, you just create a new class without modifying your controller classes app(Pipeline::class) ->send($this->model->query()) ->through( Client::class, CallStatus::class , Sort::class )->thenReturn()->get(); An example: It filters this model by client_id, call_status and then sorts them. The filters are run only when they are present in the request
@silvesterwali65653 жыл бұрын
i love this
@carlosfranciscobenegasbini783 жыл бұрын
Amazinng!
@josegerardosanchezalvarado47033 жыл бұрын
esto me salvo la vida User::whereHas('model', function ($query) { $query->where('propiedad', '!=', 'value'); })->with(['model', 'model'])->get();
@Arman-cn2tf4 жыл бұрын
Great
@pamithbosilu60553 жыл бұрын
Laravel Tips and Tricks - kzbin.info/www/bejne/oafPeIqnp8aSqMU
@fractalzombie3 жыл бұрын
Говнокодик конечно… есть ощущение, что только программисты из СНГ заботятся о качестве кода.
@asim-gandu-phenchod3 жыл бұрын
Diamond content
@davidspooner46843 жыл бұрын
Way too many commercials interrupting this tutorial. Hard to follow with so many interruptions.
@aschmelyun3 жыл бұрын
Hey David, I'm really sorry about that. I'll see if I can tone them down across my channel. Thanks for letting me know!
@davidspooner46843 жыл бұрын
@@aschmelyun Awesome. Thanks for the reply! Otherwise, I really enjoyed your content. Keep it up!
@joepalala47572 жыл бұрын
Another tip, You can now use ->latest() in your queries which will getlatest by created date (afaik)
@robertoflores20782 жыл бұрын
I've been using Mehradsadeghi\FilterQueryString for years. Forget about if's and when's. Let the library do all that work for you. I can't find a cleaner way to do it.
@pamithbosilu60553 жыл бұрын
Laravel Tips and Tricks - kzbin.info/www/bejne/oafPeIqnp8aSqMU