PHP code example of rateb / structure

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

    

rateb / structure example snippets


   \RatebSa\Structure\StructureServiceProvider::class,
   

'providers' => [
    /*
     * Laravel Framework Service Providers...
     */
    Illuminate\Auth\AuthServiceProvider::class,
    Illuminate\Broadcasting\BroadcastServiceProvider::class,
    Illuminate\Routing\RouteServiceProvider::class,
    // ... other Laravel service providers

    /*
     * Application Service Providers...
     */
    App\Providers\AppServiceProvider::class,
    App\Providers\AuthServiceProvider::class,
    App\Providers\EventServiceProvider::class,
    App\Providers\RouteServiceProvider::class,
    
    // Add the StructureServiceProvider
    \RatebSa\Structure\StructureServiceProvider::class,
],

use App\Http\Controllers\UserController;

Route::post('create', [UserController::class, 'create']);
Route::post('index', [UserController::class, 'index']);



namespace App\Http\Controllers;

use App\Http\Repositories\UserRepo;
use Illuminate\Http\Request;

class UserController extends Controller
{
    // Constructor to inject the UserRepo dependency
    public function __construct(protected UserRepo $userRepository)
    {
    }

    // Method to retrieve a filtered and searchable list of users
    public function index(Request $request)
    {
        return $this->userRepository->index()->get();
    }

    // Method to handle the creation of a new user
    public function create(Request $request)
    {
        return $this->userRepository->store($request);
    }
}




namespace App\Http\Repositories;

use App\Filters\User\StatusUserFilter;
use App\Http\DTOs\UserData;
use App\Models\User;
use RatebSa\Structure\Repositories\BaseRepo;

class UserRepo extends BaseRepo
{
    protected $filtersKeys = [
        'status' => StatusUserFilter::class,
    ];

    protected $searchFileds = ['email'];

    protected $relations = [];

    protected $realationFileds = [
        'profile' => ['first_name']
    ];

    public function __construct(User $model)
    {
        parent::__construct($model, UserData::class);
    }
}


      // class UserRepo
      // Array with filterable fields
      protected $filtersKeys = [
          'status'=>StatusUserFilter::class,
      ];


      // class UserRepo
      // Array containing fields that can be searched within
      protected $searchFileds = ['field'];


      // class UserRepo
       // An Array containing fields that exist within searchable relationships
     protected $realationFileds = [
     'relation1'=>[
           'field1' ,
           'field2'  
 
        ],
        'relation2'=>[
           'field3' ,
           'field4' 
        ],
];


namespace App\Http\DTOs;

use Illuminate\Http\Request;
use RatebSa\Structure\DTOs\BaseDTO;

class UserData extends BaseDTO
{
    public static function fromRequest(?Request $request, ...$params): static
    {
        $instance = parent::fromRequest($request, ...$params);

        $instance->name = $request->input('name');
        $instance->email = $request->input('email');
        $instance->password = $request->input('password');
        $instance->status = $request->input('status');

        return $instance;
    }
}


namespace App\Filters\User;

use RatebSa\Structure\Filters\Filter;

class StatusUserFilter extends Filter
{
    public static function rules(): array
    {
        return [
            'status' => []
        ];
    }

    public function apply(&$query)
    {
        return $query->where('status', $this->status);
    }
}

bash
php artisan make:repo-dto User --action=repo
bash
php artisan make:repo-dto User --action=dto
bash
php artisan make:repo-dto User --action=all
//or
php artisan make:repo-dto User