I got this error
"Method Illuminate\Database\Eloquent\Collection::save does not exist."
when i'm trying to Register User API.
i dont know what 's wrong with it...
My AuthController
public function register(Request $request)
{
$request->validate([
'name' => 'required|min:3',
'email' => 'required|email',
'password' => 'required|between:6,25',
]);
$user = User::all();
$user->name = $request->name;
$user->email = $request->email;
$user->password = bcrypt($request->password);
$user->save();
return response()->json([
'registered' => true,
'token' => $user->createToken('token')->accessToken
]);
}
Any help? Thanks....
$user = User::all(); return a collection of all users in database, you should replace it with $user = new User;, which return a new User object.
Change code to
public function register(Request $request)
{
$request->validate([
'name' => 'required|min:3',
'email' => 'required|email',
'password' => 'required|between:6,25',
]);
$user = new User();
$user->name = $request->name;
$user->email = $request->email;
$user->password = bcrypt($request->password);
$user->save();
return response()->json([
'registered' => true,
'token' => $user->createToken('token')->accessToken
]);
}
Related
so I have this store function at my Controller
public function store(Request $request)
{
Penghuni::create([
'nama_penghuni' => $request->nama_penghuni,
'email' => $request->email,
'phone' => $request->phone,
'tower' => $request->tower,
'no_unit' => $request->no_unit
]);
User::create([
'name' => $request->nama_penghuni,
'email' => $request->email,
'password' => Hash::make($request['password']),
'role' => 'penghuni',
]);
return redirect(route('penghuni.index'));
}
what I want is make sure both insert is success, because the current result I got is when Penghuni create is done but the user is fails it keeps getting redirected
hope someone can help, I use laravel 5.8
thank you
This Code is Perfect check other things.
public function store(Request $request)
{
Penghuni::create([
'nama_penghuni' => $request->nama_penghuni,
'email' => $request->email,
'phone' => $request->phone,
'tower' => $request->tower,
'no_unit' => $request->no_unit
]);
User::create([
'name' => $request->nama_penghuni,
'email' => $request->email,
'password' => Hash::make($request['password']),
'role' => 'penghuni',
]);
return redirect(route('penghuni.index'));
}
1. Model
Penghuni and user Model must added this Line
protected $guarded = [];
Other Solution
public function store(Request $request)
{
$penghuni = new Penghuni;
$penghuni->nama_penghuni = $request->nama_penghuni;
$penghuni->email = $request->email;
$penghuni->phone = $request->phone;
$penghuni->tower = $request->tower;
$penghuni->no_unit = $request->no_unit;
$penghuni->save();
$user = new User;
$user->name = $request->nama_penghuni;
$user->email = $request->email;
$user->password = Hash::make($request->password);
$user->role = 'role';
$penghuni->users()->save($user);
return redirect(route('penghuni.index'));
}
You can wrap your queries in a database transaction like so:
DB::transaction(function () use ($request) {
// queries here
});
return redirect(route('penghuni.index'));
Or something like this, depending on your use-case.
DB::beginTransaction();
try {
// queries here
// all good
DB::commit();
return redirect(route('penghuni.index'));
} catch (\Exception $e) {
// something went wrong
DB::rollback();
}
You can read more about database transaction here: https://laravel.com/docs/8.x/database#database-transactions
Iam trying to register using passport api authentication. but I got an error
"message": "Trying to get property 'id' of non-object",
"exception": "ErrorException",
public function register(Request $request)
{
$credentials = $request->only('name', 'email', 'password');
$rules = [
'name' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'password' => 'required',
];
$validator = Validator::make($credentials, $rules);
if($validator->fails()) {
return response()->json(['success'=> false, 'error'=> $validator->errors()]);
}
$name = $request->name;
$email = $request->email;
$password = $request->password;
$user = User::create(['name' => $name, 'email' => $email, 'password' => bcrypt($password)]);
$data['token'] = $user->createToken("MyApp")->accessToken();
$data['name'] = $user->name;
return response()->json(['success'=> true, 'message'=> $data]);
}
how to solve it?
you can run again:
php artisan passport:install
I have created a form where the admin can register accounts on the system. The problem is that the password is not hashed when the user is created on the admin side, and I think this is what stops the user from logging in on the system. Do you have any tips for me to make this work? I'm a total newbie at Laravel so.
I think the problem is in this code..
public function store(Request $request)
{
$this->validate(request(), [
'firstname' => 'required|string|max:255',
'lastname' => 'required|string|max:255',
'username' => 'required|string|max:25|unique:users',
'phone' => 'required|string|max:12|min:11|unique:users',
'address' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users',
'password' => 'required|string|min:8|confirmed',
'membership_term' => 'required|string|max:255',
'mode_of_payment' => 'required|string',
]);
$user = new User;
$user->firstname = $request->input('firstname');
$user->lastname = $request->input('lastname');
$user->username = $request->input('username');
$user->phone = $request->input('phone');
$user->address = $request->input('address');
$user->email = $request->input('email');
$user->password = $request->input('password');
$user->membership_term = $request->input('membership_term');
$user->mode_of_payment = $request->input('mode_of_payment');
$user->save();
return back()->with('success', 'Data Updated.')->with('users', $user);
}
Try This
use Illuminate\Support\Facades\Hash;
$user->password = Hash::make($request->input('password'));
You can use laravel helper function bcrypt()
$user->password = bcrypt($request->input('password'));
ref link https://laravel.com/docs/6.x/helpers#method-bcrypt
I want to store data through api. It's working but problem is when I add validation it does not give me corresponding message . How can I fix it? Thanks in advance
Here is my route
Route::post('api/add_user', 'TestApiController#store');
Here is my controller
public function store(Request $request)
{
$validation = Validator::make(Request::all(), [
'name' => 'required',
'phone' => 'required',
'email' => 'required'
]);
if ($validation->errors()) {
return $errors->toJson();
} else {
$testApi = new testApi();
$testApi->name = $request->name;
$testApi->phone = $request->phone;
$testApi->email = $request->email;
$testApi->save();
return "ok";
}
}
to handle that your method should be like this :
public function store(Request $request)
{
$validator = Validator::make($request->all(), [
'name' => 'required',
'phone' => 'required',
'email2' => 'required|email'
]);
if($validator->fails()){
// here we return all the errors message
return response()->json(['errors' => $validator->errors()], 422);
}
$testApi = new testApi();
$testApi->name = $request->name;
$testApi->phone = $request->phone;
$testApi->email = $request->email;
$testApi->save();
// 201 http code means that the server has proceced your request correctly
return response()->json([], 201);
}
You don't have to manually do this. simply
public function store(Request $request)
{
$request->validate([
'name' => 'required',
'phone' => 'required',
'email' => 'required'
]);
$testApi = new testApi();
$testApi->name = $request->name;
$testApi->phone = $request->phone;
$testApi->email = $request->email;
$testApi->save();
return "ok";
}
this will automatically handles validation and returns error message when invalid.
Update
if you wanna stick with your approach. this is where you need to change.
if ($validation->fails()) {
return $validation->errors();
}
Here's my store function. I've tried playing with it a few different ways. For some reason I thought maybe putting the $post->save(); inside the if would at least change the profile image, but no. Not even that works. I initially tried return redirect()->route('profile')->withUser($user); and that didn't work either. This is my latest attempt. And still no luck so I thought to post it here. Thanks in advance! Using: Laravel 5.4
public function store(Request $request)
{
$this->validate($request, array(
'description' => 'required|string',
'projects' => 'required|string',
'experience' => 'required|string',
'links' => 'required|string',
'status' => 'required|string',
));
$userInfo = new User_Info;
$user_id = Auth()->user()->id;
$user = User::find($user_id);
$userInfo->user_id = $user_id;
$userInfo->description = $request->input('description');
$userInfo->projects = $request->input('projects');
$userInfo->experience = $request->input('experience');
$userInfo->links = $request->input('links');
$userInfo->status = $request->input('status');
$userInfo->save();
return redirect()->route('profile')->withUser($user);
}
public function settings($id)
{
$user = User::find($id);
return view("profile.settings")->withUser($user);
}
public function update(Request $request, $id)
{
$user_id = Auth()->user()->id;
$this->validate($request, array(
'first_name' => 'required|string|max:255',
'last_name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users',
'type' => 'required|string',
'description' => 'required|string',
'projects' => 'required|string',
'experience' => 'required|string',
'links' => 'required|string',
'status' => 'required|string'
));
if($request->hasFile('avatar')){
$avatar = $request->file('avatar');
$filename = time() . '.' . $avatar->getClientOriginalExtension();
Image::make($avatar)->resize(300, 260)->save( public_path('/images/uploads/avatars/' . $filename ) );
$user = Auth::user();
$user->avatar = $filename;
}
$user = User::find($id);
$user->first_name = $request->input('first_name');
$user->last_name = $request->input('last_name');
$user->email = $request->input('email');
$user->type = $request->input('type');
$user_info = DB::select("SELECT * FROM user_infos WHERE user_id = $user_id");
$user_info->description = $request->input('description');
$user_info->projects = $request->input('projects');
$user_info->expereince = $request->input('experience');
$user_info->status = $request->input('status');
$user->save();
$user_info->save();
return view('profile.profile')->withUser($user);
}
You don't get existing object, so change this:
$user_info = DB::select("SELECT * FROM user_infos WHERE user_id = $user_id");
To something like this:
$user_info = UserInfo::where('user_id', $user->id)->first();