Randy_Johnson's avatar

Image Upload Not working If no Image is selected

When creating an item, if an image is selected then it is added to the db, but if an image isn't then it is not. The added a placement image to just have something, to see if that would work.

The strange thing is that one time, whilst using the nullable entry, it worked, but only once.

    public function up()
    {
        Schema::create('items', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->integer('amount');
            $table->char('shelf_sec');
            $table->integer('shelf_num');
            $table->string('img')->nullable();
            $table->timestamps();
        });
    }

    public function addItem()
    {
        $this->validate([
            'photo' => 'image|max:10000', 
            ]);

        $filename = $this->photo->store('photos');
            
        if($filename == null)
            $filename = "no-photo-available.png";
        
        $item = Item::create([
            'name' => $this->itemName,
            'amount' => $this->itemAmount,
            'shelf_sec' => $this->itemLetter,
            'shelf_num' => $this->itemNumber,
            'img' => $filename, 
        ]);

        $this->items = Item::all();
    }
0 likes
8 replies
MichalOravec's avatar
public function addItem()
{
    $this->validate([
        'photo' => 'image|max:10000',
    ]);

    $item = Item::create([
        'name' => $this->itemName,
        'amount' => $this->itemAmount,
        'shelf_sec' => $this->itemLetter,
        'shelf_num' => $this->itemNumber,
        'img' => $this->hasFile('photo') ? $this->photo->store('photos') : 'no-photo-available.png'
    ]);

    $this->items = Item::all();
}

You may determine if a file is present on the request using the hasFile method.

Docs: https://laravel.com/docs/8.x/requests#retrieving-uploaded-files

Randy_Johnson's avatar

Hi, I get this error, I am using Livewire.

Method App\Http\Livewire\Inventory::hasFile does not exist. 
MichalOravec's avatar

Try it to change to this

'img' => $this->photo ? $this->photo->store('photos') : 'no-photo-available.png'

I don't use livewire so I just guessing.

2 likes
Randy_Johnson's avatar

So much love to the top ten, you guys really are the pillars of this community!

Randy_Johnson's avatar

Hi, so it turns out that I hadn't fixed the problem, but I have found the source of the problem and it is to do with the validation.

    public function addItem()
    {
        if ($this->photo != null) {
            $this->validate([
                'photo' => 'image|max:10000',
            ]);
        }
    
        $item = Item::create([
            'name' => $this->itemName,
            'amount' => $this->itemAmount,
            'shelf_sec' => $this->itemLetter,
            'shelf_num' => $this->itemNumber,
            'img' => is_file($this->photo) ?  $this->photo->store('photos') : 'no-photo-available.png',
        ]);

        $this->photo = null;
    
        $this->items = Item::all();
    }

I gave an if statement, but now when I select an image, no-photo-available.png is shown, when I don't select an image, no photo available.png is shown.

Randy_Johnson's avatar

Thanks! Everything you said was right from the beginning!

    public function addItem()
    {
        $this->validate([
            'photo' => 'nullable|image|max:10000',
        ]);
    
        $item = Item::create([
            'name' => $this->itemName,
            'amount' => $this->itemAmount,
            'shelf_sec' => $this->itemLetter,
            'shelf_num' => $this->itemNumber,
            'img' => $this->photo ?  $this->photo->store('photos') : 'no-photo-available.png',
        ]);

        $this->itemName = null;
        $this->itemAmount = null;
        $this->photo = null;
    
        $this->items = Item::all();
    }

Please or to participate in this conversation.