Last active
May 14, 2024 08:48
-
-
Save karandatwani92/953e5817692529f1cb54514f7b2b5d18 to your computer and use it in GitHub Desktop.
Using MediaLibrary Uploaders with Relationship pivot field
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Illuminate\Database\Migrations\Migration; | |
use Illuminate\Database\Schema\Blueprint; | |
use Illuminate\Support\Facades\Schema; | |
return new class extends Migration | |
{ | |
/** | |
* Run the migrations. | |
*/ | |
public function up(): void | |
{ | |
Schema::create('products', function (Blueprint $table) { | |
$table->id(); | |
$table->string('name'); | |
$table->timestamps(); | |
}); | |
} | |
/** | |
* Reverse the migrations. | |
*/ | |
public function down(): void | |
{ | |
Schema::dropIfExists('products'); | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Illuminate\Database\Migrations\Migration; | |
use Illuminate\Database\Schema\Blueprint; | |
use Illuminate\Support\Facades\Schema; | |
return new class extends Migration | |
{ | |
/** | |
* Run the migrations. | |
*/ | |
public function up(): void | |
{ | |
Schema::create('attribute_options', function (Blueprint $table) { | |
$table->id(); | |
$table->string('name'); | |
$table->timestamps(); | |
}); | |
} | |
/** | |
* Reverse the migrations. | |
*/ | |
public function down(): void | |
{ | |
Schema::dropIfExists('attribute_options'); | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Illuminate\Database\Migrations\Migration; | |
use Illuminate\Database\Schema\Blueprint; | |
use Illuminate\Support\Facades\Schema; | |
return new class extends Migration | |
{ | |
/** | |
* Run the migrations. | |
*/ | |
public function up(): void | |
{ | |
Schema::disableForeignKeyConstraints(); | |
Schema::create('attribute_option_product', function (Blueprint $table) { | |
$table->id(); | |
$table->foreignId('product_id')->constrained(); | |
$table->foreignId('attribute_option_id')->constrained(); | |
$table->string('image')->nullable(); | |
$table->timestamps(); | |
}); | |
Schema::enableForeignKeyConstraints(); | |
} | |
/** | |
* Reverse the migrations. | |
*/ | |
public function down(): void | |
{ | |
Schema::dropIfExists('attribute_option_product'); | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Models; | |
use Backpack\CRUD\app\Models\Traits\CrudTrait; | |
use Illuminate\Database\Eloquent\Factories\HasFactory; | |
use Illuminate\Database\Eloquent\Model; | |
use Spatie\MediaLibrary\HasMedia; | |
use Spatie\MediaLibrary\InteractsWithMedia; | |
class AttributeOption extends Model implements HasMedia | |
{ | |
use CrudTrait; | |
use HasFactory; | |
use InteractsWithMedia; | |
/** | |
* The attributes that are mass assignable. | |
* | |
* @var array | |
*/ | |
protected $fillable = [ | |
'name', | |
]; | |
/** | |
* The attributes that should be cast to native types. | |
* | |
* @var array | |
*/ | |
protected $casts = [ | |
'id' => 'integer', | |
]; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Models; | |
use Backpack\CRUD\app\Models\Traits\CrudTrait; | |
use Illuminate\Database\Eloquent\Factories\HasFactory; | |
use Illuminate\Database\Eloquent\Model; | |
use Spatie\MediaLibrary\HasMedia; | |
use Spatie\MediaLibrary\InteractsWithMedia; | |
class Product extends Model implements HasMedia | |
{ | |
use InteractsWithMedia; | |
use CrudTrait; | |
use HasFactory; | |
/** | |
* The attributes that are mass assignable. | |
* | |
* @var array | |
*/ | |
protected $fillable = [ | |
'name', | |
]; | |
/** | |
* The attributes that should be cast to native types. | |
* | |
* @var array | |
*/ | |
protected $casts = [ | |
'id' => 'integer', | |
]; | |
public function attributesOptions() | |
{ | |
return $this->belongsToMany(\App\Models\AttributeOption::class, 'attribute_option_product') | |
->withPivot('image') | |
->using(\App\Models\ProductAttributeOption::class) | |
->withTimestamps(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Models; | |
use Illuminate\Database\Eloquent\Factories\HasFactory; | |
use Illuminate\Database\Eloquent\Relations\BelongsTo; | |
use Spatie\MediaLibrary\HasMedia; | |
use Spatie\MediaLibrary\InteractsWithMedia; | |
use Illuminate\Database\Eloquent\Relations\Pivot; | |
class ProductAttributeOption extends Pivot implements HasMedia | |
{ | |
use HasFactory; | |
use InteractsWithMedia; | |
protected $table = 'attribute_option_product'; | |
/** | |
* The attributes that are mass assignable. | |
* | |
* @var array | |
*/ | |
protected $fillable = [ | |
'product_id', | |
'attribute_option_id', | |
'image' | |
]; | |
/** | |
* The attributes that should be cast to native types. | |
* | |
* @var array | |
*/ | |
protected $casts = [ | |
'id' => 'integer', | |
'product_id' => 'integer', | |
'attribute_option_id' => 'integer', | |
]; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Controllers\Admin; | |
use App\Http\Requests\ProductRequest; | |
use Backpack\CRUD\app\Http\Controllers\CrudController; | |
use Backpack\CRUD\app\Library\CrudPanel\CrudPanelFacade as CRUD; | |
/** | |
* Class ProductCrudController | |
* @package App\Http\Controllers\Admin | |
* @property-read \Backpack\CRUD\app\Library\CrudPanel\CrudPanel $crud | |
*/ | |
class ProductCrudController 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\Product::class); | |
CRUD::setRoute(config('backpack.base.route_prefix') . '/product'); | |
CRUD::setEntityNameStrings('product', 'products'); | |
} | |
/** | |
* 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(); // set columns from db columns. | |
/** | |
* Columns can be defined using the fluent syntax: | |
* - CRUD::column('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(ProductRequest::class); | |
CRUD::field('name'); | |
CRUD::field([ | |
'name' => 'attributesOptions', | |
'label' => 'Glass', | |
'type' => 'relationship', | |
'subfields' => [ | |
[ | |
'name' => 'image', | |
'type' => 'upload', | |
'label' => 'Image', | |
'withFiles' => true, | |
'wrapper' => ['class' => 'form-group col-md-2'], | |
], | |
], | |
'pivotSelect' => [ | |
'label' => 'Glass', | |
'placeholder' => 'select', | |
'wrapper' => ['class' => 'col-md-4'], | |
'options' => (function ($query) { | |
return $query; | |
}), | |
], | |
'new_item_label' => 'Add glass', | |
'init_rows' => 1, | |
'min_rows' => 0, | |
'max_rows' => 3, | |
'reorder' => false, | |
]); | |
} | |
/** | |
* Define what happens when the Update operation is loaded. | |
* | |
* @see https://backpackforlaravel.com/docs/crud-operation-update | |
* @return void | |
*/ | |
protected function setupUpdateOperation() | |
{ | |
$this->setupCreateOperation(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment