I want to add a single image form. The image has successfully entered into the public directory, it's just that the image url path is not included in the database.
Here's the configuration
Model Code:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class ProductImage extends Model
{
use HasFactory;
protected $table = "product_images";
protected $fillable = [
'imageUrl',
'isCover',
'product_id',
];
}
Migration Code:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('product_images', function (Blueprint $table) {
$table->id();
$table->string('imageUrl');
$table->enum('isCover', ['true', 'false'])->default('false');
$table->unsignedBigInteger('catalog_id');
$table->index('catalog_id');
$table->foreign('catalog_id')->references('id')->on('catalogs')->onDelete('cascade');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('product_images');
}
};
Controller:
<?php
namespace App\Admin\Controllers;
use App\Models\ProductImage;
use App\Models\Product;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
class ProductImageController extends AdminController
{
/**
* Title for current resource.
*
* @var string
*/
protected $title = 'ProductImage';
/**
* Make a grid builder.
*
* @return Grid
*/
protected function grid()
{
$grid = new Grid(new ProductImage());
$grid->column('id', __('Id'));
$grid->column('imageUrl', __('ImageUrl'));
$grid->column('isCover', __('IsCover'));
$grid->column('catalog_id', __('Catalog id'));
$grid->column('created_at', __('Created at'));
$grid->column('updated_at', __('Updated at'));
return $grid;
}
/**
* Make a show builder.
*
* @param mixed $id
* @return Show
*/
protected function detail($id)
{
$show = new Show(ProductImage::findOrFail($id));
$show->field('id', __('Id'));
$show->field('imageUrl', __('ImageUrl'));
$show->field('isCover', __('IsCover'));
$show->field('catalog_id', __('Catalog id'));
$show->field('created_at', __('Created at'));
$show->field('updated_at', __('Updated at'));
return $show;
}
/**
* Make a form builder.
*
* @return Form
*/
protected function form()
{
$form = new Form(new ProductImage());
$form->image('imageUrl', __('ImageUrl'));
$form->select('catalog_id', __('Catalog id'))->options(Product::pluck('name', 'id'));
$form->checkbox('isCover', __('Set Cover'))->options(['true' => 'Set to Image Cover of this product. note: recommended add only one']);
return $form;
}
}
Description:
I want to add a single image form. The image has successfully entered into the public directory, it's just that the image url path is not included in the database.
Here's the configuration Model Code:
Migration Code:
Controller: