Make images column polmorphic

This commit is contained in:
bad 2021-12-08 21:29:28 +01:00
parent 46e1fa8aee
commit 9b9bd4ff7b
4 changed files with 75 additions and 1 deletions

View file

@ -18,4 +18,8 @@ class Image extends Model
{
return Storage::url($this->path);
}
public function imageable() {
$this->morphTo();
}
}

View file

@ -13,7 +13,7 @@ class Product extends Model
public function images()
{
return $this->hasMany(Image::class, "product_uuid", "uuid");
return $this->morphMany(Image::class, 'imageable');
}
protected $fillable = [

View 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
}
}

View 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
}
}