PHP code example of gpapakitsos / laravel-traits

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

    

gpapakitsos / laravel-traits example snippets


'providers' => [
    // ...
    GPapakitsos\LaravelTraits\TraitsServiceProvider::class,
];

/**
 * Checks if model’s state is active
 *
 * @return bool
 */
$model->isActive();

/**
 * Returns the title of model’s state
 *
 * @return string
 */
$model->getActiveTitle();

/**
 * Scope a query to only tabase\Eloquent\Builder  $query
 * @return void
 */
$model->active()->get();

/**
 * Scope a query to only tabase\Eloquent\Builder  $query
 * @return void
 */
$model->notActive()->get();

/**
 * Returns next available ordering value
 *
 * @param  array  $fieldsAndValues
 * @return int
 *
 * @throws ErrorException
 */
$model::getNewOrdering();
// or
$model::getNewOrdering(['category' => 1]);

/**
 * Resets ordering
 *
 * @param  array  $fieldsAndValues
 * @return void
 *
 * @throws ErrorException
 */
$model::resetOrdering();
// or
$model::resetOrdering(['category' => 1]);



namespace App\Models;

use GPapakitsos\LaravelTraits\ModelFile;
use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    use ModelFile;

    const FILE_MODEL_ATTRIBUTE = 'avatar'; // The model’s attribute name
    const FILE_INPUT_FIELD = 'avatar_input'; // The form’s input field name
    const FILE_FOLDER = 'avatars'; // The folder name of the storage disk
    const FILE_STORAGE_DISK = 'local'; // The storage disk
    const FILE_DEFAULT_ASSET_URL = 'avatars/default.png'; // The default asset if file does not exist
}

/**
 * Stores file if exists & adds the path of the uploaded file into request object
 *
 * @param  \Illuminate\Http\Request  $request
 * @return void
 *
 * @throws ErrorException|\Illuminate\Validation\ValidationException
 */
$user::storeFile($request);

/**
 * Deletes model’s file if exists
 *
 * @return void
 */
$user->deleteFile();

/**
 * Removes the previous file if exists & stores the new one
 *
 * @param  \Illuminate\Http\Request  $request
 * @return void
 *
 * @throws ErrorException|\Illuminate\Validation\ValidationException
 */
$user::changeFile($request);

/**
 * Returns file’s URL
 *
 * @return string|null
 */
$user->getFileURL();

/**
 * Returns file’s path
 *
 * @return string|null
 */
$user->getFilePath();



namespace App\Http\Controllers;

use App\Models\User;
use GPapakitsos\LaravelTraits\CRUDController;
use Illuminate\Http\Request;

class UsersController extends Controller
{
    use CRUDController;

    protected $request;
    protected $model;

    // You can set the property $returnModelsFromCRUD as `true` if you would like the methods `doAdd` & `doEdit` to return the model
    // protected $returnModelsFromCRUD = true;

    public function __construct(Request $request, User $model)
    {
        $this->request = $request;
        $this->model = $model;
    }
}

Route::prefix('users')->group(function () {
    Route::get('get-resource/{id?}', 'UsersController@getResource');
    Route::post('add', 'UsersController@doAdd');
    Route::post('edit', 'UsersController@doEdit');
    Route::post('delete', 'UsersController@doDelete');
});



namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    /**
     * The attributes that are mass assignable
     *
     * @var array
     */
    protected $fillable = ['name', 'email', 'password'];

    /**
     * Validation rules of the model
     *
     * @var array
     */
    public $validations = [
        'add' => [
            'name' => 'he same between `add` & `edit`
        // 'all' => [
        //     'name' => '

/**
 * Returns model’s JSON response by provided id
 *
 * @return \Illuminate\Http\JsonResponse
 *
 * @throws ErrorException|\Illuminate\Database\Eloquent\ModelNotFoundException
 */
getResource();

/**
 * Creates a new model if the request is valid
 *
 * @return \Illuminate\Http\JsonResponse|\Illuminate\Database\Eloquent\Model
 *
 * @throws ErrorException|\Illuminate\Validation\ValidationException
 */
doAdd();

/**
 * Updates the model if the request is valid
 *
 * @return \Illuminate\Http\JsonResponse|\Illuminate\Database\Eloquent\Model
 *
 * @throws ErrorException|\Illuminate\Validation\ValidationException|\Illuminate\Database\Eloquent\ModelNotFoundException
 */
doEdit();

/**
 * Deletes a model
 *
 * @return \Illuminate\Http\JsonResponse
 *
 * @throws ErrorException|\Illuminate\Database\Eloquent\ModelNotFoundException
 */
doDelete();
shell
php artisan vendor:publish --provider="GPapakitsos\LaravelTraits\TraitsServiceProvider"