PHP code example of vajexal / laravel-attributes-relationships

1. Go to this page and download the library: Download vajexal/laravel-attributes-relationships library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?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;