.
Отношения / Relations
- Отношения «один к одному»
- Отношение «один ко многим»
- Имеет отношение «один из многих»
- Отношения HasOneThrough и HasManyThrough
- Отношение «многие ко многим»
- Полиморфные отношения
- Полиморфный Один к одному
- Полиморфный Один Ко Многим
- Полиморфный Один из Многих
- Полиморфный Многие-К-Многим
Ссылки
Отношения «один к одному»
$ php artisan make:model Tenant $ Php artisan make:model Rent
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; class Tenant extends Model { /** * Get the rent of a Tenant */ public function rent() { return $this->hasOne(Rent::class); //return $this- >hasOne(Rent::class, "custom_key"); //return $this->hasOne(Rent::class, "custom_key", "other_key"); } } $rent = Tenant::find(10)->rent;
Отношение «один ко многим»
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; class Tenant extends Model { /** * Get the rents of a Tenant */ public function rent() { return $this->hasMany(Rent::class); //return $this->hasMany(Rent::class, "foreign_key"); //return $this->hasMany(Rent::class, "foreign_key", "local_key"); } } $rents = Tenant::find(10)->rent()->where('payment', '>', 500)->first();
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; class Rent extends Model { /** * Return the tenant for the rent */ public function tenant() { return $this->belongsTo(Tenant::class); } } $tenant = Rent::find(1)->tenant;
Имеет отношение «один из многих»
public function latestRent() { return $this->hasOne(Rent::class)->latestOfMany(); } public function oldestRent() { return $this->hasOne(Rent::class)->oldestOfMany(); } return $this->hasOne(Rent::class)->ofMany('price', 'min');
Отношения HasOneThrough и HasManyThrough
rent id - integer name - string value - double tenants id - integer name - string rent_id - integer landlord id - integer name - string tenant_id - integer
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; class Rent extends Model { /** * Return the rents' landlord */ public function rentLandlord() { return $this->hasOneThrough(Landlord::class, Tenant::class); } }
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; class Tenant extends Model { /** * Get the rents of a Tenant */ public function rent() { return $this->hasMany(Rent::class); } }
Аналогично, отношение «Has Many Through»
country id - integer name - string user id - integer country_id - integer name - string games id - integer user_id - integer title - string
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; class Country extends Model { protected $fillable = ['name']; public function users() { return $this->hasMany(User::class); } public function games() { return $this->hasManyThrough(Games::class, User::class); } }
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; class User extends Model { protected $fillable = [article_id, 'name']; public function country() { return $this->belongsTo(Country::class); } public function posts() { return $this->hasMany(Post::class); } }
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; class Game extends Model { protected $fillable = ['user_id', 'title']; public function user() { return $this->belongsTo(User::class); } }
<?php $country = Country::find(159); // Retrieve all games for the country $games = $country->games;
Отношение «многие ко многим»
employees id - integer name - string roles id - integer name - string role_employees user_id - integer role_id - integer
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; class Employee extends Model { public function roles() { return $this- >belongsToMany(Role::class); } }
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; class Role extends Model { public function employees() { return $this->belongsToMany(Employee::class); return $this->belongsToMany(Employee::class) ->withPivot("active", "created_at"); return $this->belongsToMany(Employee::class) ->withTimestamps(); return $this->belongsToMany(Employee::class) ->as('subscription') ->withPivot("active", "created_by"); return $this->belongsToMany(Employee::class) ->wherePivot('promoted', 1); return $this->belongsToMany(Employee::class) ->wherePivotIn('level', [1, 2]); return $this->belongsToMany(Employee::class) ->wherePivotNotIn('level', [2, 3]); return $this->belongsToMany(Employee::class) ->wherePivotBetween('posted_at', ['2023-01-01 00:00:00', '2023-01-02 00:00:00']); return $this->belongsToMany(Employee::class) ->wherePivotNull('expired_at'); return $this->belongsToMany(Employee::class) ->wherePivotNotNull('posted_at'); return $this->belongsToMany(Employee::class) ->where('promoted', true) ->orderByPivot('hired_at', 'desc'); return $this->belongsToMany(Employee::class) ->using(RoleEmployees::class) ->withPivot([ 'user_id', 'role_id' ]); } }
class RoleEmployees { }
$employee = Employee::find(1); $employee->roles->forEach(function($role) { // }); $employee = Employee::find(1)->roles()->orderBy('name')->where('name', 'admin')->get();
Полиморфный Один к одному
tenants id – integer name – string landlords id – integer name – string waterbills id – integer amount – double waterbillable_id waterbillable_type
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; class WaterBill extends Model { public function billable() { return $this->morphTo(); } } class Tenant extends Model { public function waterBill() { return $this->morphOne(WaterBill::class, 'billable'); } } class Landlord extends Model { public function waterBill() { return $this->morphOne(WaterBill::class, 'billable'); } }
<?php $tenant = Tenant::find(1)->waterBill; $landlord = Landlord::find(1)->waterBill;
Полиморфный Один Ко Многим
posts id – integer title – string body – text videos id – integer title – string url – string polls id – integer title – string comments id – integer body – text commentable_id – integer commentable_type – string
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; class Comment extends Model { public function commentable() { return $this->morphTo(); } } class Poll extends Model { public function comments() { return $this->morphMany(Comment::class, 'commentable'); } } class Live extends Model { public function comments() { return $this->morphMany(Comments::class, 'commentable'); } }
<?php use App\Models\Live; use App\Models\Comment; $live = Live::find(1); foreach ($live->comments as $comment) { } // OR Live::find(1)->comments()->each(function($comment) { // }); Live::find(1)->comments()->map(function($comment) { // }); Live::find(1)->comments()->filter(function($comment) { // }); $comment = Comment::find(10); $commentable = $comment->commentable;
Полиморфный Один из Многих
Полиморфный Многие-К-Многим
videos id – integer description – string stories id – integer description – string taggables tag_id – integer taggable_id – integer taggable_type – string
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; class Video extends Model { public function tags() { return $this->morphToMany(Tag::class, 'taggable'); } }
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; class Tag extends Model { public function stories() { return $this->morphedByMany(Story::class, 'taggable'); } public function videos() { return $this->morphedByMany(Video::class, 'taggable'); } }
<?php use App\Model\Tag; $tag = Tag::find(10); $posts = $tag->stories; $videos = $tag->stories;