philonik's avatar

Error Call to a member function store() on null

I am trying to upload an image to my Properties Database and am getting the following error...

Error
Call to a member function store() on null

Form

namespace App\Http\Livewire\Property;

use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Input;
use App\Models\Properties;
use Livewire\WithFileUploads;
use Livewire\Component;

class Form extends Component
{
    use WithFileUploads;

    public $address;
    public $user_id;
    public $city;
    public $county;
    public $post_code;
    public $purchase_price;
    Public $purchase_date;
    Public $current_value;
    Public $paid_cash;
    Public $mortgage_balance;
    Public $mortgage_initial;
    Public $mortgage_term;
    Public $interest_rate;

    // Image Upload

    public $property_image;
    public $iteration;


    protected $rules = [
        'address' => 'required|min:6'
    ];

    public function render()
    {
        return view('livewire.property.form');
    }

    public function addProperty()
    {
        $this->validate([
            'property_image' => 'image|mimes:png,jpg|max:2048', // 1MB Max
        ]);

        $prop = new Properties();
        $prop->address = $this->address;
        $prop->property_image->store('photos'); // Image Upload
        $prop->user_id = Auth::id();
        $prop->city = $this->city !== null ? $this->city : '';
        $prop->county = $this->county !== null ? $this->county : '';
        $prop->post_code = $this->post_code !== null ? $this->post_code : '';
        $prop->purchase_price = $this->purchase_price !== null ? $this->purchase_price : '';
        $prop->purchase_date = $this->purchase_date;
        $prop->current_value = $this->current_value;
        $prop->paid_cash = $this->paid_cash !== null ? $this->paid_cash : 0;
        $prop->mortgage_balance = $this->mortgage_balance;
        $prop->mortgage_initial = $this->mortgage_initial;
        $prop->mortgage_term = $this->mortgage_term;
        $prop->interest_rate = $this->interest_rate;

        $prop->save();

        $this->emit('saved');

         //clean up
         $this->propert_image=null;
         $this->iteration++;
    }

}

Form (blade

<x-jet-form-section submit="addProperty">
                <x-slot name="title">
                    {{ __('Add Property') }}
                </x-slot>

                <x-slot name="form">

                <div class="col-span-6">

                        @if ($property_image)
                            <img src="{{ $property_image->temporaryUrl() }}">
                        @endif
                        <x-jet-input id="property_image" type="file" class="mt-1 block w-full" wire:model="property_image" id="upload{{ $iteration }}"/>
                        @error('property_image') <span class="error">{{ $message }}</span> @enderror


                    </div>...
0 likes
4 replies
andyabihaidar's avatar

I think you mean to do something like this:

$savedPath = $request->file('property_image')->store('photos');
$prop->property_image = $savedPath;
tykus's avatar

The UploadedFile will be the public property on the Component:

$prop->property_image = $this->store('photos');

You were incorrectly using $prop->property_image as an UploadedFile instance

philonik's avatar

This is giving me the following error...

Method App/Http/Livewire/Property/Form::store does not exist

tykus's avatar
tykus
Best Answer
Level 104

Sorry, over-zealous with the edit:

$prop->property_image = $this->property_image->store('photos');

$this->property_image being the Component's TemporaryUploadedFile property

1 like

Please or to participate in this conversation.