ponich / eloquent-traits
Traits for laravel eloquent models
1.0.3
2018-07-12 17:24 UTC
Requires
- php: >=7.1
- illuminate/support: 5.5.*|5.6.*
Requires (Dev)
- laravel/framework: 5.5.*|5.6.*
- mockery/mockery: ~1.0
- orchestra/testbench: ~3.3.0|~3.4.2|^3.5.0
- phpunit/phpunit: ~7.0
Suggests
- ext-pdo_sqlite: For SQLite database for tests
This package is not auto-updated.
Last update: 2025-03-29 01:59:51 UTC
README
This package adds the ability to use traits in you Laravel Eloquent Models
Installation
This package can be used in Laravel 5.5 or higher.
composer require ponich/eloquent-traits
You can publish the migration with:
php artisan vendor:publish --provider="Ponich\Eloquent\Traits\ServiceProvider" --tag="migrations"
After the migration has been published you can create tables by running the migrations:
php artisan migrate
Traits
Virtual Attributes
Adds the ability to create virtual attributes in your model.
Use trait: \Ponich\Eloquent\Traits\VirtualAttribute
Example:
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; class Post extends Model { use \Ponich\Eloquent\Traits\VirtualAttribute; protected $table = 'posts'; protected $guarded = ['id']; public $virtalAttributes = ['tags', 'og_tags']; }
In the property of the class
$virtalAttributes
list all valid virtual attributes.
$post = Post::firstOrFail(1); $post->tags = ['tag1', 'tag2', 'tag3']; $post->save(); $post->refresh(); var_dump($post->tags); /** array(3) { [0]=> string(4) "tag1" [1]=> string(4) "tag2" [2]=> string(4) "tag3" } */
Attachments
Allows links files to models
Use trait: \Ponich\Eloquent\Traits\HasAttachment
Example:
Model:
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; class Post extends Model { use \Ponich\Eloquent\Traits\HasAttachment; protected $table = 'posts'; protected $guarded = ['id']; }
Add attachment :
$post = Post::findOrFail(1); // by path $post->attach('/path/to/file'); // by request $post->attach( $request->file('photo') );