PHP code example of ravuthz / laravel-crud

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

    

ravuthz / laravel-crud example snippets


// PostController.php


namespace App\Http\Controllers\Api;

use App\Models\Post;
use App\Http\Requests\PostRequest;
use App\Http\Resources\PostResource;
use App\Http\Resources\PostCollection;
use Ravuthz\LaravelCrud\CrudController;

class PostController extends CrudController
{
    protected $model = Post::class;
    protected $resource = PostResource::class;
    // protected $collection = PostCollection::class;
    protected $storeRequest = PostRequest::class;
    protected $updateRequest = PostRequest::class;

    // Override this method to add custom logic before saving
    protected function beforeSave($request, $model, $id = null)
    {
        return $model;
    }

    // Override this method to add custom logic after saving
    protected function afterSave($request, $model, $id = null)
    {
        return $model;
    }

}

// PostControllerTest.php


namespace Tests\Feature\Http\Controllers\Api;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Laravel\Passport\Passport;
use Ravuthz\LaravelCrud\TestCrud;

class PostControllerTest extends TestCrud
{
    use RefreshDatabase;

    protected string $route = 'api/posts';

    protected function setUp(): void
    {
        parent::setUp();
        Passport::actingAs(User::factory()->create());

        Post::create([
            'name' => $this->faker->name(),
            'desc' => $this->faker->sentence(),
        ]);
    }

    protected function requestPayload($id = null): array
    {
        // $time = now()->format('Y-m-d_H:m:s.u');
        // some related data, attachment with some unique value with time here

        return [
            'id' => $id
            // 'name' => $this->faker->name(),
            // 'desc' => $this->faker->sentence(),
        ];
    }
}

bash
php artisan crud --help

# Generate crud model, controller, test, request, resource
php artisan crud:generate Post --test

# Generate only crud controller
php artisan crud:controller Post

# Generate only crud controller test
php artisan crud:controller-test Post