user29152626
user29152626

Reputation: 11

Laravel Backpack v6 CRUD: select2_from_ajax field not displaying returned AJAX values

I'm working with Laravel Backpack CRUD and have a select2_from_ajax field that needs to display product prices based on customer selection. with different customer getting different pricing currently i use

public function fetchProductPrice()
    {
        $formData = request()->input('form');
        $customerIdField = collect($formData)->firstWhere('name', 'customer_id');
        $productIdField = collect($formData)->firstWhere('name', 'items[0][product_id]');
        $customerId = $customerIdField['value'];
        $productId = $productIdField['value'];

        $customer = \App\Models\Customers::find($customerId);
        $priceSettingId = $customer->price_setting_id;

    //     dd([
    //     'customerId' => $customerId,
    //     'productId' => $productId,
    //     'priceSettingId' => $priceSettingId,
    //     'query' => \App\Models\Price::where('price_setting_id', $priceSettingId)
    //         ->where('product_id', $productId)
    //         ->get()
    // ]);

        // $price = \App\Models\Price::where('price_setting_id', $priceSettingId)
        //     ->where('product_id', $productId)
        //     ->first();

        // return [
        //     'id' => $price->id,
        //     'text' => $price->nominal
        // ];
        return $this->fetch(\App\Models\Price::class)
            ->where('price_setting_id', $priceSettingId)
            ->where('product_id', $productId);
    }

any help would be appreciated since this is my first time using this website.

using:

return $this->fetch(\App\Models\Price::class)             
            ->where('price_setting_id', $priceSettingId)             
            ->where('product_id', $productId);

returns the price i wanted for some of the product needed but some of them return in empty response.

using the method below resulting in the fetch i needed but i cant put it in the field

return [
    'id' => $price->id,
    'text' => $price->nominal
]; 

Edit: i solved the query problem by using custom fetch function like this

return $this->fetch([
        'model' => \App\Models\Price::class,
        'searchable_attributes' => ['nominal'],
        'paginate' => 10,
        'query' => function($model) use ($priceSettingId, $productId) {
            return $model->where('price_setting_id', $priceSettingId)
                        ->where('product_id', $productId);
        }
    ]);

instead of only

return $this->fetch(\App\Models\Price::class)
            ->where('price_setting_id', $priceSettingId)
            ->where('product_id', $productId);

Upvotes: 1

Views: 18

Answers (0)

Related Questions