samedi 18 janvier 2020

laravel5 controller issue. Undefined variable: filename

I want to upload a blogger's profile image. But I got an error Undefined variable: file_name. I think it is a controller issue.

I don't know why the variable is not defined, though I declared the same variable name. I restarted the server but it did not work.

So please help me out.

BloggerController.php

public function store(Request $request, User $user_id){

        $user_id = DB::table('users')->where('id', $user_id)->get();

        if($request->hasfile('image')){
            $file = $request->file('image');
            $ext = $file->getClientOriginalExtension();
            $filename = time().'.'.$ext;
            $file->move('bloggers/', $filename);
        }



        $blog = Blog::create(
            ['user_id' => $user_id],
            [
                'image'=>$filename,
                'introduction' => $request->introduction,
            ]
        );
        return redirect(route('blogger'));
    }

I also challenged the store method.

if($request->hasfile('image')){
        $filename = $request->file('image')->getClientOriginalName();
        $request->file('image')->storeAs('bloggers/',$filename);
        }

My migration

Schema::create('blogs', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->integer('user_id')->unsigned();
            $table->foreign('user_id')->references('id')->on('bloggers');
            $table->string('image');
            $table->string('introduction');
            $table->timestamps();
        });

error image



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire