Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

segun's avatar
Level 1

i want to save an image into database along with the user_id someone please help me out

the following is my controller and that doesn't work , thanks in advance.

public function store(Request $request) {

    $image = new User;
    $image->user_id = Auth::user()->id;
    
    if($request->hasFile('image')) {
        $file = Input::file('image');
        //getting timestamp
        $timestamp = str_replace([' ', ':'], '-', Carbon::now()->toDateTimeString());
        
        $name = $timestamp. '-' .$file->getClientOriginalName();
        
        $image->filePath = $name;

        $file->move(public_path().'/images/', $name);
    }
    $image->save();
    return  'Image Uploaded Successfully';
    

}
0 likes
32 replies
segun's avatar
Level 1

yes and it is

Class 'App\Http\Controllers\Auth' not found

1 like
DmitriySemenov's avatar

put at the top instead 'App\Http\Controllers\Auth'

use Illuminate\Support\Facades\Auth;

1 like
vipin93's avatar

on the top of controller "use Auth;"

irsyadadl's avatar

Do this simple

$image = new User;
$image->user_id = auth()->user()->id;
1 like
segun's avatar
Level 1

@DmitriySemenov i got his error: ErrorException in GuardsAttributes.php line 188: array_flip() expects parameter 1 to be array, string given

1 like
segun's avatar
Level 1

sorry my new error now is : MethodNotAllowedHttpException in RouteCollection.php line 233:

here is my form:

Uplaod proof of payment after paying.

File input {{csrf_field()}}
                                  </div>
                                  <button type="submit" class="btn btn-default">Upload</button>
                                </form>

and this is my web.php route:

Route::get('store', 'HomeController@store' );

1 like
DmitriySemenov's avatar

i need your full form

try Route::post('store', 'HomeController@store' );

1 like
irsyadadl's avatar
Route::post('store', 'HomeController@store' );
1 like
segun's avatar
Level 1

@IrsyadAdl @vipin93 @DmitriySemenov please someone help me check the original code at the top is that how to upload an image with user_id, i just keep on getting one error to the other please help me check the code bless you and thanks in advance. I am a new laravel developer.

1 like
vipin93's avatar

here is quite confusing u want to use user_id in User model ,

    $image = new User;
    $image->user_id = Auth::user()->id;

i guess u doing wrong if user have model of image then it should be like

    $image = new Image;
    $image->user_id = Auth::user()->id;

and image belongs to User , and post your User table

1 like
segun's avatar
Level 1

i have change that since

1 like
vipin93's avatar

if u have relationship in your User model like(if User have one image then change hasmany to hasone)

public function images()
{
   
        return $this->hasMany(Image::Class);
 
}

and in your Image model


    public function owner()
    {
        return $this->belongsTo(User::Class,'user_id');
    }

and if u make user_id as fillable then remove that from protected $fillable and in your controller simply do like

 
    $user = Auth::user();
    
    if($request->hasFile('image')) {
        $file = Input::file('image');
        //getting timestamp
        $timestamp = str_replace([' ', ':'], '-', Carbon::now()->toDateTimeString());
        
        $name = $timestamp. '-' .$file->getClientOriginalName();

        $file->move(public_path().'/images/', $name);
    }
    $user->images()->create($name);
    return  'Image Uploaded Successfully';
1 like
segun's avatar
Level 1

@vipin93 after using that code the fellowing is the error i get thanks in advance.

FatalErrorException in Image.php line 11: syntax error, unexpected ''filePath'' (T_CONSTANT_ENCAPSED_STRING), expecting variable (T_VARIABLE)

vipin93's avatar
FatalErrorException in Image.php line 11: syntax error, unexpected ''filePath'' 

follow this line and go your Image.php (model) and show us

segun's avatar
Level 1

@IrsyadAdl @vipin93 @DmitriySemenov i came up with a new code in my controller after installing image intervention but how ever the code didnt show me error and it didnt submit anything

public function store(Request $request) {

    $image = new Image;
        
    $image->user_id = Auth::user()->id;

    if($request->hasFile('imageproof')) {
        $proof = $request->file('imageproof');
        $filename = time().'.'.$proof->getClientOriginalExtension();
        $location = public_path('images/',$filename );
        Image::make($proof)->resize(800, 400)->save($location);
        $image->filePath=$filename;

        
    }
    
       $image->save();
    
      return  'Image Uploaded Successfully';
    

}
bwrice's avatar

First make sure the if statement is triggering by adding an else statement:

if($request->hasFile('imageproof')) {
        $proof = $request->file('imageproof');
        $filename = time().'.'.$proof->getClientOriginalExtension();
        $location = public_path('images/',$filename );
        Image::make($proof)->resize(800, 400)->save($location);
        $image->filePath=$filename
} else {
        return 'Image Proof not found';
}
1 like
segun's avatar
Level 1

my form and web route:

{{csrf_field()}}

Uplaod proof of payment after paying.

File input
                                  </div>
                                  <button type="submit" class="btn btn-default">Upload</button>
                                </form>

Route::post('store', 'HomeController@store' );

vipin93's avatar
vipin93
Best Answer
Level 13

as I said use relationship for saving account and delete user_id from fillable and

 <form method="POST" class="form-inline" enctype="multipart/form-data" action="/store">
           {{ csrf_field() }} 
              <div class="form-group">
              <input name="avatar" class="form-control" type="file" required="" />
              </div>
              <div class="form-group">
               <button type="submit" class="btn btn-primary pull-right">Submit</button>
              </div>
            </form>

controller

public function update_avatar(Request $request)
    {
         $this->validate($request,[

            'avatar' => 'required|image|max:10240',

        ]);

         $image = new Image;  
  //change model name Image to something else because u are using Image as alias  if u not then it will give u a errors 
        
      $image->user_id = Auth::user()->id;


        if ($request->hasFile('avatar'))
         {
            $avatar = $request->file('avatar');
            $filename = time().'.'.$avatar->getClientOriginalExtension();
            Image::make($avatar)->resize(300,300)->save(public_path('image/'.$filename));

            $image->filePath=$filename
            $image->save();
        }
          //flash()->success('Successfully Profile Pic. Updated');

          return back();
    }
1 like
segun's avatar
Level 1

dd($request->avatar);

@vipin93 that didnt print out anything, no error no messages, it just keep on taken me back to the same page

segun's avatar
Level 1

yes is to just check if the form return any data and in my case it return an empty array and i don't know why that happened sir

segun's avatar
Level 1

@vipin93 i really don't get you but i have updated my controller to the your code if tatis what you are asking me and still same problem sir

segun's avatar
Level 1

@vipin93 for a reason i move the code to another controller and it started showing a sign of working by giving rhis error message sir:

FatalErrorException in Image.php line 11: syntax error, unexpected ''filePath'' (T_CONSTANT_ENCAPSED_STRING), expecting variable (T_VARIABLE)

vipin93's avatar

I told u you have to got your model Image and check your line 11 you can show us your Image model

Next

Please or to participate in this conversation.