Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
added Snippets CRUD
  • Loading branch information
tabacitu committed Dec 3, 2019
commit a541cb6f2c51636517c8f95afc04bc31f28f5e17
102 changes: 102 additions & 0 deletions app/Http/Controllers/Admin/SnippetCrudController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php

namespace App\Http\Controllers\Admin;

use App\Http\Requests\SnippetRequest;
use Backpack\CRUD\app\Http\Controllers\CrudController;
use Backpack\CRUD\app\Library\CrudPanel\CrudPanelFacade as CRUD;

/**
* Class SnippetCrudController
* @package App\Http\Controllers\Admin
* @property-read CrudPanel $crud
*/
class SnippetCrudController extends CrudController
{
use \Backpack\CRUD\app\Http\Controllers\Operations\ListOperation;
use \Backpack\CRUD\app\Http\Controllers\Operations\CreateOperation;
use \Backpack\CRUD\app\Http\Controllers\Operations\UpdateOperation;
use \Backpack\CRUD\app\Http\Controllers\Operations\DeleteOperation;
use \Backpack\CRUD\app\Http\Controllers\Operations\ShowOperation;

public function setup()
{
$this->crud->setModel('App\Models\Snippet');
$this->crud->setRoute(config('backpack.base.route_prefix') . '/snippet');
$this->crud->setEntityNameStrings('snippet', 'snippets');
}

protected function setupListOperation()
{
$this->crud->addColumn('name');
$this->crud->addColumn([
'label' => 'Category',
'type' => 'select',
'name' => 'category_id',
'entity' => 'category',
'attribute' => 'name',
]);
$this->crud->addColumn([
'label' => 'Created by',
'type' => 'select',
'name' => 'created_by',
'entity' => 'creator',
'attribute' => 'name',
]);

$this->crud->addColumn([
'label' => 'Updated by',
'type' => 'select',
'name' => 'updated_by',
'entity' => 'updater',
'attribute' => 'name',
]);
}

protected function setupShowOperation()
{
$this->setupListOperation();

$this->crud->addColumn('description');
$this->crud->addColumn('content');
}

protected function setupCreateOperation()
{
$this->crud->setValidation(SnippetRequest::class);

$this->crud->addField([
'type' => 'text',
'name' => 'name',
'label' => 'Name',
'wrapperAttributes' => [
'class' => 'form-group col-md-6'
]
]);
$this->crud->addField([
'label' => 'Category',
'type' => 'select',
'name' => 'category_id',
'entity' => 'category',
'attribute' => 'name',
'wrapperAttributes' => [
'class' => 'form-group col-md-6'
]
]);
$this->crud->addField([
'type' => 'simplemde',
'name' => 'description',
'label' => 'Description'
]);
$this->crud->addField([
'type' => 'textarea',
'name' => 'content',
'label' => 'Content'
]);
}

protected function setupUpdateOperation()
{
$this->setupCreateOperation();
}
}
57 changes: 57 additions & 0 deletions app/Http/Requests/SnippetRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace App\Http\Requests;

use App\Http\Requests\Request;
use Illuminate\Foundation\Http\FormRequest;

class SnippetRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
// only allow updates if the user is logged in
return backpack_auth()->check();
}

/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'name' => 'required|min:5|max:255',
'category_id' => 'required',
];
}

/**
* Get the validation attributes that apply to the request.
*
* @return array
*/
public function attributes()
{
return [
//
];
}

/**
* Get the validation messages that apply to the request.
*
* @return array
*/
public function messages()
{
return [
//
];
}
}
61 changes: 61 additions & 0 deletions app/Models/Snippet.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace App\Models;

use Backpack\CRUD\app\Models\Traits\CrudTrait;
use Illuminate\Database\Eloquent\Model;

class Snippet extends Model
{
use CrudTrait;
use Traits\CreatedByTrait;

/*
|--------------------------------------------------------------------------
| GLOBAL VARIABLES
|--------------------------------------------------------------------------
*/

protected $table = 'snippets';
// protected $primaryKey = 'id';
public $timestamps = true;
protected $guarded = ['id'];
// protected $fillable = [];
// protected $hidden = [];
// protected $dates = [];

/*
|--------------------------------------------------------------------------
| FUNCTIONS
|--------------------------------------------------------------------------
*/

/*
|--------------------------------------------------------------------------
| RELATIONS
|--------------------------------------------------------------------------
*/

public function category()
{
return $this->belongsTo('Backpack\NewsCRUD\app\Models\Category', 'category_id');
}

/*
|--------------------------------------------------------------------------
| SCOPES
|--------------------------------------------------------------------------
*/

/*
|--------------------------------------------------------------------------
| ACCESSORS
|--------------------------------------------------------------------------
*/

/*
|--------------------------------------------------------------------------
| MUTATORS
|--------------------------------------------------------------------------
*/
}
39 changes: 39 additions & 0 deletions app/Models/Traits/CreatedByTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace App\Models\Traits;

use Illuminate\Database\Eloquent\Model;

trait CreatedByTrait
{
/**
* Stores the user id at each create & update.
*/
public function save(array $options = [])
{
$user = backpack_auth()->check() ? backpack_auth()->user() : (\Auth::check() ? \Auth::user() : false );

if ($user) {
$this->created_by = $this->created_by ?? $user->id;
$this->updated_by = $user->id;
}

parent::save();
}

/*
|--------------------------------------------------------------------------
| RELATIONS
|--------------------------------------------------------------------------
*/

public function creator()
{
return $this->belongsTo('App\User', 'created_by');
}

public function updater()
{
return $this->belongsTo('App\User', 'updated_by');
}
}
37 changes: 37 additions & 0 deletions database/migrations/2019_12_03_115109_create_snippets_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateSnippetsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('snippets', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->integer('category_id')->unsigned();
$table->text('description')->nullable();
$table->text('content')->nullable();
$table->integer('created_by')->nullable()->unsigned();
$table->integer('updated_by')->nullable()->unsigned();
$table->timestamps();
$table->softDeletes();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('snippets');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,6 @@
<li class="nav-title">Demo Entities</li>
<li class="nav-item"><a class="nav-link" href="{{ backpack_url('monster') }}"><i class="nav-icon fa fa-optin-monster"></i> <span>Monsters</span></a></li>
<li class="nav-item"><a class="nav-link" href="{{ backpack_url('icon') }}"><i class="nav-icon fa fa-info-circle"></i> <span>Icons</span></a></li>
<li class="nav-item"><a class="nav-link" href="{{ backpack_url('product') }}"><i class="nav-icon fa fa-shopping-cart"></i> <span>Products</span></a></li>
<li class="nav-item"><a class="nav-link" href="{{ backpack_url('product') }}"><i class="nav-icon fa fa-shopping-cart"></i> <span>Products</span></a></li>

<li class='nav-item'><a class='nav-link' href='{{ backpack_url('snippet') }}'><i class='nav-icon fa fa-question'></i> Snippets</a></li>
3 changes: 2 additions & 1 deletion routes/backpack/custom.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,5 @@
});
}
}
}); // this should be the absolute last line of this file
Route::crud('snippet', 'SnippetCrudController');
}); // this should be the absolute last line of this file