File upload not working

Published 4 weeks ago by aondoe

Hello everyone.

I'm having trouble with file upload in laravel 5.6 I'm trying to upload images to my CMS I've created with Laravel.

I have a table called "post_its" below:

    public function up()
    {
        Schema::create('post_its', function (Blueprint $table) {
            $table->increments('id');
            $table->string('title');
            $table->text('content');
            $table->text('subject');
            $table->text('featured_img');
            $table->timestamps();
        });
    }

I had to add a column in the table to upload images so I did so like below:

    public function up()
    {
        Schema::table('post_its', function(Blueprint $table)
        {   
            //add a column to the post_its table and give it 100 characters. Also specify 100 characters
            $table->string('featured_img', 100)->nullable();
        });
    }

Here's my form data I'm trying to fix, as you can see below:

<form action="{{route('posts.update', $post->id)}}" method="POST" type="multipart/form-data">
<!--other form fields are here as well, but omitted for brevity-->
        <div class="form-group">
                <input type="file" name="featured_img" title="Featured image">
                
        </div>
</form>

Every other field works and updates the database table, but not this upload field. All the fields under the featured_img column says null. I was hoping after I added this column I could edit this column. Any suggestions?

Best Answer (As Selected By aondoe)
Digitalized

Are you updating the Post model via mass assignment in the Controller? If so, make sure you have included featured_img in the $fillable property of your model

Dry7
Dry7
4 weeks ago (152,030 XP)

@aondoe can you provide code from controller?

Digitalized

Are you updating the Post model via mass assignment in the Controller? If so, make sure you have included featured_img in the $fillable property of your model

knubbe82

you don't have {{csrf_field()}} in your form or @csrf

tykus
tykus
4 weeks ago (690,940 XP)

What are you doing in the controller action to handle the uploaded file?

aondoe

Hey guys thanks for your responses. I omitted the @csrf token from my form above. I actually include that right below the tag.

Controller code below:

    public function update(Request $request, PostIt $post)
    {
        //The code below will update the post/record in the database.
        request()->validate([
                    'title' => 'required|min:2',
                      'content' => 'required|min:50',

                  ]);
                  
        $post->update($request->all());

        session()->flash('message', 'Your post has been created successfully');
        return redirect()->back();
    }

Here's the code from the Model:

namespace App;

use Illuminate\Database\Eloquent\Model;

class PostIt extends Model
{

    protected $fillable=['title','content', 'subject','featured_image'];

    protected $guarded=[];//For guarded data if needed


    //
}
tykus
tykus
4 weeks ago (690,940 XP)

You are doing nothing to handle the uploaded file. What were you actually hoping to achieve; saving the image as a BLOB?

aondoe

Hey tykus. No I was following online examples, and I just wanted it to be stored inside the storage/app/public/ directory

tykus
tykus
4 weeks ago (690,940 XP)
public function update(Request $request, PostIt $post)
{
    request()->validate([
        'title' => 'required|min:2',
        'content' => 'required|min:50',
    ]);

    // Store the file are get the path 
    $path = $request->file('featured_img')->store(public_path());
                  
    $post->update(array_merge($request->all(), ['featured_img' => $path]));

    session()->flash('message', 'Your post has been created successfully');
    return redirect()->back();
}
aondoe

Hey tykus thanks for your prompt response and assist, but I'm getting a

"Call to a member function store() on null" error, on this line:

 $path = $request->file('featured_img')->store(public_path());
       

I suspect the request isn't to the proper file. I'm not sure actually. But I thank you for your help. I will keep working at it.

tykus
tykus
4 weeks ago (690,940 XP)

Be sure that the input name attribute has the same value as the $request->file() parameter.

Cronix
Cronix
4 weeks ago (786,650 XP)

Also make sure you select a file to upload. This code assumes there will be a file present. If the file is required, you should also add a required validation rule for it otherwise it will produce that same error.

Or check if request has the file and only save it then. if ($request->hasFile('fileInputName')) etc

aondoe

Hey you guys are awesome. This framework is awesome, and I'm working constantly to get better. I finally got it to work. My $fillable variable in my model didn't match the column I was trying to upload to.

I had 'featured_image' instead of 'featured_img'.

So now I'm able to upload finally! Thank you all.

Please sign in or create an account to participate in this conversation.