elnooronline / laravel-concerns
The components that make up the Elnooronline's Laravel Projects
Installs: 1 091
Dependents: 1
Suggesters: 0
Security: 0
Stars: 2
Watchers: 3
Forks: 0
Open Issues: 0
Type:package
Requires
- elnooronline/laravel-bootstrap-forms: ^2.0
- elnooronline/laravel-locales: ^1.0
- ezyang/htmlpurifier: ^4.10
- laracasts/flash: ^3.0
- laracasts/presenter: ^0.2.1
- laravel/framework: ~5.6|^6.0|^7.0
- spatie/laravel-medialibrary: ^7.12
Requires (Dev)
- mockery/mockery: ^1.2
- orchestra/testbench: ~3.0
README
The components that make up the Elnooronline's Laravel Projects.
Install
composer require elnooronline/laravel-concerns
Configuration
- Import
\Elnooronline\LaravelConcerns\Http\Controllers\Helpers
trait in your base controller class
<?php namespace App\Http\Controllers; use Elnooronline\LaravelConcerns\Http\Controllers\Helpers; ... class Controller extends BaseController { use Helpers; ... }
This feature enables to use flash()
and getResourceName()
methods.
flash()
: This method will set flash session with localed message.- example :
public function store(Request $request) { // Handle store $this->>flash('created'); // returns : trans('%RESOURCE_NAME%.messages.created'). return view('home'); }
getResourceName()
: This method returns the resource name of the specified crud used inController
,Request
,Model
.- example : If you have
UserController
class will returnsusers
. and you can add custom resource name by adding$resourceName
property.
class UserController extends Controller { public function index() { return $this->getResourceName(); // returns : users } } class UserController extends Controller { /** * The controller resource name. * * @return string */ protected $resourceName = 'persons'; public function index() { return $this->getResourceName(); // returns : persons } }
- example : If you have
- All
Request
classes should extends\Elnooronline\LaravelConcerns\Http\RequestsFormRequest
class.
API :
getResourceName()
parseLocale()
- example :
insted of/** * Get the validation rules that apply to the request. * * @return array */ public function rules() { return $this->parseLocale([ 'name:{default}' => 'required|string|max:255', 'name:{lang}' => 'nullable|string|max:255', ]); }
/** * Get the validation rules that apply to the request. * * @return array */ public function rules() { // The default locale is "en" in this example return [ 'name:en' => 'required|string|max:255', 'name:ar' => 'nullable|string|max:255', 'name:fr' => 'nullable|string|max:255', ]; }
getAproperRules()
- example :
/** * Get the validation rules that apply to the request. * * @return array */ public function rules() { return $this->getAproperRules(); } /** * The rules of create request. * * @return array */ public function createRules() { return []; } /** * The rules of update request. * * @return array */ public function updateRules() { return []; }
Filterable
First you should use
Filterable
trait in your model.
namespace App\Models;
use Elnooronline\LaravelConcerns\Http\Filters\Filterable;
use Elnooronline\LaravelConcerns\Models\Abstracts\Authenticatable;
class User extends Authenticatable
{
use Filterable;
...
}
Then run the following command to generate your filter class.
php artisan make:filter UserFilter
App\Http\Filters\UserFilter
<?php
namespace App\Http\Filters;
use Elnooronline\LaravelConcerns\Http\Filters\BaseFilters;
class UserFilter extends BaseFilters
{
/**
* Registered filters to operate upon.
*
* @var array
*/
protected $filters = [
'type',
'created_at',
];
/**
* Filter the query by a given type.
*
* @param string|int $value
* @return \Illuminate\Database\Eloquent\Builder
*/
protected function type($value)
{
return $this->builder->where('type', $value);
}
/**
* Filter the query by a given created at date.
*
* @param string|int $value
* @return \Illuminate\Database\Eloquent\Builder
*/
protected function createdAt($value)
{
return $this->builder->whereDate('created_at', $value);
}
}
Now you can use the filter from controller.
Example Url :
/users?type=admin
<?php
namespace App\Http\Controllers;
use App\Models\User;
use App\Http\Filters\UserFilter;
use Illuminate\Support\Facades\View;
class UserController extends Controller
{
/**
* Display a listing of the resource.
*
* @param \App\Http\Filters\UserFilter $filter
* @return \Illuminate\Http\Response
*/
public function index(UserFilter $filter)
{
return View::make('users.index')
->withUsers(
User::filter($filter)->latest()->paginate()
);
}
...
}
Eloquent Multiple Auth Provider
You should add 'eloquent.multiple' provider in your
config/auth.php
'providers' => [
'users' => [
'driver' => 'eloquent.multiple',
'model' => App\Models\User::class,
'mapping' => [
App\Models\User::ADMIN_TYPE => App\Models\Admin::class,
App\Models\User::USER_TYPE => App\Models\User::class,
],
],
Add
type
column in users table.
Schema::table('users', function (Blueprint $table) {
$table->string('type')->index();
});
In
User
model should add constants of user types.
class User extends Authenticatable
{
/**
* The code of normal user type.
*/
const USER_TYPE = 'user';
/**
* The code of admin type.
*/
const ADMIN_TYPE = 'admin';
Also you should use
Elnooronline\LaravelConcerns\Models\Concerns\SingleTableInheritance
trait in user model to fill type in creating event.
// User model
use Elnooronline\LaravelConcerns\Models\Concerns\SingleTableInheritance;
class User extends Authenticatable
{
use SingleTableInheritance;
/**
* The type of the current model for single table inheritance.
*
* @var string
*/
protected $modelType = 'user';
...
// Admin Model
use Elnooronline\LaravelConcerns\Models\Scopes\UserTypeScope;
use Elnooronline\LaravelConcerns\Models\Concerns\SingleTableInheritance;
class Admin extends User
{
use SingleTableInheritance;
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'users';
/**
* The type of the current model for single table inheritance.
*
* @var string
*/
protected $modelType = 'admin';
/**
* The "booting" method of the model.
*
* @return void
*/
protected static function boot()
{
parent::boot();
static::addGlobalScope(new UserTypeScope(static::ADMIN_TYPE));
}
...
The
Elnooronline\LaravelConcerns\Models\Scopes\UserTypeScope
used to determine the user type when use the model. and should be added to all user type models.
Now if your account type is
admin
will returnApp\Models\Admin
instance when you callAuth::user()
insted ofApp\Models\User
.
Note : the
App\Models\Admin
model and other type models should extendsApp\Models\User
model.