Ajvanho's avatar
Level 14

Call to a member function store() on string

I am doing LC Livewire series, episode 11, File Uploads, but I have an error, when I want to update product without photo: Call to a member function store() on string

namespace App\Http\Livewire;

use Livewire\Component;
use App\Models\Product;

use Livewire\WithFileUploads;

class ProductEdit extends Component
{
	use WithFileUploads;	

	public $product;	

	public $name;
    public $price;
    public $photo;

    protected $rules = [
        'name' => 'required',
        'price' => 'required',
        'photo' => 'nullable|sometimes|max:5000',
    ];

	public function mount(Product $product)
    {
    	$this->product = $product;


    	$this->name = $product->name;
    	$this->price = $product->price;
    	$this->photo = $product->photo; 	
    }

    public function submitForm()
    {
        $this->validate(); 

$imageToShow = $this->product->photo ?? null;	

        $this->product->update([
            'name' => $this->name,
            'price' => $this->price,
            'photo' => $this->photo ? $this->photo->store('photos', 'public') : $imageToShow,
        ]);

         session()->flash('message', 'Product was updated successfully!');

    }

    public function render()
    {
    	return view('livewire.product-edit');
    }
}
0 likes
2 replies
Snapey's avatar
Snapey
Best Answer
Level 122

You are using $this->photo to be both the uploaded file object AND the string from the database?

1 like
Ajvanho's avatar
Level 14
public function mount(Product $product)
    {
    	$this->product = $product;


    	$this->name = $product->name;
    	$this->price = $product->price;
    	//$this->photo = $product->photo;	//I deleted this and now it works
    }

Please or to participate in this conversation.