samedi 18 janvier 2020

laravel5 controller issue. Undefined variable: filename

I want to upload a blogger's profile image but I get 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();
        $file_name = time() . '.' . $ext;
        $file->move('bloggers/', $file_name);
    }

    $blog = Blog::create(
        ['user_id' => $user_id],
        [
            'image'        => $file_name,
            'introduction' => $request->introduction,
        ]
    );

    return redirect(route('blogger'));
}

I also changed the store method.

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

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



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2R8DsPD
via IFTTT

Aucun commentaire:

Enregistrer un commentaire