How to add file in laravel through dropify data-default-file - php

Hello i am trying to show the image in the dropify field by fetching the image from database.But i am unable to do so .Kindly check my code.
`
<input type="file" class="dropify" name="profile_pic" data-default-file="{{ url('/uploads/driving/'.$user->profile_pic) }}"/>
THE CONTROLLER IS
public function editUserForm($id)
{
$user = User::find($id);
return view('admin.users.edit-users',compact('user'));
}
public function updateUser(Request $request,$id)
{
$r1 = $request->validate([
'profile_pic' => 'required|image|mimes:jpg,png,jpeg,gif,svg|max:2048|',
'name' => 'required',
'email' => 'required|email',
'city' => 'required',
'phone_no' => 'required',
]);
if ($request->hasFile('profile_pic')) {
$imgName = uniqid() . '.' . $request->profile_pic->getClientOriginalExtension();
}
$user = User::find($id);
$user->name = $request->name;
$user->email = $request->email;
$user->phone_no = $request->phone_no;
$user->profile_pic =$imgName;
$user->city = $request->city;
$user->update();
$path = public_path('uploads/driving');
if ($request->hasFile('profile_pic')) {
$request->profile_pic->move($path, $imgName);
}
return redirect()->route('users')->with('message', 'User info updated successfully!');
}
i created a url data-default-file="{{ url('/uploads/driving/'.$user->profile_pic) }}" but unable to show the image.

Related

Issue with: Upload image Database Laravel "c:\xampp\tmp\php.tmp"

I'm confused again, if im trying to upload a image im my database. The name of the image is given with a tmp file like this "C:\xampp\tmp\phpB001.tmp". What is the solution to this Issue.
Controller: AdminLeistungController.php
public function store(Request $request)
{
$data = $this->_validate($request);
$data['creator_id'] = auth()->user()->id;
if($request->hasfile('image')){
$fileameWithExt = $request->file('image')->getClientOriginalName();
$filename = pathinfo($fileameWithExt, PATHINFO_FILENAME);
$extension = $request->file('image')->getClientOriginalExtension();
$fileNameToStore = $filename . '_' . time() . '.' . $extension;
$path = $request->file('image')->storeAs('uploads/leistungen', $fileNameToStore);
} else {
$fileNameToStore = 'noimage.jpg';
}
Leistungen::create($data);
return redirect(route('admin.leistung.index'))->withSuccess('Successfully');
}
private function _validate($request)
{
$rules = [
'title' => 'required',
'article' => 'required|min:3',
'seo_title' => 'required',
'seo_description' => 'required',
'image' => 'mimes:jpeg,png|max:1014',
];
return $this->validate($request, $rules);
}
public function update(Request $request, Leistungen $leistung)
{
$data = $this->_validate($request);
$leistung->update($data);
return redirect(route('admin.leistung.index'))->withSuccess('Successfully');
}
Controller: create.blade.php
<form enctype="multipart/form-data" action="{{route('admin.leistung.store')}}" method="post" enctype="multipart/form-data" class="col-lg-12">
#include('admin.leistung._form')
...
Controller: _form.blade.php
<input type="file" id="image" name="image" value="{{old('image') ?? $leistung->image ?? ''}}" class="form-control #if($errors->has('image')) is-invalid #endif">

Laravel registration page eero

Please I have a laravel website which have been working fine then all of a sudden when a user uses a referral link to register, the page will reload and redirect back to the registration page. I tried it for few times and it works then I used the new referral link to register another new account and didn't work again ever since then. And its not showing any error message but rather bounce back to same page. I have checked the RegistrationController and all the code is still fine as same before. Please I really need this help.
Thanks in Advance
Below is my RegistrationControl
`public function getRegistrationPage(){
return view('auth.register');
}
public function storeUser(Request $request){
$this->validate($request, [
'name' => 'required|max:30',
'email' => 'required|unique:users',
'phone' => 'required|min:10|unique:users',
'username' => 'required|min:5|unique:users|regex:/^\S*$/u',
'password' => 'required|string|min:6|confirmed',
'country' => 'required',
'state' => 'required',
]);
$email_code = strtoupper(Str::random(6));
$email_time = Carbon::parse()->addMinutes(5);
$phone_code = strtoupper(Str::random(6));
$phone_time = Carbon::parse()->addMinutes(5);
$upliner = Cookie::get('referral');
$uplinerUsername = Cookie::get('referral');
$email_verify = 1;
$phone_verify = 1;
$user = new User();
if($upliner == null){
$upliner = 0;
}
else{
$chkUserRef = User::where('reference', $upliner)->first();
if($chkUserRef == null){
return redirect('register');
}else{
$user_id = $chkUserRef->id;
$upliner = $user_id;
}
}
$data['name'] = $user->name = $request['name'];
$data['email'] =$user->email = $request['email'];
$user->phone = $request['phone'];
$data['username'] = $user->username = $request['username'];
$user->reference = $request['username'];
$user->country = $request['country'];
$user->state = $request['state'];
$user->under_reference = $upliner;
$user->email_verify = $email_verify;
$user->email_code = $email_code;
$user->email_time = $email_time;
$user->phone_verify = $phone_verify;
$user->phone_code = $phone_code;
$user->phone_time = $phone_time;
$data['password'] = $user->password = bcrypt($request['password']);
$saved = $user->save();
if($saved){
//send mail to the registered user
Mail::send('emails.welcome-email', ['data' => $data], function ($message) use ($data) {
$message->from('noreply#example.com', 'example.com')
->to($data['email'], $data['username'], $data['name'])
->subject('Welcome Email');
});
//send mail to Admin
$data['adminEmail'] = "example#gmail.com";
Mail::send('emails.registration-notification', ['data' => $data], function ($message) use ($data) {
$message->from('noreply#example.com', 'example.com')
->to($data['adminEmail'], $data['username'], $data['email'], $data['name'])
->subject('User Registeration Notification');
});
//send mail to referrer
if($uplinerUsername != null){
$chkReff = User::where('username',$uplinerUsername)->first();
if($chkReff == null){
return redirect('register');
}
else{
//get the person's details and send mail
$userReff = User::where('username',$uplinerUsername)->first();
$data['userEmail'] = $userReff->email;
$data['refUsername'] = $userReff->username;
Mail::send('emails.referral-notification', ['data' => $data], function ($message) use ($data) {
$message->from('noreply#example.com', 'example.com')
->to($data['userEmail'], $data['username'],
$data['refUsername'], $data['userEmail'], $data['email'], $data['name'])
->subject('Referral Notification');
});
}
}
return redirect('login');
}
`

How can I update image in edit view in laravel?

In laravel I have a field in my edit view named Profile Picture, whenever I click On edit button I got all the values from database in edit view but I don't get image, And If whenever I click on submit button everytime I have to upload image without that I can't process further I want If I not upload new image then form will automatic consider old profile pic
my blade file is like
<div class="col-md-6">
<div class="form-group">
<label for="photo">Profile Picture :<span class="danger">*</span> </label>
<div class="row">
<div class="col-md-9">
<input type="file" class="form-control" id="file" name="file">
</div>
<div class="col-md-3">
#foreach ($empProfilePic as $empProfilePicture)
#if($employee->id == $empProfilePicture->id)
<img src="uploads/images/{{ $empProfilePicture->file }}" id="profile-img-tag" height="100" width="100">
#endif
#endforeach
</div>
</div>
</div>
</div>
Controller File
public function updateEmployee(Request $request, $id)
{
$employee = User::find($id);
//Get inputs for personal detail
$firstName = $request->get('firstName');
$middleName = $request->get('middleName');
$lastName = $request->get('lastName');
$gender = $request->get('gender');
$city = $request->get('city');
$state = $request->get('state');
$localAddress = $request->get('localAddress');
$permanentAddress = $request->get('permanentAddress');
$personalEmail = $request->get('personalEmail');
$mobileNumber = $request->get('mobileNumber');
$companyEmail = $request->get('companyEmail');
$empId = $request->get('empId');
$department = $request->get('department');
$designation = $request->get('designation');
$status = $request->get('status');
$totalExperience = $request->get('totalExperience');
$aboutMe = $request->get('aboutMe');
$roleName = $request->get('role');
$role = $request->get('role');
if ($role == 'hr')
{
$role = '5c8a51ed650fbd5398503043';
}
else
{
$role = '5c8a51ed650fbd5398503044';
}
// //Store Image In Folder
$file = $request->file('file');
$name = $file->getClientOriginalName();
$file->move('uploads/images', $name);
if (file_exists(public_path($name = $file->getClientOriginalName())))
{
unlink(public_path($name));
};
$accountHolderName = $request->get('accountHolderName');
$accountNumber = $request->get('accountNumber');
$bankName = $request->get('bankName');
$ifscCode = $request->get('ifsc_code');
$panNumber = $request->get('panNumber');
$branchName = $request->get('branchName');
//Enter in database
$employee->role_id = $role;
$employee->role_name = $roleName;
$employee->username = $firstName;
//Update Image
$employee->file = $name;
$employee->personal_email = $personalEmail;
$employee->company_email = $companyEmail;
$employee->status = $status;
$personalDetail = ([
'emp_id' => $empId,
'first_name' => $firstName,
'middle_name' => $middleName,
'last_name' => $lastName,
'gender' => $gender,
'city' => $city,
'state' => $state,
'local_address' => $localAddress,
'permanent_address' => $permanentAddress,
'personal_email' => $personalEmail,
'mobile_number' => $mobileNumber,
'department' => $department,
'designation' => $designation,
'total_experience' => $totalExperience,
'about_me' => $aboutMe,
]);
$bankDetail = ([
'account_holder_name' => $accountHolderName,
'account_number' => $accountNumber,
'bank_name' => $bankName,
'ifsc_code' => $ifscCode,
'pan_number' => $panNumber,
'branch_name' => $branchName,
]);
$employee->status = $status;
$employee->personal_detail = $personalDetail;
$employee->bank_detail = $bankDetail;
$employee->save();
return redirect('list-of-employees')->with('Success', 'Data Updated Successfully!');
}
Will anyone will help, Thank you in advance
In controller:
Code for update file:
Its update only when you select a file for the update.
Here I use the Employee model you can replace with your model
public function update(Request $request, $id){
$employee = Employee::find($id);
if($request->file != ''){
$path = public_path().'/uploads/images/';
//code for remove old file
if($employee->file != '' && $employee->file != null){
$file_old = $path.$employee->file;
unlink($file_old);
}
//upload new file
$file = $request->file;
$filename = $file->getClientOriginalName();
$file->move($path, $filename);
//for update in table
$employee->update(['file' => $filename]);
}
}
Try this. If you get the file in post method, only then set the file variable of an employee.
// //Store Image In Folder
if (isset($_FILES['file'])) {
$file = $request->file('file');
$name = $file->getClientOriginalName();
$file->move('uploads/images', $name);
if (file_exists(public_path($name = $file->getClientOriginalName())))
{
unlink(public_path($name));
};
//Update Image
$employee->file = $name;
}
//Enter in database
$employee->role_id = $role;
$employee->role_name = $roleName;
$employee->username = $firstName;
Make the image input optional (don't add required) on the blade file and on the validation (controller) then check before updating as:
if ($request->hasFile('image')) {
// Perform image update
}

Laravel Validation is validating data but then redirecting to the same page with the inputs without executing the following code

Here is my code for validation.
public function store(Request $request)
{
$validator = Validator::make($request->all(), [
'email' => 'required|email|unique:users',
'phone' => 'required|unique:users',
'user_name' => 'required|unique:users',
'operator_name' => 'required|max:255',
'operator_nid' => 'required|numeric|unique:operators',
'operator_password' => 'required',
'operator_gender' => 'required',
'operator_birthday' => 'required',
]);
if ($validator->fails()) {
return redirect('operator/create')
->withErrors($validator)
->withInput();
}
$user = new User;
$user->name = $request->operator_name;
$user->email = $request->email;
$user->phone = $request->phone;
$user->user_name = $request->user_name;
$user->password = bcrypt($request->password);
$user->type = 3;
$user->save();
$operator = new Operator;
$operator->operator_name = $request->operator_name;
$operator->operator_email = $request->email;
$operator->operator_phone = $request->phone;
$operator->operator_nid = $request->operator_nid;
$operator->operator_user_name = $request->user_name;
$operator->user_id = $user->id;
$operator->type = 3;
$operator->operator_gender = $request->operator_gender;
$operator->operator_birthday = $request->operator_birthday;
$operator->operator_occupation = $request->operator_occupation;
$operator->operator_facebook = $request->operator_facebook;
$operator->operator_twitter = $request->operator_twitter;
$operator->operator_gplus = $request->operator_gplus;
$operator->operator_address = $request->operator_address;
if ($request->hasfile('operator_pro_pic')){
$image = $request->file('operator_pro_pic');
$filename = time() . '.' . $image->getClientOriginalExtension();
$location = public_path('images/operator/' . $filename);
Image::make($image)->resize(950, 700)->save($location);
$operator->operator_pro_pic = $filename;
}
$operator->save();
return $user;
}
I am trying to create an operator and a user at the same time. email, phone and user_name should be unique in the user's table and other data will be validate from the operators table. Validation is working as it is giving me the errors but after validating it's not going further. So my code after the validation is not executing. What is the possible reason for this?
You can use -
if ($validator->fails()) {
return back()->withInput()
->withErrors($validator);
}
The better way will be to create a request file by using
php artisan make:request requestName
define all the validation in there. In this case if your validation fails the application will return back with error and old-inputs without reaching to the controller.

How to upload my file and photo and add to databases

I want to upload a picture.
I wrote a codes for this ... but this photo is not added to the database at all. And Just, "else" is executed.
public function store(Request $request){
//Get Request Input
$name = $request ->input('name');
$description = $request ->input('description');
$cover_image = $request ->file('cover_image');
$owner_id = 1;
//Check Image Upload
if($cover_image)
{
$cover_image_filename = $cover_image -> getClientOriginalName();
$cover_image -> move(public_path('images'), $cover_image_filename);
}
else{
$cover_image_filename = 'noimage.jpg';
}
//Insert Gallery
DB::table('galleries')-> insert(
[
'name' => $name,
'description' => $description,
'cover_image' => $cover_image_filename,
'owner_id' => $owner_id
]
);
//Redirect
return \Redirect::route('gallery.index') -> with('message', 'Gallery Created');
}`
what's the wrong?
1) Make sure you have added enctype="multipart/form-data" in your form and a <input type="file"> with field name="cover_image"
2) Create a new folder named images in your laravel public folder.
3) In your controller
public function store(Request $request){
//Get Request Input
$name = $request ->input('name');
$description = $request ->input('description');
$owner_id = 1;
//Check Image Upload
if( $request->hasFile('cover_image')) {
$cover_image = $request->file('cover_image');
$path = public_path(). '/images/';
$cover_image_filename = $cover_image->getClientOriginalName();
$cover_image->move($path, $cover_image_filename);
}
else{
$cover_image_filename = 'noimage.jpg';
}
//Insert Gallery
DB::table('galleries')-> insert([
'name' => $name,
'description' => $description,
'cover_image' => $cover_image_filename,
'owner_id' => $owner_id
]);
//Redirect
return \Redirect::route('gallery.index') -> with('message', 'Gallery Created');
}
Hope it's helpful.

Categories