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;
}
}
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;
}
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',
];
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.