z-song / laravel-admin

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

Property [id] does not exist on this collection instance. How to solve this problem? #5848

Closed jesnagifto closed 4 months ago

jesnagifto commented 4 months ago

Description:

protected function form() //form add/edit { $form = new Form(new Product()); $form->text('code',('Code')); $form->text('name',('Product Name'));

    $form->select('catg_id', __('Category Name'))
    ->options(Category::all()->pluck('catg_name', 'id'))
    ->setWidth(4, 2)
    ->load('brand_id', '/admin/api/brands'); 

    $form->multipleSelect('brand_id', __('Brand Name'))->setWidth(4, 2)
    ->options(function ($id) {
        $brand = Brands::find($id);

        if ($brand) {
            return [$brand->id => $brand->id];
        } else {
            return [];
        }
    });

$form->currency('rate', __('Rate'))->symbol('₹')->setWidth(4, 2);
$form->switch('active', __('Active'))->states(Constant::STATUS)->default(1);

$form->saved(function (Form $form) { $id = $form->model()->id; $catid = $form->model()->catg_id; $brandid = $form->model()->getOriginal('brand_id');

if ($id && is_array($brandid)) {
    foreach ($brandid as $followItem) {

        $brand = Brands::where('brand_name', $followItem)->first();

        $tracks = new prdt_brand_category();
        $tracks->prdt_id = $id;
        $tracks->category_id = $catid;
        $tracks->brands_id = $brand->id;
        $tracks->save();
    }
}

}); $form->builder()->isEditing(); return $form;

}

// Your controller

public function brands(Request $request) { $brand = $request->get('q'); $brandNames = Brand_category::with('brans')->where('category_id', $brand)->get()->pluck('brans.brand_name');

return $brandNames;

}

Screenshot 2024-02-19 144347

Steps To Reproduce: