Make images column polmorphic
This commit is contained in:
parent
46e1fa8aee
commit
9b9bd4ff7b
4 changed files with 75 additions and 1 deletions
|
@ -18,4 +18,8 @@ class Image extends Model
|
||||||
{
|
{
|
||||||
return Storage::url($this->path);
|
return Storage::url($this->path);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function imageable() {
|
||||||
|
$this->morphTo();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,7 +13,7 @@ class Product extends Model
|
||||||
|
|
||||||
public function images()
|
public function images()
|
||||||
{
|
{
|
||||||
return $this->hasMany(Image::class, "product_uuid", "uuid");
|
return $this->morphMany(Image::class, 'imageable');
|
||||||
}
|
}
|
||||||
|
|
||||||
protected $fillable = [
|
protected $fillable = [
|
||||||
|
|
35
database/migrations/2021_12_08_183713_make_images_morph.php
Normal file
35
database/migrations/2021_12_08_183713_make_images_morph.php
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
class MakeImagesMorph extends Migration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the migrations.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function up()
|
||||||
|
{
|
||||||
|
Schema::table("images", function(Blueprint $table) {
|
||||||
|
$table->uuidMorphs("imageable");
|
||||||
|
});
|
||||||
|
Schema::table("images", function(Blueprint $table) {
|
||||||
|
DB::table("images")->update(['imageable_id'=>DB::raw('`product_uuid`')], ['imageable_type' => "App\Models\Product"]);
|
||||||
|
$table->dropConstrainedForeignId("product_uuid");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function down()
|
||||||
|
{
|
||||||
|
// Ehh don't wanna bother
|
||||||
|
}
|
||||||
|
}
|
35
database/migrations/2021_12_08_183713_morph_images.php
Normal file
35
database/migrations/2021_12_08_183713_morph_images.php
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
class AddCategories extends Migration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the migrations.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function up()
|
||||||
|
{
|
||||||
|
Schema::table("images", function(Blueprint $table) {
|
||||||
|
$table->uuidMorphs("imageable");
|
||||||
|
});
|
||||||
|
Schema::table("images", function(Blueprint $table) {
|
||||||
|
DB::table("images")->update(['imageable_id'=>DB::raw('`product_uuid`')], ['imageable_type' => "App\Models\Product"]);
|
||||||
|
$table->dropConstrainedForeignId("product_uuid");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function down()
|
||||||
|
{
|
||||||
|
// Ehh don't wanna bother
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue