PHP code example of sarkis-sh / laravel-spray

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

    

sarkis-sh / laravel-spray example snippets




namespace App\Http\Controllers;

use App\Http\Requests\UserRequest;
use App\Http\Resources\UserResource;
use App\Services\UserService;
use App\Models\User;

class UserController extends GenericController
{
    public function __construct(private UserService $userService)
    {
        parent::__construct(new UserRequest(), new UserResource([]), new UserService(new User()));
    }
}



namespace App\Services;

class UserService extends GenericService
{
}



namespace App\Models;


use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;

class User extends GenericModel
{
    use HasApiTokens, HasFactory, Notifiable;

    protected $table = 'users';

    protected $fillable = [
        'name',
        'age',
        'email',
        'password',
    ];

    /**
     * Get all posts for the User
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function posts(): HasMany
    {
        return $this->hasMany(Post::class, 'user_id', 'id');
    }
}





namespace App\Http\Requests;

class UserRequest extends GenericRequest
{
    /**
     * Dynamically Get the the validation rules based on the request's action method.
     *
     * @return array
     */
    public function rules()
    {
        $method = request()->route()->getActionMethod();
        return $this->{$method . 'Validator'}();
    }

    /**
     * Validation rules for the 'store' action.
     *
     * @return array
     */
    private function storeValidator()
    {
        return [
            'name'      =>  'ist.*.email'     =>  'ion.
     *
     * @return array
     */
    private function bulkDeleteValidator()
    {
        return [
            'ids'    =>  '



namespace App\Http\Resources;

class UserResource extends GenericResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
     */
    public function toArray($request)
    {
        return [
            'id'          =>  $this->id,
            'name'        =>  $this->name,
            'age'         =>  $this->age,
            'email'       =>  $this->email,
            'password'    =>  $this->password,
            'created_at'  =>  $this->created_at,
        ];
    }
}





namespace Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;

class UserFactory extends Factory
{
    /**
     * Define the model's default state.
     *
     * @return array
     */
    public function definition()
    {
        return [
            'name'   =>  $this->faker->name(),
            'age'    =>  $this->faker->numberBetween(0, 2147483647),
            'email'  =>  $this->faker->unique()->safeEmail(),
        ];  
    }
}

Route::group([
    'prefix' => '/users',
    'controller' => UserController::class,
    // 'middleware' => ''
], function () {
    Route::delete('/', 'bulkDelete');
    Route::delete('/{id}', 'delete');
    Route::put('/{id}', 'update');
    Route::post('/bulk', 'bulkStore');
    Route::post('/', 'store');
    Route::get('/{id}', 'findById');
    Route::get('/', 'getAll');
});