Saving image to the destination path with Laravel - php

I am making a registration which also includes uploading a photo. Here's my code register controller code.
This the function to store data in the database
protected function create(array $data) //storing data to database
{
$fileName = 'null';
if (Input::file('photo')->isValid()) {
$destinationPath = public_path('public/uploads/files');
$extension = Input::file('photo')->getClientOriginalExtension();
$fileName = uniqid().'.'.$extension;
Input::file('photo')->move($destinationPath, $fileName);
}
return Travelers::create([
'fname' => $data['fname'],
'lname' => $data['fname'],
'gender' => $data['gender'],
'username' => $data['username'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
'birthday' => $data['birthday'],
'photo' => $fileName,
]);
}

you are using public_path() helper. so you don't need to include public/ again.
use
public_path('uploads/files')
instead of
public_path('public/uploads/files');

Related

How to upload picture when register in laravel?

I'm trying to upload an image in laravel default user registration. I'm using laravel ui package.
Here is create method in RegisterController
public function create(array $data) {
if (request()->hasFile('image')) {
$image = request()->file('image')->getClientOriginalName();
request()->file('image')->storeAs('avatars', $image, 'public');
}
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'address' => $data['address'],
'image' => $image ,
'contact' => $data['contact'],
'password' => Hash::make($data['password']),
]);
I'm getting this error
Undefined variable: image"
In the User model:
protected $fillable = [
'name',
'email',
'password',
'image', //add this
'contact', //add this
];

Argument 1 passed to Illuminate\Auth\SessionGuard::login() when adding ROLE during registration in laravel 5.8

I am trying to add default ROLE upon registration of the user. I add ->assignRole() but it gives me this error
here's my create function
use App\HasRoles;
protected function create(array $data)
{
$user = User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
])->assignRole('borrower');
$user_id = $user->id;
// HasRoles::create([
// 'role_id' => 4,
// 'user_id' => $users->id,
// ]);
Referral::find($data['referral_id'])->update ([
'status' => 1,
'date_used' => $data['referral_date_used']
]);
CollectorMember::create ([
'collector_id' => $data['referral_generate_by'],
'borrower_id' => $user_id,
'referral_id' => $data['referral_id'],
]);
return $user;
}
you'll also notice the commented hasRole::create. well I thought that's just like that.
Any suggestions? thanks in advance!
Just add the assignRole() method after create the user.
$user = User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
]);
$user->assignRole('borrower');

Validating image and storing image in specific folder

I am validating and storing image using my Controller.
When I validate image form is not stored in database but when I comment validation it stores in database.
FrontController.php
public function StorePost(Request $request)
{
$formInput = $request->except('image');
$this->validate($request, [
'title' => 'required',
'name' => 'required',
'email' => 'required|email',
'contact' => 'required',
'model' => 'required',
'city' => 'required',
'description' => 'required',
'image' => 'image|mimes:png,jpg,jpeg|max:10000'
]);
//image upload
$image = $request->image;
if($image){
$imageName = $image->getClientOriginalName();
$image->move('images', $imageName);
$formInput['image'] = $imageName;
}
Post::create($formInput);
$posts = Post::all();
return view('ad.ad', compact('name', 'posts'));
}

Copying an ID when registering an user in Laravel 5.3

I want to copy the content of id into owner_id after someone registers.
$table->increments('id');
$table->integer('owner_id');
How do I do this? I have tried this which I obviously expected not to work:
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
...
'owner_id' => $data['id'],
]);
}
Since $data only gives you the form information. I am clueless now. Any ideas?
You need to create user first. Only then you can use it's ID:
$user = User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
$user->update(['ownder_id' => $user->id]);
You can do something like:
$user = User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']);
$user->owner_id = $user->id;
$user->save();
First create the user then update the ownder_id by
DB::getPdo()->lastInsertId(); or $user->id.
Try this,
protected function create(array $data)
{
$user = User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
User::where('id', $user->id)
->update(['ownder_id' => $user->id]);
return $user;
}

Laravel 5.2 | double insert data in single request

I have table dosen (iddosen, user_id, namadosen, notelpdosen, fotodosen, etc)
I try to register a user + upload photo, but it came with 2 row inserted.
all columns are null, except "fotodosen"
all row has data, except "fotodosen".
This is my method :
public function store(CreateDosenRequest $request)
{
$user = User::create([
'name' => $request->input('name'),
'username' => $request->input('username'),
'email' => $request->input('email'),
'password' => $request->input('password'),
'admin' => $request->input('admin'),
]);
$dosen = Dosen::create([
'iddosen' => $request->input('iddosen'),
'nipy' => $request->input('nipy'),
'namadosen' => $user->name,
'user_id' => $user->id,
'alamatdosen' => $request->input('alamatdosen'),
'notelpdosen' => $request->input('notelpdosen'),
'tempatlahirdosen' => $request->input('tempatlahirdosen'),
'tanggallahirdosen' => $request->input('tanggallahirdosen'),
'agamadosen' => $request->input('agamadosen'),
]);
if ($request->hasFile('image')) {
$data = $request->input('image');
$photo = $request->file('image')->getClientOriginalName();
$destination = public_path() . '/uploads/';
$request->file('image')->move($destination, $photo);
$data['fotodosen'] = $photo;
Dosen::create($data);
}
return redirect('admin/dosen')->with('message', 'Data berhasil ditambahkan!');
}
Please correct my code
You are using Dosen::create twice there for a new row for each usage.
append $data to request or try this :
public function store(CreateDosenRequest $request)
{
$user = User::create([
'name' => $request->input('name'),
'username' => $request->input('username'),
'email' => $request->input('email'),
'password' => $request->input('password'),
'admin' => $request->input('admin'),
]);
if ($request->hasFile('image')) {
$data = $request->input('image');
$photo = $request->file('image')->getClientOriginalName();
$destination = public_path() . '/uploads/';
$request->file('image')->move($destination, $photo);
$data['fotodosen'] = $photo;
}
$dosen = Dosen::create([
'iddosen' => $request->input('iddosen'),
'nipy' => $request->input('nipy'),
'namadosen' => $user->name,
'user_id' => $user->id,
'alamatdosen' => $request->input('alamatdosen'),
'notelpdosen' => $request->input('notelpdosen'),
'tempatlahirdosen' => $request->input('tempatlahirdosen'),
'tanggallahirdosen' => $request->input('tanggallahirdosen'),
'agamadosen' => $request->input('agamadosen'),
'fotodosen' => $photo, //you have to add it hear
]);
return redirect('admin/dosen')->with('message', 'Data berhasil ditambahkan!');
}

Categories