Различия
Показаны различия между двумя версиями страницы.
| Следующая версия | Предыдущая версия | ||
| php:laravel:relations [2024/10/10 01:32] – создано mirocow | php:laravel:relations [2024/10/10 02:37] (текущий) – mirocow | ||
|---|---|---|---|
| Строка 1: | Строка 1: | ||
| - | ====== Relations ====== | + | {{tag> |
| + | {{backlinks> | ||
| + | |||
| + | ====== Отношения / Relations ====== | ||
| + | |||
| + | * Отношения «один к одному» | ||
| + | * Отношение «один ко многим» | ||
| + | * Имеет отношение «один из многих» | ||
| + | * Отношения HasOneThrough и HasManyThrough | ||
| + | * Отношение «многие ко многим» | ||
| + | * Полиморфные отношения | ||
| + | * Полиморфный Один к одному | ||
| + | * Полиморфный Один Ко Многим | ||
| + | * Полиморфный Один из Многих | ||
| + | * Полиморфный Многие-К-Многим | ||
| + | |||
| + | ===== Ссылки ===== | ||
| + | |||
| + | * [[https:// | ||
| + | |||
| + | ===== Отношения «один к одному» ===== | ||
| + | |||
| + | {{: | ||
| + | |||
| + | <code bash> | ||
| + | $ php artisan make:model Tenant | ||
| + | $ Php artisan make:model Rent | ||
| + | </ | ||
| + | |||
| + | <code php> | ||
| + | <?php | ||
| + | |||
| + | namespace App\Models; | ||
| + | use Illuminate\Database\Eloquent\Model; | ||
| + | |||
| + | class Tenant extends Model | ||
| + | { | ||
| + | /** | ||
| + | * Get the rent of a Tenant | ||
| + | */ | ||
| + | public function rent() | ||
| + | { | ||
| + | return $this-> | ||
| + | //return $this- > | ||
| + | //return $this-> | ||
| + | } | ||
| + | } | ||
| + | |||
| + | $rent = Tenant:: | ||
| + | </ | ||
| + | |||
| + | ===== Отношение «один ко многим» ===== | ||
| + | |||
| + | {{: | ||
| + | |||
| + | <code php> | ||
| + | <?php | ||
| + | |||
| + | namespace App\Models; | ||
| + | use Illuminate\Database\Eloquent\Model; | ||
| + | |||
| + | class Tenant extends Model | ||
| + | { | ||
| + | /** | ||
| + | * Get the rents of a Tenant | ||
| + | */ | ||
| + | public function rent() | ||
| + | { | ||
| + | return $this-> | ||
| + | //return $this-> | ||
| + | //return $this-> | ||
| + | } | ||
| + | } | ||
| + | |||
| + | $rents = Tenant:: | ||
| + | </ | ||
| + | |||
| + | <code php> | ||
| + | <?php | ||
| + | |||
| + | namespace App\Models; | ||
| + | use Illuminate\Database\Eloquent\Model; | ||
| + | |||
| + | class Rent extends Model | ||
| + | { | ||
| + | /** | ||
| + | * Return the tenant for the rent | ||
| + | */ | ||
| + | public function tenant() | ||
| + | { | ||
| + | return $this-> | ||
| + | } | ||
| + | } | ||
| + | |||
| + | $tenant = Rent:: | ||
| + | </ | ||
| + | |||
| + | ===== Имеет отношение «один из многих» ===== | ||
| + | |||
| + | |||
| + | <code php> | ||
| + | public function latestRent() { | ||
| + | return $this-> | ||
| + | } | ||
| + | |||
| + | public function oldestRent() { | ||
| + | return $this-> | ||
| + | } | ||
| + | |||
| + | return $this-> | ||
| + | </ | ||
| + | |||
| + | ===== Отношения 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 | ||
| + | </ | ||
| + | | ||
| + | <code php> | ||
| + | <?php | ||
| + | |||
| + | namespace App\Models; | ||
| + | use Illuminate\Database\Eloquent\Model; | ||
| + | |||
| + | class Rent extends Model | ||
| + | { | ||
| + | /** | ||
| + | * Return the rents' landlord | ||
| + | */ | ||
| + | public function rentLandlord() | ||
| + | { | ||
| + | return $this-> | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | <code php> | ||
| + | <?php | ||
| + | |||
| + | namespace App\Models; | ||
| + | use Illuminate\Database\Eloquent\Model; | ||
| + | |||
| + | class Tenant extends Model | ||
| + | { | ||
| + | /** | ||
| + | * Get the rents of a Tenant | ||
| + | */ | ||
| + | public function rent() | ||
| + | { | ||
| + | return $this-> | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | <code php> | ||
| + | |||
| + | </ | ||
| + | |||
| + | ===== Аналогично, | ||
| + | |||
| + | {{: | ||
| + | |||
| + | < | ||
| + | country | ||
| + | id - integer | ||
| + | name - string | ||
| + | |||
| + | user | ||
| + | id - integer | ||
| + | country_id - integer | ||
| + | name - string | ||
| + | |||
| + | games | ||
| + | id - integer | ||
| + | user_id - integer | ||
| + | title - string | ||
| + | |||
| + | </ | ||
| + | |||
| + | <code php> | ||
| + | <?php | ||
| + | |||
| + | namespace App\Models; | ||
| + | use Illuminate\Database\Eloquent\Model; | ||
| + | |||
| + | class Country extends Model | ||
| + | { | ||
| + | protected $fillable = [' | ||
| + | |||
| + | public function users() | ||
| + | { | ||
| + | return $this-> | ||
| + | } | ||
| + | |||
| + | public function games() | ||
| + | { | ||
| + | return $this-> | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | <code php> | ||
| + | <?php | ||
| + | |||
| + | namespace App\Models; | ||
| + | use Illuminate\Database\Eloquent\Model; | ||
| + | |||
| + | class User extends Model | ||
| + | { | ||
| + | protected $fillable = [article_id, | ||
| + | |||
| + | public function country() | ||
| + | { | ||
| + | return $this-> | ||
| + | } | ||
| + | |||
| + | public function posts() | ||
| + | { | ||
| + | return $this-> | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | <code php> | ||
| + | <?php | ||
| + | |||
| + | namespace App\Models; | ||
| + | use Illuminate\Database\Eloquent\Model; | ||
| + | |||
| + | class Game extends Model | ||
| + | { | ||
| + | protected $fillable = [' | ||
| + | |||
| + | public function user() | ||
| + | { | ||
| + | return $this-> | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | <code php> | ||
| + | <?php | ||
| + | |||
| + | $country = Country:: | ||
| + | | ||
| + | // Retrieve all games for the country | ||
| + | $games = $country-> | ||
| + | </ | ||
| + | |||
| + | ===== Отношение «многие ко многим» ===== | ||
| + | |||
| + | {{: | ||
| + | |||
| + | < | ||
| + | employees | ||
| + | id - integer | ||
| + | name - string | ||
| + | |||
| + | roles | ||
| + | id - integer | ||
| + | name - string | ||
| + | |||
| + | role_employees | ||
| + | user_id - integer | ||
| + | role_id - integer | ||
| + | </ | ||
| + | |||
| + | <code php> | ||
| + | <?php | ||
| + | |||
| + | namespace App\Models; | ||
| + | use Illuminate\Database\Eloquent\Model; | ||
| + | |||
| + | class Employee extends Model | ||
| + | { | ||
| + | public function roles() | ||
| + | { | ||
| + | return $this- > | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | <code php> | ||
| + | <?php | ||
| + | |||
| + | namespace App\Models; | ||
| + | use Illuminate\Database\Eloquent\Model; | ||
| + | |||
| + | class Role extends Model | ||
| + | { | ||
| + | public function employees() | ||
| + | { | ||
| + | return $this-> | ||
| + | return $this-> | ||
| + | -> | ||
| + | return $this-> | ||
| + | -> | ||
| + | return $this-> | ||
| + | -> | ||
| + | -> | ||
| + | return $this-> | ||
| + | -> | ||
| + | return $this-> | ||
| + | -> | ||
| + | return $this-> | ||
| + | -> | ||
| + | return $this-> | ||
| + | -> | ||
| + | return $this-> | ||
| + | -> | ||
| + | return $this-> | ||
| + | -> | ||
| + | return $this-> | ||
| + | -> | ||
| + | -> | ||
| + | return $this-> | ||
| + | -> | ||
| + | -> | ||
| + | ' | ||
| + | ' | ||
| + | ]); | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | <code php> | ||
| + | class RoleEmployees | ||
| + | { | ||
| + | |||
| + | } | ||
| + | </ | ||
| + | |||
| + | <code php> | ||
| + | $employee = Employee:: | ||
| + | $employee-> | ||
| + | $employee = Employee:: | ||
| + | </ | ||
| + | |||
| + | ===== Полиморфный Один к одному ===== | ||
| + | |||
| + | {{: | ||
| + | |||
| + | < | ||
| + | tenants | ||
| + | id – integer | ||
| + | name – string | ||
| + | |||
| + | landlords | ||
| + | id – integer | ||
| + | name – string | ||
| + | |||
| + | waterbills | ||
| + | id – integer | ||
| + | amount – double | ||
| + | waterbillable_id | ||
| + | waterbillable_type | ||
| + | </ | ||
| + | | ||
| + | <code php> | ||
| + | <?php | ||
| + | |||
| + | namespace App\Models; | ||
| + | use Illuminate\Database\Eloquent\Model; | ||
| + | |||
| + | class WaterBill extends Model | ||
| + | { | ||
| + | public function billable() | ||
| + | { | ||
| + | return $this-> | ||
| + | } | ||
| + | } | ||
| + | |||
| + | class Tenant extends Model | ||
| + | { | ||
| + | public function waterBill() | ||
| + | { | ||
| + | return $this-> | ||
| + | } | ||
| + | } | ||
| + | |||
| + | class Landlord extends Model | ||
| + | { | ||
| + | public function waterBill() | ||
| + | { | ||
| + | return $this-> | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | <code php> | ||
| + | <?php | ||
| + | |||
| + | $tenant = Tenant:: | ||
| + | $landlord = Landlord:: | ||
| + | </ | ||
| + | |||
| + | ===== Полиморфный Один Ко Многим ===== | ||
| + | |||
| + | {{: | ||
| + | |||
| + | < | ||
| + | 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 | ||
| + | </ | ||
| + | | ||
| + | <code php> | ||
| + | <?php | ||
| + | |||
| + | namespace App\Models; | ||
| + | use Illuminate\Database\Eloquent\Model; | ||
| + | |||
| + | class Comment extends Model | ||
| + | { | ||
| + | public function commentable() | ||
| + | { | ||
| + | return $this-> | ||
| + | } | ||
| + | } | ||
| + | |||
| + | class Poll extends Model | ||
| + | { | ||
| + | public function comments() | ||
| + | { | ||
| + | return $this-> | ||
| + | } | ||
| + | } | ||
| + | |||
| + | class Live extends Model | ||
| + | { | ||
| + | public function comments() | ||
| + | { | ||
| + | return $this-> | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | <code php> | ||
| + | <?php | ||
| + | |||
| + | use App\Models\Live; | ||
| + | use App\Models\Comment; | ||
| + | |||
| + | $live = Live:: | ||
| + | |||
| + | foreach ($live-> | ||
| + | |||
| + | // OR | ||
| + | |||
| + | Live:: | ||
| + | Live:: | ||
| + | Live:: | ||
| + | |||
| + | $comment = Comment:: | ||
| + | $commentable = $comment-> | ||
| + | </ | ||
| + | |||
| + | ===== Полиморфный Один из Многих ===== | ||
| + | |||
| + | |||
| + | |||
| + | ===== Полиморфный Многие-К-Многим ===== | ||
| + | |||
| + | {{: | ||
| + | |||
| + | < | ||
| + | videos | ||
| + | id – integer | ||
| + | description – string | ||
| + | |||
| + | stories | ||
| + | id – integer | ||
| + | description – string | ||
| + | |||
| + | taggables | ||
| + | tag_id – integer | ||
| + | taggable_id – integer | ||
| + | taggable_type – string | ||
| + | </ | ||
| + | |||
| + | <code php> | ||
| + | <?php | ||
| + | |||
| + | namespace App\Models; | ||
| + | use Illuminate\Database\Eloquent\Model; | ||
| + | |||
| + | class Video extends Model | ||
| + | { | ||
| + | public function tags() | ||
| + | { | ||
| + | return $this-> | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | <code php> | ||
| + | <?php | ||
| + | |||
| + | namespace App\Models; | ||
| + | use Illuminate\Database\Eloquent\Model; | ||
| + | |||
| + | class Tag extends Model | ||
| + | { | ||
| + | public function stories() | ||
| + | { | ||
| + | return $this-> | ||
| + | } | ||
| + | |||
| + | public function videos() | ||
| + | { | ||
| + | return $this-> | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | <code php> | ||
| + | <?php | ||
| + | use App\Model\Tag; | ||
| + | |||
| + | $tag = Tag:: | ||
| + | $posts = $tag-> | ||
| + | $videos = $tag-> | ||
| + | </ | ||