Extend Eloquent Model: BaseModel or Trait?

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys today i want to show you how to deal with repetitive things in your eloquent models so for example if you have some condition like with where has and a similar condition is repeated in many eloquent models and you want to extract that into some kind of a function which you would reuse in many eloquent models so in other words how to extend the base laravel model with your own functionality on top and in this video i will show you two ways how to do that maybe you will add your own way in the comments be active there or tell me which one do you prefer with base model or with traits but before we dive deep into that example i want to apologize for my previous video this example is based on a video that i've published on this channel a week ago or so about simple query example where i was trying to filter the project by resources with title partner and one of the commenters marina beretta rightfully pointed out that the query was incorrect or in fact not fully correct so it was filtering correctly with where has but it wasn't filtering correctly with so the same condition query where should have been inside of width unfortunately i cannot reshoot the video and it was viewed already by 4 000 views so what i did i pinned the comment by marina so everyone would see that comment first and i will try to be more careful in the future with that in mind let's correct this mistake this time so we are filtering projects with many-to-many relationship with resources with condition of title partner so we need to add the same condition of where inside of with and here's where we get to today's topic these repetitive conditions may be reused somewhere else in other model another query and to avoid repeating those two you would like to have something like with where has so let's write that code hypothetically we don't have that function yet but we will create that in a minute so with where has would have the same parameters name of the relationship and then conditions like this so you would want to work with this query instead of those two so let's comment them out like this this would be shorter but where do we put that with warehouse there are two ways as i told you first you can create a base model so the model that would then be the base of all your other eloquent models which would contain some extra functionality on top of eloquent so we can do php autism make model base model which would be in the same app models folder and that is fine in here we don't need has factory we don't need this so in the base model we would add public function scope eloquent scope with where has in the parameters we have query then relationship name so relationship and then conditions and inside of the functions we would have query with and then we copy the logic from the controller so this is the relationship name and this is the condition so relationship conditions and then where has so same where has relationship conditions and now we can reuse that if we extend the project model with base model so by default project extends the model of eloquent but instead we would extend base model and then we donate this one on top let's try it out whether the query works refresh the page and we have the correct result so this is one way if you have some functionality repeating for eloquent models you can create your base model on top with adding scopes or whatever you want but then you would have extend other models from base model not from the eloquent model i'm not sure how i feel about this approach because then it requires to remember that you need to extend from base model including all the future models by future developers or if they forget to extend from base model then there may be inconsistency so personally i wouldn't choose that approach what i would choose instead is trades so let's create a trade file just in phpstorm app new file trades subfolder and then let's call it with where has dot php then we add php namespace would be app trades and then it would be a trade off with where has and inside we just copy the same function as from base model here scope with where has we paste and then in the model of project instead of having base model so let's revert that one let's extend model but then we would use the trade use with where has and on top here as well and then you can inject that trade use that trade only in the models that you actually want so it doesn't change any base models or any other models from your project that's why i like this approach more in controller nothing changes it's still with wire has it's just loaded from a trade not from the base model if we refresh it shouldn't break the result is the same so which approach do you prefer more traits or base model or maybe you have some other ideas shoot in the comments and if you want to dive deeper into eloquent or other larval structure i have a lot of courses around laravel on teachable platform the last one is better eloquent performance currently i'm working on a new course about graphql in laravel and vue apollo which will be released by the end of november so if you want to get all those courses and upcoming courses for a year the best way to do that is yearly membership with currently 22 courses available in that bundle and new ideas are coming for the year ahead and by doing that you're supporting this youtube channel because the more money i get from the courses the more free time i get to shoot free videos here on youtube see you guys in other videos
Info
Channel: Laravel Daily
Views: 7,419
Rating: undefined out of 5
Keywords:
Id: bNCuY99E8g8
Channel Id: undefined
Length: 5min 56sec (356 seconds)
Published: Sat Nov 20 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.