laravel scope relationship

Solutions on MaxInterview for laravel scope relationship by the best coders in the world

showing results for - "laravel scope relationship"
Emiliano
19 Apr 2020
1Problem :
2$activePosts = Post::where('active', true)->get();
3
4Solution: 
5
6class Post extends Model
7{
8    public function scopeActive($query)
9    {
10        return $query->where('active', 1);
11    }
12}
13
14$activePosts = Post::active()->get();
15=======================================================
16
17Create Dynamic Scope:
18
19class Post extends Model 
20{
21    public function scopeActive($query, $value)
22    {
23        return $query->where('active', $value);
24    }
25}
26
27// Get active posts
28$activePosts = Post::active(true)->get();
29
30// Get not active posts
31$notActivePosts = Post::active(false)->get();
32
33===========================================================
34
35Scope with Relation :
36
37$category = Category::find(1);
38$activePost = $category->posts()->active(true)->get();
39
Sami
23 May 2016
1class User extends Model {
2
3    protected function getDateFormat()
4    {
5        return 'U';
6    }
7
8}
queries leading to this page
scope laravel eloquentlaravel relationship scopelaravel eloquent relationships exampleeloquent query by scopelaravel scope access relationlaravel eloquent scopesscope with belongs to laravellaravel scopeeloquent relation with scopelaravel scope on relationshiplaravel scope by relationshipuse relation in scope laravellaravel scope and attachscope laravellaravel model scope functionlaravel scope in modellaravel scope with relationshiplaravel eloquent scopelaravel scope relationshipscope laravel examplescope laravel modelscope in laraveleloquent scope relationshiplaravel model scopelaravel scopeslaravel scopes relationshipslaravel relation with scopelaravel scope and relationshiplaravel eloquent scope relationshiplaravel use a scope to get data from a relationshiplaravel where in scopescopes 27 in models laravelusing scope in laravellaravel relation scopeeloquent relationship scopeslaravel use model scope in insertlaravel relationships with scopeusing laravel scopes with relationscope with relationship laravellaravel use scope in relationshipeloquent scope examplelaravel scope examplecreate scope in laravel modellaravel scope where relationlaravel use scopelaravel scope based on relationshiplaravel scope another relationship dblaravel scopes 3alaravel eloquent model query scopedlaravel relationship in model and scopelaravel model set scope model laravel scopesrelation in laravel scopeeloquent query scope on relationshipseloquent scope on relationshiphow to scope relations query laravellaravel get relation scopescope relationship laraveladd scope to relationship laravellaravel query scope relationshipscope model laravellaravel scope modellaravel relationship with scopeuse relationship in scope laraveluse relation in scope in laravellaravel model scope function with relationshipeloquent query scopelaravel global scope relationshipusing scope with laravellaravel model scope call laravel scope relationship