d4nd3v / crud by d4nd3v

Laravel 5 CRUD Generator
146
0
0
Package Data
Maintainer Username: d4nd3v
Package Create Date: 2017-01-21
Package Last Update: 2017-12-04
Language: PHP
License: MIT
Last Refreshed: 2025-02-05 15:04:08
Package Statistics
Total Downloads: 146
Monthly Downloads: 0
Daily Downloads: 0
Total Stars: 0
Total Watchers: 0
Total Forks: 0
Total Open Issues: 0

Laravel 5.3 CRUD Generator

Usage

Step 1: Install Through Composer

composer require d4nd3v/crud:dev-master

Step 2: Add the Service Provider

Add the provider in app/Providers/AppServiceProvider.php

public function register()
{
    ...
	if ($this->app->environment() !== 'production') {
		$this->app->register('D4nd3v\Crud\CrudServiceProvider');
	}
}

Step 3: Run Artisan!

Run php artisan generate:crud TABLE_NAME from the console.

|Options |Description | |:---------------------------------|:---------------------------| |--overwrite=false | overwrite existing files | |--crud=CRUD | C(reate) R(read) U(pdate) D(elete) | |--model_only=false | only generates model file | |--parent_of=TABLE1,TABLE2,TABLE3 | add links & "belongs to" in model | |--child_of=TABLE4,TABLE5,TABLE6 | generates links to parent | |--many_with=TABLE1,TABLE2 | many to many relation | |--upload=field1,field2 | transformed to file upload fields |

Options examples

--crud=RD => all actions without C-reate and U-pdate
--many_with adds:
  • in model: ...$this->belongsToMany...
  • in controller: ...->sync(request()->input(....
  • in index view: ...list from MNY_TBL...
  • in create and edit view: ...js select2 MNY_TBL...

Notes

If you need pagination don't forget to run:
php artisan vendor:publish --tag=laravel-pagination

The generated files are:

  • app/Http/Controllers/Crud/[Resource]Controller.php
  • app/Models/[Resource].php
  • resources/views/[Resource]/create.blade.php
  • resources/views/[Resource]/edit.blade.php
  • resources/views/[Resource]/index.blade.php
  • resources/views/[Resource]/view.blade.php
  • + added route resource in routes/web.php

####For User Roles (or any other filter)####

in ```UsersController.php````

	$orderBy = $request->input('by', 'id');
	....
		$roles = array();
		if (class_exists(Role::class)) {
			$roles = Role::get();
		}
		if(!empty(request()->input('role'))) {
			$items = User::role(request()->input('role'))->orderBy($orderBy, $order)->paginate(20);
		} else {
			$items = User::orderBy($orderBy, $order)->paginate(20);
		}
		
        return view('users.index')
            ->withItems($items)
            ->withPage($request->input('page', 1))
            ->withOrder($order)
            ->withOrderBy($orderBy)
            ->withRoles($roles);

in index.blade.php

	@section('content')
	.....
	@if($roles)
		<a href="?role=">all</a>
		@foreach($roles as $role)
			&nbsp; <a href="?role={{ $role->name }}"
					  @if(request()->input('role')==$role->name) class="active" @endif
			>{{ $role->name }}</a>
		@endforeach
		<hr>
	@endif