Repeating Eloquent Scopes into Reusable Traits

  Рет қаралды 6,128

Laravel Daily

Laravel Daily

Күн бұрын

If you have Eloquent local scopes repeating in multiple Models, it's a candidate for refactoring into Traits!
My course "Eloquent: The Expert Level" laraveldaily.c...
- - - - -
Support the channel by checking out my products:
- My Laravel courses: laraveldaily.c...
- Laravel QuickAdminPanel: quickadminpane...
- Livewire Kit Components: livewirekit.com
- - - - -
Other places to follow:
- My weekly Laravel newsletter: us11.campaign-...
- My personal Twitter: / povilaskorop

Пікірлер: 23
@earhackerdem
@earhackerdem Жыл бұрын
Veo tus videos antes y después de ir a dormir, estoy como JR de Laravel y ver tus videos me ayuda mucho Saludos
@qb9801
@qb9801 Жыл бұрын
Thank you for videos, that is helpful. I begin my each work day with your video and know that i get some new and helpful information. Laravel is cool.
@sagardolui2998
@sagardolui2998 Жыл бұрын
very useful, thank you
@kenjohnsiosan9707
@kenjohnsiosan9707 Жыл бұрын
As always very helpful.
@singlapanther1374
@singlapanther1374 Жыл бұрын
It's very useful
@singlapanther1374
@singlapanther1374 Жыл бұрын
धन्यवाद श्रीमान
@danngzshot4827
@danngzshot4827 Жыл бұрын
thks master
@christianesoliman8087
@christianesoliman8087 Жыл бұрын
Thank you for the great information, teacher. Can you show us how to dynamically translate the site from the database?
@LaravelDaily
@LaravelDaily Жыл бұрын
There are packages like Laravel translatable, please search my channel, I had videos about it
@christianesoliman8087
@christianesoliman8087 Жыл бұрын
@@LaravelDaily Thank you very much😇
@flyer0022f
@flyer0022f Жыл бұрын
Isn't it also possible to create a baseModel. And make your models extend this new BaseModel with the repeated code ?
@TouiKinMinao
@TouiKinMinao Жыл бұрын
how about without using Traits?
@waqaransari7274
@waqaransari7274 Жыл бұрын
Thank you for this video this is really helpful for me. Can you please guide me how can I modify this code. $maxId = LongTermContracts::UserById()->whereYear("created_at", date('Y'))->count() + 1; $maxId = str_pad($maxId, 5, 0, STR_PAD_LEFT); $request->request->add(['contract_no' => "LT" . date('Y') . "-" . $maxId, 'user_id' => auth()->user()->id]); $contract = LongTermContracts::create($request->all()); Thank you again.
@LaravelDaily
@LaravelDaily Жыл бұрын
Modify for what? What is the actual problem or goal to solve?
@waqaransari7274
@waqaransari7274 Жыл бұрын
@@LaravelDaily the goal is to optimize this code to shorter way.
@waqaransari7274
@waqaransari7274 Жыл бұрын
@@LaravelDaily I just want to create a contract no when contract create and the number will be like LT20230001 so when I create a contract I need to create first this number and then create contract. So i need to do in a shorter way.
@LaravelDaily
@LaravelDaily Жыл бұрын
Not sure if that can be much optimized, maybe just not using some temporary variables, bu tthen lines would get longer. Just a few things. 1. Instead of auth()->user()->id you can use auth()->id() 2. Don't use $request->all() it's considered unsafe: laraveldaily.com/post/laravel-request-all-security-issue
@waqaransari7274
@waqaransari7274 Жыл бұрын
​@@LaravelDaily Thank you sir for your valuable time that you replied me. so Humble and Highly Appreciated.
@er.anilkumarthakur1493
@er.anilkumarthakur1493 Жыл бұрын
//its better to make it work with the date range... if I am wrong correct me ///trait public function scopeRecent($query, $search, $column = 'created_at') { if (empty($search) || ! isset($search)) { return $query; } return $query->whereDate($this->getTable().'.'. $column, '>=', Carbon::parse(current(explode(' to ', $search)))->startOfDay()->toDateString()) ->whereDate($this->getTable().'.'.$column, '
@LaravelDaily
@LaravelDaily Жыл бұрын
That probably may be more accurate, yes, the date accuracy was not the point of this video
@maulvieyazid7652
@maulvieyazid7652 Жыл бұрын
Instead of concatenating $this->getTable . '.created_at' You can use $this->qualifyColumn('created_at') Its cleaner in my opinion Its not in laravel documentation, but this method exist in Model class
@krzysiej--9229
@krzysiej--9229 Жыл бұрын
this ☝
@Niboros
@Niboros Жыл бұрын
Thank you for sharing that usefullness!
SOLID Principles in Laravel: 5 Examples (+ New Course!)
21:07
Laravel Daily
Рет қаралды 73 М.
How/When to Use Traits in Laravel: My Opinion
7:03
Laravel Daily
Рет қаралды 26 М.
Самое неинтересное видео
00:32
Miracle
Рет қаралды 1,2 МЛН
大家都拉出了什么#小丑 #shorts
00:35
好人小丑
Рет қаралды 81 МЛН
Dad gives best memory keeper
01:00
Justin Flom
Рет қаралды 20 МЛН
Load More Pagination In VueJS (Options API) | Urdu & Hindi
10:37
KMP vs. Flutter - Who Will Win The Cross-Platform Battle?
16:19
Philipp Lackner
Рет қаралды 46 М.
The Tools I Use to Build Products in Laravel
19:00
Josh Cirre
Рет қаралды 27 М.
Laravel Basics - Scopes
16:36
Laratips
Рет қаралды 5 М.
BEWARE these Laravel attributes | Real World Laravel
19:10
Sam Lewis
Рет қаралды 2,9 М.
Turns out REST APIs weren't the answer (and that's OK!)
10:38
Dylan Beattie
Рет қаралды 156 М.
Laravel DB Optimization: From 1.5k to 26 Queries
14:49
Laravel Daily
Рет қаралды 20 М.
Why More People Dont Use Linux
18:51
ThePrimeTime
Рет қаралды 182 М.
Bet Against SQL: Queries as Code by James Cowling & Sujay Jayakar
40:45
Two Things Laravel Services Should NOT Do
8:20
Laravel Daily
Рет қаралды 21 М.
Самое неинтересное видео
00:32
Miracle
Рет қаралды 1,2 МЛН