PHP code example of safiullahsarhandi / laravel-repository

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

    

safiullahsarhandi / laravel-repository example snippets

 
 
//config/repository.php
return [
      /* 
      * repositories will contain all the repositories namespaces from app/Repositories directory  
      * and bind Contract and model associated with it  
      *
      */
      'repositories' => [],
  ];

      
      
      namespace App\Filters\Api;
      use LaravelRepository\Abstracts\Filters;

      class UserFilter extends Filters {

          protected $filters = ['search'];

          public function search($value)
          {
              $this->builder->where("status", $value);
          }
      }
      

      Route::get('/account', function (UserRepositoryContract $user,UserFilter $filter) {
          // extends request after it has been initialized
          $filter->extendRequest([
              'user_id' => auth()->id(),
          ]);
          $data = $user->findOne($filter);
          // under the hood it work like this
          // User::where('user_id',1)->first();
      });
      
      

      
      
      namespace App\Filters\Api;
      use LaravelRepository\Abstracts\Filters;

      class UserFilter extends Filters {

          protected $filters = ['search','user_id'];

          public function search($value)
          {
              $this->builder->where("status", $value);
          }
          public function user_id($value)
          {
              $this->builder->where("id", $value);
          }
      }
      
      

      
      namespace App\Events\User;

      use Illuminate\Database\Eloquent\Model;
      use LaravelRepository\Contracts\EventContract;

      class UserRepositoryEvent implements EventContract {

          public function beforeCreate($repository,array $params = []){}

          public function created(Model $model){}

          public function beforeUpdate($repository,array $params = []){}

          public function updated(Model $model){}

          public function beforeDelete($repository,mixed $param){}

          public function deleted(mixed $param){}

          public function beforeFetch($repository,mixed $params = null){}

          public function fetched(null|Model $value = null){}
      }
      
      

      
      namespace App\Events\Order;

      use Illuminate\Database\Eloquent\Model;
      use LaravelRepository\Contracts\EventContract;

      class OrderRepositoryEvent implements EventContract {
    
          private $params;
          
          public function beforeCreate($repository,array $params = [])
          {
               //you don't need to do this every time 
              // and we don't have another way to access params in created
              $this->params = $params;
          }


          public function created(Model $model)
          {
              // store products after order created 
              $products = $this->params['products'];
              // storing laravel belongsToMany order_products
              $model->products()->attach($products);      
              
              // process merchant payment  
              $model->pay();
                
          }
      }