PHP code example of osi / laravel-controller-trait

1. Go to this page and download the library: Download osi/laravel-controller-trait 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/ */

    

osi / laravel-controller-trait example snippets


php artisan trait:controller
php artisan trait:model


#【2021-06-11】新增对withCount的支持

?expandCount: ["user",{"role.code":"admin"}]

需要在model类加入以下支持
public function extraFields()
{
    return ['user', 'role'];
}



use Osi\LaravelControllerTrait\Traits\ControllerBaseTrait; // trait
use App\Admin; //model file
class AdminsController extends Controller
{
    use ControllerBaseTrait;

    public function __construct(Admin $model)
    {
        $this->model = $model;
        $this->resource = '\Osi\LaravelControllerTrait\Resources\Resource';
        $this->collection = '\Osi\LaravelControllerTrait\Resources\Collection';
        $this->functions = get_class_methods(self::class);
    }
}

Route::resources(['admins' => 'AdminsController']);
#以上完成,即提供了常规的增删改查方法

#【1.10】新增批量更新
post:api/admins/batch
request()->all(): [
	['id'=>1,'field'=>'xxx','field2'=>xxx],
	['id'=>2,'field'=>'x2x','field2'=>x2x]
]

#【1.11】剥离基础返回类

use Osi\LaravelControllerTrait\Traits\ResponseBaseTrait; // trait 附带以下方法

dataSuccess
created
accepted
noContent
badRequest
unauthorized
forbidden
unprocesableEtity
success