coyote6 / laravel-base
提供了一些用于Coyote6 GraphX开发项目的通用基础类。为使用它作为主键的模型提供UUID特性。提供特性方法以返回模型作为选项。
Requires
- webpatser/laravel-uuid: ^4.0
README
这只是一个常用的特性集合,这些特性在我的网站和包中广泛使用,例如UUID和模型上自动填充author_id字段。
使用方法
- 选择您想要实现的特性。
- 将该特性添加到模型中。
可用特性
启动方法
BootTraits - 用于在创建、创建后、更新、更新后、删除和删除方法上调用启动方法。在实现其他方法之前调用父方法。还添加了static::getUser()、static::getUserId()方法以在其他特性中查找当前用户。
需要启动方法的创建助手
HasAuthor - 使用当前用户的id
自动填充模型上的author_id
属性。HasClient - 如果用户有一个client_id
,则使用当前用户的client_id
自动填充模型上的client_id
属性。HasMachineName - 使用name
属性自动填充模型上的machine_name
属性,移除空格和所有标点符号。HasUuid - 使用uuid自动填充模型上的id
属性。
选择下拉/单选按钮助手
GetAsOptions - 使用模型属性id
和name
创建用于选择下拉和单选按钮的选项数组。GetAsOptionsAbbr - 使用模型属性abbr
和name
创建用于选择下拉和单选按钮的选项数组。(与GetAsOptions相同,但使用缩写字段而不是名称。主要用于州和国家。)
查询助手
GetBySlug - 通过唯一的slug
属性选择模型。
示例
Uuid示例
App\Models\Example.php
<?php namespace App\Models; use Coyote6\LaravelBase\Traits\BootTraits; use Coyote6\LaravelBase\Traits\HasUuid; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class Example extends Model { use HasFactory, HasUuid, BootTraits; public $incrementing = false; protected $keyType = 'string'; protected $fillable = [ 'id', 'name', ]; }
此示例仅自动在创建时添加uuid作为id
,如果未填充。
所有Boot Traits示例
App\Models\Example.php
<?php namespace App\Models; use Coyote6\LaravelBase\Traits\BootTraits; use Coyote6\LaravelBase\Traits\HasAuthor; use Coyote6\LaravelBase\Traits\HasClient; use Coyote6\LaravelBase\Traits\HasMachineName; use Coyote6\LaravelBase\Traits\HasUuid; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class Example extends Model { use HasFactory, HasUuid, HasAuthor, HasClient BootTraits; public $incrementing = false; protected $keyType = 'string'; protected $fillable = [ 'id', 'machine_name', 'name', 'client_id', 'user_id' ]; }
此示例自动在创建时添加uuid作为id
,用户的id和client id分别作为author_id
和client_id
,以及基于name
的机器name_based
,如果未填充。
Get As Options示例
App\Models\Example.php
<?php namespace App\Models; use Coyote6\LaravelBase\Traits\GetAsOptions; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class Example extends Model { use HasFactory, GetAsOptions; public $incrementing = false; protected $keyType = 'string'; protected $fillable = [ 'id', 'name', ]; }
App\Http\Controllers\ExampleController.php
<?php namespace App\Http\Controllers; use App\Http\Controllers\Controller; use Illuminate\Http\Request; use App\Models\Example; class ExampleController extends Controller { public function index () { dd (Example::getAsOptions()); } }
Get As Options Abbreviation示例
App\Models\Example.php
<?php namespace App\Models; use Coyote6\LaravelBase\Traits\GetAsOptions; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class Example extends Model { use HasFactory, GetAsOptionsAbbr; public $incrementing = false; protected $keyType = 'string'; protected $fillable = [ 'id', 'abbr', 'name', ]; }
App\Http\Controllers\ExampleController.php
<?php namespace App\Http\Controllers; use App\Http\Controllers\Controller; use Illuminate\Http\Request; use App\Models\Example; class ExampleController extends Controller { public function index () { dd (Example::getAsOptionsAbbr()); } }
Get By Slug示例
App\Models\Example.php
<?php namespace App\Models; use Coyote6\LaravelBase\Traits\GetBySlug; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class Example extends Model { use HasFactory, GetBySlug; public $incrementing = false; protected $keyType = 'string'; protected $fillable = [ 'id', 'slug', 'name', ]; }
App\Http\Controllers\ExampleController.php
<?php namespace App\Http\Controllers; use App\Http\Controllers\Controller; use Illuminate\Http\Request; use App\Models\Example; class ExampleController extends Controller { public function index (Request $request) { $slug = $request->get('slug'); dd (Example::getBySlug ($slug)); } }