I want to Update this data in database.
error :
ErrorException
Creating default object from empty value
i get error in this line :
$user->name = $request->name;
My Controller code is :
public function update(Request $request, $id)
{
$this->validate($request, [
'name' => 'required|max:255',
'mobile' => 'required|numeric|regex:/(0)[0-9]/|not_regex:/[a-z]/|digits:11',
'national_code' => 'required|numeric|regex:/(0)[0-9]/|not_regex:/[a-z]/|digits:10',
'avatar' => 'required|mimes:jpg,jpeg,png',
]);
$id = (int)$id;
$user = User::findOrFail($id);
$user->name = $request->name;
$user->mobile = $request->mobile;
$user->national_code = $request->national_code;
$user->province = $request->province;
$user->city = $request->city;
$user->address = $request->address;
$user->postcode = $request->postcode;
$user->active = $request->active;
$user->avatar = $request->avatar;
if(! is_null($request->password)) {
$request->validate([
'password' => ['required', 'string', 'min:8', 'confirmed'],
]);
$user->password = $request->password;
}
$user->active = $request->active ? 1 : 0;
if ($request->role_id) {
$user->syncRoles($request->role_id);
}
$user->save();
$notification = array(
'message' => 'Success !',
'alert-type' => 'success'
);
return redirect(route('management.users.index'))->with($notification);
}
Route :
Route::resource('users', 'UserController');
Thank you
Try this:
$user = User::find($id)
or this:
$user = User::where('id', $id)->first();
And check if in your User Model:
protected $fillable = [
'name','mobile', 'national_code', 'city', 'province', 'address',
'postcode', 'active', 'avatar',
];
Related
I'm creating a user registration form. I create a form in the Component. when the user registers he redirects to the user page where he sees all users. when he wanted to edit or update something from in his details he redirects to the same registration form page but this will be a new URL and new Title. I'm getting an undefined variable $title and $url error. when I pass data from the controller to view I get this error.
Registration Form Controller
public function create(Request $request)
{
$url = url('/register');
$title = ("Registration Form");
$data = compact( 'url');
return view('RegistrationForm')->with($data);
}
public function store (Request $request)
{
$request->validate(
[
'firstname' => 'required',
'lastname' => 'required',
'email' => 'required|email',
'password' => 'required',
'confirm_password' => 'required|same:password|min:8',
'address' => 'required',
'country' => 'required',
'state' => 'required',
'city' => 'required',
'gender' => 'required',
'tearms_and_conditions' => 'required',
],
[
'firstname.required' => 'Please enter your First Name',
'lastname.required' => 'Please nter your Last Name',
'email.required' => 'Please enter an Email',
'password.required' => 'Please Enter a Password'
],
);
$users = new Users;
$users->firstname = $request['firstname'];
$users->lastname = $request['lastname'];
$users->email = $request['email'];
$users->password = md5($request['password']);
$users->address = $request['address'];
$users->country = $request['country'];
$users->state = $request['state'];
$users->city = $request['city'];
$users->gender = $request['gender'];
$users->date_of_birth = $request['date_of_birth'];
$users->save();
return redirect('/register/view');
}
public function view (Request $request)
{
$users = Users::all();
$data = compact('users');
return view('user-view')->with($data);
}
public function delete($id)
{
$user = Users::find($id);
echo "<pre>";
print_r ($user);
die;
return redirect('/register/view');
}
public function edit($id)
{
$user = Users::find($id);
if (is_null($user))
{
return redirect('/register/view');
}
else
{
$url = url("user/update"."/". $id);
$title = "Update Details";
$data = compact('user', 'url', 'title');
return redirect('/register')->with($data);
}
}
public function update($id, Request $request)
{
$user = Users::find($id);
$users->firstname = $request['firstname'];
$users->lastname = $request['lastname'];
$users->email = $request['email'];
$users->address = $request['address'];
$users->country = $request['country'];
$users->state = $request['state'];
$users->city = $request['city'];
$users->gender = $request['gender'];
$users->date_of_birth = $request['date_of_birth'];
$users->save();
return redirect('register/view');
}
}
Route
Route::get('/register', [RegistrationFormController::class, 'index']);
View
<body>
<h1> {{$title}} </h1>
<x-registration-form-component :country="$country" :url="$url" />
RegisreationFormComponent
You have to pass the $data variable as the second argument of the view() method:
public function index ()
{
$url = url('/register');
$title = "Registration Form";
$data = compact('title', 'url');
return view('RegistrationForm', $data);
}
First, to answer your question:
When you use the compact method, you are placing those values in an array. You would need to call them like so:
{{ $data['title'] }}
{{ $data['url'] }}
However, you could clean the code up a little bit like this:
public function index()
{
return view('RegistrationForm', [
'url' => url('/register'),
'title' => 'Registration Form',
]);
}
You can then use them in the blade file like so:
{{ $url }}
{{ $title }}
This is my API register method for taking new user registration into the Database.
public function register(Request $request)
{
$user = new User();
$user->name = $request->input('name');
$user->email = $request->input('email');
$user->password = Hash::make($request->input('password'));
if($user->save()){
response (['result' => true]);
}
return response(['result' => false,]);
}
Now when I register from the same email Id, I am getting errors like below
Illuminate\Database\QueryException: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'pkyadav#gmail.com' for key 'users_email_unique' (SQL: insert into `users` (`name`, `email`, `password`, `updated_at`, `created_at`) values (Pramod, pkyadav#gmail.com, y$WJJrNjB8K/jGSXpPagVjSujZcifKXrzs3gnvPcjSK3W1c.IekaBna, 2021-12-16 07:46:56, 2021-12-16 07:46:56))
I want to get a false result response on a duplicate email registration
You can add Validation using Exists:
Example: $user->where('email',$request->input('email'))->exists()
See the below code:
public function register(Request $request)
{
$user = new User();
if(!$user->where('email',$request->input('email'))->exists()){
$user->name = $request->input('name');
$user->email = $request->input('email');
$user->password = Hash::make($request->input('password'));
if($user->save()){
return response(['result' => true]);
}else{
return response(['result' => false]);
}
}
return response(['result' => false,]);
}
You can use Laravel validation:
public function register(Request $request)
{
$validated = $request->validate([
'email' => 'required|unique:users',
]);
if($validation->fails()){
return response()->json($validation->errors());
}
$user = new User();
$user->name = $request->input('name');
$user->email = $request->input('email');
$user->password = Hash::make($request->input('password'));
if($user->save()){
return response (['result' => true]);
}
return response(['result' => false,]);
}
You can be use Form Request.
https://laravel.com/docs/8.x/validation#form-request-validation
or you can use the following
public function register(Request $request){
$data = $request->validate([
'email' => 'required|email|unique:users',
'name' => 'required|string',
'password' => 'required|string'
]);
$data['password'] = bcrypt($data['password']);
try {
User::create($data);
return response()->json([
'message' => __('User created'),
'result' => true
]);
}catch (\Exception $exception){
return response()->json([
'message' => $exception->getMessage(),
'result' => false
]);
}
}
I've been getting the error Creating default object from empty value laravelI was successful to insert new rows with the following code but today when I tried testing the code it is returning the error pointing on the line $reviw->rating = $request->productrating;.
Structure of my db table is:
id|fname|lname|email|country|title|content|rating|thumbnail|tour_id|status
public function store(Request $request)
{
// dd($request->all());
$this->validate($request, [
'fname' => 'required',
'lname' => 'required',
'email' => 'required',
'country' => 'required',
'title' => 'required|min:10',
'productrating' => 'required',
'content' => 'required|min:10'
]);
// dd($request->productrating);
$review = new Review;
$review->fname = $request->fname;
$review->lname = $request->lname;
$review->email = $request->email;
$review->country = $request->country;
$review->title = $request->title;
$review->content = $request->content;
$reviw->rating = $request->productrating;
if($request->hasFile('fileupload1')){
$image = $request->file('fileupload1');
$filename = 'thumb'.time().'.'.$image->getClientOriginalExtension();
$location = 'images/client_review/'.$filename;
Image::make($image)->resize(200, 200)->save($location);
$review->thumbnail = $location;
}
$review->tour_id = $request->product_id;
$review->status = false;
$review->save();
Session::flash('success','Thank You for submitting us your review.');
return view('public.pages.message-review');
}
I'm sending following data from the form to save into the table.
I think you need to update your code like :
$review = new Review;
$review->fname = $request->fname;
$review->lname = $request->lname;
$review->email = $request->email;
$review->country = $request->country;
$review->title = $request->title;
$review->content = $request->content;
$review->rating = $request->productrating;
You have an error in $reviw->rating = $request->productrating; change $reviw to $review and it will work
I try to give users ability to change their password in profile page but when they change the password, password will not be hashed.
This is my update function in usercontroller:
public function update(Request $request, $id)
{
$user = User::find($id);
$this->validate($request, array(
'name' => 'required|string|max:255',
'email' => [
'required','nullable','string','email','max:255',
Rule::unique('users')->ignore($user->id),
],
'gender' => 'required|string',
'password' => 'nullable|string|min:6|confirmed',
));
$user = User::find($id);
$user->name = $request->input('name');
$user->email = $request->input('email');
$user->gender = $request->input('gender');
$user->password = $request->input('password');
$user->save();
Session::flash('success', 'Your information was successfully updated.');
return redirect()->route('users.list');
}
I solved the issue with using Trim. here is the code for those who need it:
if (trim(Input::get('password')) != '') {
$user->password = Hash::make(trim(Input::get('password')));
}
I have created a model
Review.php
and resource controller for it
ReviewController.php
with route
Route::resource('review','ReviewController');
and created a form to get the values. My store method:
public function store(Request $request)
{
$this->validate($request, [
'fullname' => 'required|max:255',
'email' => 'required',
'country' => 'required',
'tourname' => 'required',
'content' => 'required'
]);
$reviews = new Review;
$reviews->name = $request->fullname;
$reviews->email = $request->email;
$reviews->country = $request->country;
$reviews->content = $request->content;
$reviews->tour_id = $request->tourname;
if($request->hasFile('clidentpp')){
$image = $request->file('clidentpp');
$filename = time().'.'.$image->getClientOriginalName();
$location = public_path('images/client_review'.$filename);
Image::make($image)->resize(128, 128)->save($location);
$reviews->path = $location;
}
$reviews->save();
Session::flash('success','Thank you for your review !');
return back()->withInput(['tab'=>'complete']);
}
I'm getting error
Use of undefined constant reviews - assumed 'reviews'
pointing at line $reviews = new Review;. I tried changing$reviews to $review also still no luck.
It should be $reviews = new Review();
Try this.
$review[] = '';
$review['name'] = $request->fullname;
$review['email'] = $request->email;
...
Review::create($review);