Skip to content
This repository has been archived by the owner on Oct 4, 2021. It is now read-only.

Challenge 1 (Rizki) - Buat Migrasi, Model dan CRUD untuk Tags #8

Open
wants to merge 4 commits into
base: origin
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
19 changes: 19 additions & 0 deletions app/Http/Controllers/Admin/ItemCrudController.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@ protected function setupListOperation()
'label' => 'Name',
'name' => 'name',
],

[
'label' => 'Tags',
'name' => 'tags',
'type' => 'relationship',
'entity' => 'tags',
'attribute' => 'name',
'model' => App\Models\Tag::class,
],
]);
}

Expand Down Expand Up @@ -103,6 +112,16 @@ protected function setupCreateOperation()
'label' => 'Name',
'name' => 'name',
],
[
'label' => 'Tags',
'name' => 'tags',
'type' => 'select_multiple',
'entity' => 'tags',
'model' => "App\Models\Tag",
'attribute' => 'name',
'pivot' => true,

]
]);
}

Expand Down
118 changes: 118 additions & 0 deletions app/Http/Controllers/Admin/TagCrudController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?php

namespace App\Http\Controllers\Admin;

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

/**
* Class TagCrudController
* @package App\Http\Controllers\Admin
* @property-read \Backpack\CRUD\app\Library\CrudPanel\CrudPanel $crud
*/
class TagCrudController 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;

/**
* Configure the CrudPanel object. Apply settings to all operations.
*
* @return void
*/
public function setup()
{
CRUD::setModel(\App\Models\Tag::class);
CRUD::setRoute(config('backpack.base.route_prefix') . '/tag');
CRUD::setEntityNameStrings('tag', 'tags');
}

/**
* Define what happens when the List operation is loaded.
*
* @see https://backpackforlaravel.com/docs/crud-operation-list-entries
* @return void
*/
protected function setupListOperation()
{
CRUD::setFromDb(); // columns
$this->crud->addColumn(
[
'label' => 'Items',
'name' => 'items',
'type' => 'relationship',
'entity' => 'items',
'attribute' => 'name',
'model' => App\Models\Item::class,
],
);

/**
* Columns can be defined using the fluent syntax or array syntax:
* - CRUD::column('price')->type('number');
* - CRUD::addColumn(['name' => 'price', 'type' => 'number']);
*/
}

/**
* Define what happens when the Create operation is loaded.
*
* @see https://backpackforlaravel.com/docs/crud-operation-create
* @return void
*/
protected function setupCreateOperation()
{
CRUD::setValidation(TagRequest::class);

CRUD::setFromDb(); // fields
$this->crud->addFields([
[
'label' => 'Items',
'name' => 'items',
'type' => 'select_multiple',
'entity' => 'items',
'model' => "App\Models\Item",
'attribute' => 'name',
'pivot' => true,

]
]);

/**
* Fields can be defined using the fluent syntax or array syntax:
* - CRUD::field('price')->type('number');
* - CRUD::addField(['name' => 'price', 'type' => 'number']));
*/
}

protected function setupShowOperation()
{
CRUD::setFromDb();
$this->crud->addColumn(
[
'label' => 'Items',
'name' => 'items',
'type' => 'relationship',
'entity' => 'items',
'attribute' => 'name',
'model' => App\Models\Item::class,
],
);

}

/**
* Define what happens when the Update operation is loaded.
*
* @see https://backpackforlaravel.com/docs/crud-operation-update
* @return void
*/
protected function setupUpdateOperation()
{
$this->setupCreateOperation();
}
}
56 changes: 56 additions & 0 deletions app/Http/Requests/TagRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace App\Http\Requests;

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

class TagRequest 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'
];
}

/**
* 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 [
//
];
}
}
5 changes: 5 additions & 0 deletions app/Models/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ class Item extends Model
|--------------------------------------------------------------------------
*/

public function tags()
{
return $this->belongsToMany(Tag::class, 'item_tags', 'item_id', 'tag_id');
}

/*
|--------------------------------------------------------------------------
| RELATIONS
Expand Down
58 changes: 58 additions & 0 deletions app/Models/Tag.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace App\Models;

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

class Tag extends Model
{
use CrudTrait;

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

protected $table = 'tags';
protected $primaryKey = 'id';
protected $guarded = ['id'];
protected $fillable = ['name'];
protected $dates = ['created_at', 'updated_at',];

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

public function items()
{
return $this->belongsToMany(Item::class, 'item_tags', 'tag_id', 'item_id');
}

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

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

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

/*
|--------------------------------------------------------------------------
| MUTATORS
|--------------------------------------------------------------------------
*/
}
2 changes: 1 addition & 1 deletion config/database.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@

'redis' => [

'client' => env('REDIS_CLIENT', 'phpredis'),
'client' => env('REDIS_CLIENT', 'predis'),

'options' => [
'cluster' => env('REDIS_CLUSTER', 'redis'),
Expand Down
32 changes: 32 additions & 0 deletions database/migrations/2021_09_12_125849_create_tags_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

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

class CreateTagsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('tags', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('tags');
}
}
41 changes: 41 additions & 0 deletions database/migrations/2021_09_12_132117_create_item_tags_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

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

class CreateItemTagsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('item_tags', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('tag_id');
$table->unsignedBigInteger('item_id');
$table->foreign('tag_id')
->references('id')
->on('tags')
->onDelete('restrict');
$table->foreign('item_id')
->references('id')
->on('items')
->onDelete('restrict');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('item_tags');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,5 @@
</li>
</ul>
</li>

<li class='nav-item'><a class='nav-link' href='{{ backpack_url('tag') }}'><i class='nav-icon la la-question'></i> Tags</a></li>
3 changes: 2 additions & 1 deletion routes/backpack/custom.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@
Route::crud('item/group', 'ItemGroupCrudController');
Route::crud('item/brand', 'ItemBrandCrudController');
Route::crud('item/item', 'ItemCrudController');
}); // this should be the absolute last line of this file
Route::crud('tag', 'TagCrudController');
}); // this should be the absolute last line of this file