17. SCOPES ‐ LOCAL
Local scopes are specific to
the model.
Although, we can always
extend the class ;‐﴿
18. SCOPES ‐ LOCAL
Do this once
class Developer extends Model
{
public function scopeSenior($query)
{
return $query‐>where('yrs_of_exp', '>', 3);
}
}
19. SCOPES ‐ LOCAL
Use anytime!
$senior_devs = AppDeveloper::senior()‐>get()
20. SCOPES ‐ LOCAL
We can accept additional
parameters to make our
scopes dynamic
21. SCOPES ‐ LOCAL
class Developer extends Model
{
public function scopeUses($query, $language)
{
return $query‐>where('language', $language);
}
}
Make it dynamic
23. SCOPES ‐ GLOBAL
We can define a scope that
will be applied to all
queries by our Model
24. SCOPES ‐ GLOBAL
use IlluminateDatabaseEloquentScope;
class VerifiedScope implements Scope
{
public function apply($builder, $model)
{
$builder‐>where('verified', true);
}
}
Define the scope
25. class Developer extends Model
{
protected static function boot()
{
parent::boot();
static::addGlobalScope(new VerifiedScope);
}
}
SCOPES ‐ GLOBAL
Include the scope
26. SCOPES ‐ GLOBAL
Now all queries from our
Developer model will
retrieve only verified
entries.
27. SCOPES ‐ GLOBAL
// Global scope automatically applied
$verified_devs = Developer::get()
// Query without the Verified global scope
Developer::withoutGlobalScope(VerifiedScope::class)‐>get()
// Query without any global scope
Developer:: withoutGlobalScopes()‐>get()
Define the scope
33. COLLECTIONS
Overwrite the newCollection method
class Developer extends Model
{
public function newCollection(array $models = [])
{
return new CustomCollection($models);
}
}
37. Sometimes we need to hide stuff so...
SERIALIZATION
class User extends Model
{
protected $hidden = ['age', 'address'];
// OR
protected $visible = ['company', 'name'];
}
38. Other times we want to access hidden
stuff so...
SERIALIZATION
$user = AppUser::find(1);
// maybe when the user is viewing
$with_age = $user‐>makeVisible('age')‐>toArray();
// for corporate reasons maybe ˉ_(ツ)_/ˉ
$without_company = $user‐>makeHidden('company')‐>toArray;
39. What if we just want to add stuff?
SERIALIZATION
class User extends Model
{
protected $appends = ['birth_year'];
public function getIBirthYearAttribute()
{
return date("Y") ‐ $this‐>age;
}
}
41. EVENTS & OBSERVERS
Laravel events are a great
way to subscribe and listen
for various events that
occur in our application.
https://laravel.com/docs/5.4/events
46. EVENTS & OBSERVERS
Create the Observer class
use AppMeetup;
class MeetupObserver
{
public function saving (Meetup $meetup)
{
// do something for the 'saving' event
// you an have more functions with 'event' names
}
}
47. EVENTS & OBSERVERS
Register the observer in a ServiceProvider
use AppMeetup;
class MyServiceProvider
{
public function boot ()
{
Meetup::observe(MeetupObserver::class);
}
}
51. users
id ‐ integer
name ‐ string
contact
id ‐ integer
user_id ‐ integer
phone ‐ string
email ‐ string
website ‐ string
Example Database schema
RELATIONSHIPS ‐ 1‐1
52. Defining one side of the relationship
class User extends Model
{
public function contact()
{
return $this‐>hasOne('AppContact');
}
}
RELATIONSHIPS ‐ 1‐1
53. Defining the other side
class Contact extends Model
{
public function user()
{
return $this‐>belongsTo('AppUser');
}
}
RELATIONSHIPS ‐ 1‐1
54. $user = AppUser::find(1);
// Instead of
$contact = AppContact::select('contacts.*')
‐>where('user_id', '=', $user‐>id)
‐>first();
// We do
$contact = $user‐>contact;
RELATIONSHIPS ‐ 1‐1
56. Example Database schema
users
id ‐ integer
name ‐ string
contact
id ‐ integer
user_id ‐ integer
type ‐ string
value ‐ string
RELATIONSHIPS ‐ 1‐n
57. Defining one side of the relationship
class User extends Model
{
public function contacts()
{
return $this‐>hasMany('AppContact');
}
}
RELATIONSHIPS ‐ 1‐n
58. Defining the other side
class Contact extends Model
{
public function user()
{
return $this‐>belongsTo('AppUser');
}
}
RELATIONSHIPS ‐ 1‐n
59. RELATIONSHIPS ‐ 1‐n
$user = AppUser::find(1);
// Instead of
$contacts = AppContact::select('contacts.*')
‐>where('user_id', '=', $user‐>id)
‐>get();
// We do
$contacts = $user‐>contacts;
61. Example Database schema
users
id ‐ integer
name ‐ string
contact
id ‐ integer
user_id ‐ integer
type_id ‐ integer
value ‐ string
type
id ‐ integer
name ‐ string
RELATIONSHIPS ‐ n‐n
62. Defining one side of the relationship
class User extends Model
{
public function types()
{
return $this‐>belongsToMany('AppType')
‐>withPivot('id', 'value');
}
}
RELATIONSHIPS ‐ n‐n
63. Defining the other side
class Type extends Model
{
public function users()
{
return $this‐>belongsToMany('AppUser');
}
}
RELATIONSHIPS ‐ n‐n
64. RELATIONSHIPS ‐ 1‐n
// Instead of
$contacts = AppContact::select('contacts.*')
‐>join('types', 'contact.type_id', '=', 'types.id')
‐>where('user_id', '=', $user‐>id)
‐>where('type.name', '=', 'phone')
‐>get();
// We do
$types = $user‐>types;
//access throught the pivot property
66. Example Database schema
users
id ‐ integer
name ‐ string
posts
id ‐ integer
user_id ‐ integer
title ‐ string
body ‐ string
comments
id ‐ integer
post_id ‐ integer
title ‐ string
body ‐ string
RELATIONSHIPS ‐ distant
67. Defining the relationship
class User extends Model
{
public function comments()
{
return $this‐>hasManyThrough('AppPost', 'AppComment);
}
}
RELATIONSHIPS ‐ distant
68. // Instead of
$comments = AppComment::select('comments.*')
‐>join('posts', 'comment.post_id', '=', 'posts.id')
‐>join('users', 'post.user_id', '=', 'users.id')
‐>where('user_id', '=', $user‐>id)
‐>get();
// We do
$comments = $user‐>comments;
RELATIONSHIPS ‐ distant
70. RELATIONSHIPS ‐ morph
We can allow more than
one model to relate with
our model.
Many applications
71. Example Database schema
users
id ‐ integer
name ‐ string
groups
id ‐ integer
name ‐ string
pictures
id ‐ integer
path‐ string
owner_id ‐ integer
owner_type ‐ string
RELATIONSHIPS ‐ morph
72. RELATIONSHIPS ‐ notes
Dynamic properties
$post‐>comments; //get all comments as a collection
$post‐>comments(); //returns a relationship class
// Can be used as query builders
$post‐>comments()‐>where('like', '>', 5);
73. RELATIONSHIPS ‐ notes
Check if it exists
Post::has('comments', '>=', 3)‐>get();
Post::doesntHave('comments')‐>get();
74. RELATIONSHIPS ‐ notes
Eager Loading
// Instead of this
$users = AppUser::all();
foreach ($users as $user) {
echo $user‐>contact‐>phone;
}
// We do this
$user = AppUser::with('contact')‐>get();
75. Inserting & Updating
create﴾﴿ save﴾﴿
associate﴾﴿ dissociate﴾﴿
attach﴾﴿ detach﴾﴿
sync﴾﴿ toggle﴾﴿
RELATIONSHIPS ‐ notes
We have many ways to modify relationships
76. RELATIONSHIPS ‐ notes
So many more lovely
capabilities.
https://laravel.com/docs/5.4/
eloquent‐relationships