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/ */
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);
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;
}
}
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();