PHP code example of dgoring / laravel-inherit-resource

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

    

dgoring / laravel-inherit-resource example snippets


namespace App\Http\Controllers;

use Dgoring\Laravel\InheritResource\Resource;

class UsersController extends Controller
{
  use Resource;
}

Route::resource('users', 'UsersController');

class UsersController extends Controller
{
  use Resource { collection as query; }


  protected function collection()
  {
    $query = $this->query();

    if($search = request()->query('search'))
    {
      $query->where('name', 'like', "%{$search}%");
    }

    return $query;
  }
}

  protected function collection()
  {
    $query = $this->query();

    $query->where('active', 1);

    return $query;
  }

class UsersController extends Controller
{
  use Resource;

  protected function validationRules()
  {
    if($this->resource()->exists)
    {
      return [
        'name' => 'string',
      ];
    }
    else
    {
      return [
        'name' => 'string|

Route::resource('teams.users', 'UsersController');

use App\Team;

class UsersController extends Controller
{
  use Resource { collection as query; }


  protected function collection()
  {
    $team = Team::findOrFail(request()->route('team'));

    $query = $team->users();

    if($search = request()->query('search'))
    {
      $query->where('name', 'like', "%{$search}%");
    }

    return $query;
  }
}

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class UserResource extends JsonResource
{

}

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\ResourceCollection;

class UsersCollection extends ResourceCollection
{

}

namespace App\Http\Controllers;

use Dgoring\Laravel\InheritResource\JsonResource;

class UsersController extends Controller
{
  use JsonResource;
}

namespace App\Http\Controllers;

use Dgoring\Laravel\InheritResource\HtmlResource;

class UsersController extends Controller
{
  use HtmlResource;
}



return [
  'namespace' => 'App\\Models\\',

  'json_resources' => 'App\\Http\\Resources\\',
  'json_collections' => 'App\\Http\\Collections\\',
];


class UsersController extends Controller
{
  use Resource;

  public function __construct()
  {
    $this->class_name = CustomUser::class; // Model Class
    $this->instance_name = 'custom_user';  // Route parameter and name of variable passed to single record views
    $this->collection_name = 'customer_users'; // Name of variable passed to index view

    $this->authorize = false; // Switch to turn off authorize checks (default is on)

    $this->saveTransaction = true; // while enabled model save and delete function are performed within a database transaction
    $this->distinctFix = true; // if the query builder returns a distinct query, a fix will be applied to get the distinct count used for pagination
    $this->fillOnlyValidated = true; // while enabled only the validated fields will be allowed to be mass assigned to the model

    //Only for Resource and HtmlResource

    $this->view_ns = 'customer_users'; // dot notation path to views folder

    $this->views = [ // name of view used for each function
      'index'  => 'index',
      'create' => 'form',
      'show'   => 'show',
      'edit'   => 'form',
    ];


  }
}