Neeraj1005's avatar

Call to undefined method Illuminate\Database\Eloquent\Relations\MorphMany::attach()

In my laravel project I'm trying to add mulitple image for RFQ table...can anyone tell me what i did wrong. My relationship like: rfq hasMany images AND images belongsTo rfqs This is my livewire method

public function store()
    {
        $validatedData = $this->validate([
            'product_name' => 'required',
            'product_category_id' => 'required|not_in:0',
            'sub_category_id' => 'required|not_in:0',
            'product_quantity' => 'required',
            'unit_id' => 'required|not_in:0',
            'purchase_price' => 'nullable',
            'city' => 'required|',
            'isChecked' => 'required|accepted',
            'details' => 'required|string|min:1|max:500',
            'images.*' => 'image|max:1024',
        ]) + [
            'user_id' => auth()->user()->id,
        ];

        $rfqData = Rfq::create($validatedData);

        // if ($this->images) 
        // {
            foreach ($this->images as $photo) {
                $path_url = $photo->storePublicly('rfqs', 'public');

                $rfqData->images()->attach($photo,[
                    'file_name' => $photo->getClientOriginalName(),
                    'file_path' => $path_url,
                ]);
            }
        // }


        session()->flash('message', 'RFQ submitted successfully.');

        $this->reset();
    }
0 likes
4 replies
Neeraj1005's avatar

@tray2 Ohh! so attach used in only manyTomany case...? Instead of save can I use create also?

tisuchi's avatar

@neeraj1005 Just to confirm, isn't that unnecessary to pass user id to validation data since you are using attach()? It should take user_id automatically. Correct me if I am wrong.

Neeraj1005's avatar

@tisuchi basically I tried the attached for rfqs and images oneToMany relationship table. And user_id is used in rfqs table.

1 like

Please or to participate in this conversation.