1. Go to this page and download the library: Download flobbos/laravel-crudable 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/ */
flobbos / laravel-crudable example snippets
'use_auto_binding' => false
'default_namespace' => 'Services'
'default_resource' => 'Admin',
'css_framework' => 'tailwind',
return [
'implementations' => [
[
//This is where you set the requesting class
'when' => \App\Http\Controllers\Admin\UserController::class,
//This is where you can define your own contracts
'needs' => \Your\Own\Contract::class,
//This is where you send out the implementation
'give' => \App\Services\UserService::class
]
]
];
namespace App\Services;
use App\Country;
use Flobbos\Crudable\Contracts\Crud;
use Flobbos\Crudable;
class CountryService implements Crud {
use Crudable\Crudable;
public function __construct(Country $country) {
$this->model = $country;
}
}
php artisan crud:controller
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Flobbos\Crudable\Contracts\Crud;
use Exception;
class CountryController extends Controller{
protected $country;
public function __construct(Crud $country) {
$this->country = $country;
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index(){
return view('admin.countries.index')->with(['country'=>$this->country->get()]);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create(){
return view('admin.countries.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request){
$this->validate($request, []);
try{
$this->country->create($request->all());
return redirect()->route('')->withMessage(trans('crud.record_created'));
} catch (Exception $ex) {
return redirect()->back()->withErrors($ex->getMessage())->withInput();
}
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id){
return view('admin.countries.show')->with(['country'=>$this->country->find($id)]);
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id){
return view('admin.countries.edit')->with(['country'=>$this->country->find($id)]);
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id){
$this->validate($request, []);
try{
$this->country->update($id, $request->all());
return redirect()->route('admin.countries.index')->withMessage(trans('crud.record_updated'));
} catch (Exception $ex) {
return redirect()->back()->withInput()->withErrors($ex->getMessage());
}
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id){
try{
$this->country->delete($id);
return redirect()->route('admin.countries.index')->withMessage(trans('crud.record_deleted'));
} catch (Exception $ex) {
return redirect()->route('admin.countries.index')->withErrors($ex->getMessage());
}
}
}
php artisan crud:controller --blank
php artisan crud:views YourModelName
php artisan crud:resource Country
php artisan crud:resource Country --silent
php artisan crud:contract YourContract
namespace App\Contracts;
use Flobbos\Crudable\Contracts\Crud;
interface CountryContract extends Crud{
//your custom code here
}
use Flobbos\Crudable\Contracts\Crud;
use Flobbos\Crudable;
use Flobbos\Crudable\Contracts\Translation;
class CategoryService implements Crud,Translation{
use Crudable\Crudable;
use \Flobbos\Crudable\Translations\Translatable;
//only necessary if your translation relation is named something else
//than 'translations' in your model
protected $translation_name = 'my_translations';
//optional array of fields that HAVE to be present to save a translation
protected $
public function processTranslations(
array $translations,
$trans_key = null,
$language_key = 'language_id'){
$approved = [];
foreach($translations as $trans){
//Check for translation key
if(!is_null($trans_key)){
unset($trans[$trans_key]);
}
//Filter out empty fields
if(!isset($this->
public function saveTranslations(
\Illuminate\Database\Eloquent\Model $model,
array $translations){
if(empty($translations))
throw new MissingTranslationsException;
return $model->{$this->translation_name}()->saveMany($translations);
}
public function processTranslations(
array $translations,
$trans_key = null,
$language_key = 'language_id');
public function saveTranslations(
\Illuminate\Database\Eloquent\Model $model,
array $translations,
$relation_name = 'translations');
public function updateTranslations(
array $translations,
\Illuminate\Database\Eloquent\Model $model,
$translation_key,
$translation_class);
public function checkRequired(array $arr);
public function filterNull(array $arr, $except = null);
protected $slug_field = 'title';
use Flobbos\Crudable\Contracts\Sluggable;
class YourServiceClass implements Crud,Sluggable{}
protected $slug_name = 'url_slug';
use Flobbos\Crudable\Contracts\Sluggable;
use Flobbos\Crudable\Contracts\Slugger;
class YourServiceClass implements Crud,Sluggable,Slugger{}
use Crudable\Slugs\Slugger;
public function getResourceIdFromTranslatedSlug(string $slug): int;
public function getTranslatedSlugFromResourceId(int $id, int $language_id): string;
use App\Country;
class CountryService {
use \Flobbos\Crudable\Crudable;
//protected $model; in version 2.0 and higher this no longer needs to be set
public function __construct(Country $country){
$this->model = $country;
}
}
namespace App\Http\Controllers;
use Flobbos\Crudable\Contracts\Crud;
class SomeController extends Controller {
protected $crud;
public function __construct(Crud $crud){
$this->crud = $crud;
}
}
namespace App\Contracts;
use Flobbos\Crudable\Contracts\Crud;
interface MyOwnContract extends Crud{
//place your custom code here
}