Hello,

This is laravel. I wonder why after pressing, Upload Image button nothing happen. I doubt that it even pass the controller. Any clue?

setting.blade.php

<div id="myModal" class="modal fade">
<div class="modal-dialog">
    <div class="modal-content">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
             <h4 class="modal-title">Please input a new background</h4>
        </div>

        <form method="post" enctype="multipart/form-data" action="{{ URL::to('/home/upbprofile') }}">
        <input type="hidden" name="_token" id="_token" value="<?php echo csrf_token() ?>">

        <div class="modal-body">                            
            <input type="file" name="b_pic">
        </div>
        <div class="modal-footer">
            <input type="submit" value="Upload Image" class="btn btn-primary" data-dismiss="modal">
        </div> 
        </form>
    </div>
    <!-- /.modal-content -->
</div>
<!-- /.modal-dialog -->
</div>

routes.php

 Route::post('/home/upbprofile', 'SettingController@UploadBackgroundProfile');

SettingController.php

public function UploadBackgroundProfile(Request $request)
{

    $b_pic = $request->file('b_pic');

    dd($b_pic);

    if($b_pic != NULL)
        $b_pic->move('uploads/bgprofile', $b_pic->getClientOriginalName());

    if($b_pic != NULL)
    {   
        // simpan yg baru
        $model->bgprofile = $b_pic->getClientOriginalName();
    }

    $model->save();

    return view('soulfy.setting');
}     
Member Avatar for diafol

Where does $model come from? What is dd() ?

Here I update my controller:

Still nothing happen when I press Upload Image.

SettingController.php

public function UploadBackgroundProfile(Request $request)
{

    $b_pic = $request->file('b_pic');

    $id = $request->input('id');

    $model = UserbgProfile::find($id);

    if($b_pic != NULL)
        $b_pic->move('uploads/bgprofile', $b_pic->getClientOriginalName());

    if($b_pic != NULL)
    {   
        // simpan yg baru
        $model->bgprofile = $b_pic->getClientOriginalName();
    }

    $model->save();

    return view('soulfy.setting');
}

UserbgProfile.php

 <?php

 namespace Soulfy;

 use Illuminate\Database\Eloquent\Model;

 class UserbgProfile extends Model
{
//
protected $table = "user_bgprofile";
}
Member Avatar for diafol

Checking if a file has been uploaded:

if ($request->hasFile('b_pic')) {
    //
}

Instead of:

if($b_pic != NULL) {   
   //
 }

Storing the file should be done like this:

 $path = $request->b_pic->store('uploads/bgprofile');

All this and more can be found on this page: https://laravel.com/docs/5.4/requests#storing-uploaded-files

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.