ponich/eloquent-traits
Composer 安装命令:
composer require ponich/eloquent-traits
包简介
Traits for laravel eloquent models
README 文档
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') );
统计信息
- 总下载量: 85
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 18
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-05-22