z-song / laravel-admin

Build a full-featured administrative interface in ten minutes
https://laravel-admin.org
MIT License
11.15k stars 2.82k forks source link

ErrorException In Str.php line 833: Array to string conversion #5685

Closed rahmannurhidayat022 closed 1 year ago

rahmannurhidayat022 commented 1 year ago

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:

<?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;
    }
}
rahmannurhidayat022 commented 1 year ago

Solved. this issue in checkbox form, so I changed to radio form.