hjortur17's avatar

Image store

Hi, I have image upload on my website, I',m trying to make it possible to have it nullable. Any ideas?

This is my store function:

public function store() { $this->validate(request(), [ 'title' => 'required|unique:threads|max:255', 'body' => 'required|min:20', 'image' => 'image|nullable ]);

          Thread::create([
                 'title' => request('title'),
                 'user_id' => Auth::user()->id,
                 'body' => request('body'),
                 'image' => request()->file('image')->store('frettir', 'public'),
          ]);

          return redirect('/stjornbord')->with('flash', 'Fréttin var búin til!');
   }

And this is my migration:

Schema::create('threads', function (Blueprint $table) { $table->increments('id'); $table->integer('user_id'); $table->string('title'); $table->text('body'); $table->string('image')->nullable(); $table->timestamps(); });

0 likes
13 replies
hjortur17's avatar

I did $table->string('image')->nullable(); in my migration

rin4ik's avatar

but you know it should 'image' => 'image|nullable' also work

hjortur17's avatar

I don't understand :/ I have both 'image' => 'image|nullable' in my validation and also $table->string('image')->nullable(); in my migration

rin4ik's avatar

try this

 'image' => request()->file('image')->storeAs('uploads', 'frettir'),
hjortur17's avatar

Now I'm getting Call to a member function storeAs() on null

rin4ik's avatar

enctype="multipart/form-data" do you have this in your form?

rin4ik's avatar

Controller strore method()

you can use this code. it is good idea to create methods for uploading image. call uploadImage method and pass the image

       $thread = Thread::create([
                 'title' => request('title'),
                 'user_id' => Auth::user()->id,
                 'body' => request('body'),
          ]);
$thread->uploadImage($request->file('image'));
return redirect('/stjornbord')->with('flash', 'Fréttin var búin til!');

your Thread model . if image doesn't you just return nothing. or if image already exists first will be deleted. then image will always have unique name. all your images will be stored in public/uploads directory

Thread.php

 public function uploadImage($image)
    {
        if ($image == null) {
            return;
        }
        $this->removeImage();
        $filename = str_random(10) . '.' . $image->extension();
        $image->storeAs('uploads', $filename);
        $this->image = $filename;
        $this->save();
    }
   public function removeImage()
    {
        if ($this->image != null) {
            Storage::delete('uploads/' . $this->image);
        }
    }

1 like
Snapey's avatar

put nullable first in validation. I think the rules are executed in order

hjortur17's avatar

@rin4ik It did work to create a public function inside Thread model but I post an error message but still save the Thread to the database.

Undefined variable: thread

elcultivo's avatar
Level 6

The problem is because you are trying to call a method that belongs to the File class in a null variable.

Try this when you create the thread.


Thread::create([
    'title' => request('title'),
    'user_id' => Auth::user()->id,
    'body' => request('body'),
    'image' => $request->hasFile('image') ? request()->file('image')->store('frettir', 'public') : null,
]);

Please or to participate in this conversation.