do you have some error message?
Mar 7, 2017
32
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';
}
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
Please or to participate in this conversation.