<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
vajexal / laravel-attributes-relationships example snippets
use Illuminate\Database\Eloquent\Model;
use Vajexal\AttributeRelations\Relations\HasOne;
#[HasOne(Phone::class)]
class User extends Model
{
}
// ...
User::first()->phone;
use Illuminate\Database\Eloquent\Model;
use Vajexal\AttributeRelations\Relations\BelongsTo;
#[BelongsTo(User::class)]
class Phone extends Model
{
}
// ...
Phone::first()->user;
use Illuminate\Database\Eloquent\Model;
use Vajexal\AttributeRelations\Relations\HasMany;
#[HasMany(Comment::class)]
class Post extends Model
{
}
// ...
Post::first()->comments;
use Illuminate\Database\Eloquent\Model;
use Vajexal\AttributeRelations\Relations\BelongsTo;
#[BelongsTo(Post::class)]
class Comment extends Model
{
}
// ...
Comment::first()->post;
use Illuminate\Database\Eloquent\Model;
use Vajexal\AttributeRelations\Relations\BelongsToMany;
#[BelongsToMany(Role::class)]
class User extends Model
{
}
// ...
User::first()->roles;
use Illuminate\Database\Eloquent\Model;
use Vajexal\AttributeRelations\Relations\BelongsToMany;
#[BelongsToMany(User::class)]
class Role extends Model
{
}
// ...
Role::first()->users;
use Illuminate\Database\Eloquent\Model;
use Vajexal\AttributeRelations\Relations\HasOneThrough;
#[HasOneThrough(Owner::class, Car::class)]
class Mechanic extends Model
{
}
// ...
// If you don't like method name, then you can define relation method
Mechanic::first()->owner;
use Illuminate\Database\Eloquent\Model;
use Vajexal\AttributeRelations\Relations\HasManyThrough;
#[HasManyThrough(Post::class, User::class)]
class Country extends Model
{
}
// ...
Country::first()->posts;
use Illuminate\Database\Eloquent\Model;
use Vajexal\AttributeRelations\Relations\MorphOne;
#[MorphOne(Image::class, 'imageable')]
class User extends Model
{
}
// ...
User::first()->image;
use Illuminate\Database\Eloquent\Model;
use Vajexal\AttributeRelations\Relations\MorphTo;
#[MorphTo('imageable')]
class Image extends Model
{
}
// ...
Image::first()->imageable;
use Illuminate\Database\Eloquent\Model;
use Vajexal\AttributeRelations\Relations\MorphMany;
#[MorphMany(Image::class, 'imageable')]
class Post extends Model
{
}
// ...
Post::first()->images;
use Illuminate\Database\Eloquent\Model;
use Vajexal\AttributeRelations\Relations\MorphTo;
#[MorphTo('imageable')]
class Image extends Model
{
}
// ...
Image::first()->imageable;
use Illuminate\Database\Eloquent\Model;
use Vajexal\AttributeRelations\Relations\MorphToMany;
#[MorphToMany(Tag::class, 'taggable')]
class Post extends Model
{
}
// ...
Post::first()->tags;
use Illuminate\Database\Eloquent\Model;
use Vajexal\AttributeRelations\Relations\MorphedByMany;
#[MorphedByMany(Post::class, 'taggable')]
class Tag extends Model
{
}
// ...
Tag::first()->posts;
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.