I have in my controller a function like this.
$validator = Validator::make($request->all(), [
'user_id' => 'required|numeric',
'role' => 'required',
'firstname' => 'required',
'middlename' => '',
'lastname' => 'required',
'phone_no' => 'required|digits:11',
'address' => 'required',
'city' => 'required',
'state' => 'required',
'bio' => 'required',
]);
if ($validator->fails()) {
return $validator->errors();
}
Then I have the first update query (using model) like this:
$user = User::find($request->user_id);
$user->firstname = $request->firstname;
$user->middlename = $request->middlename;
$user->lastname = $request->lastname;
$user->phone_no = $request->phone_no;
The second one follows like this:
if($request->role == 'customer' ){
$userInfo = Customer::where('user_id', $request->user_id);
$userInfo->address = $request->address;
$userInfo->city = $request->city;
$userInfo->state = $request->state;
$userInfo->bio = $request->bio;
}elseif($request->role == 'employer' ){
$userInfo = Employer::where('user_id', $request->user_id);
$userInfo->address = $request->address;
$userInfo->city = $request->city;
$userInfo->state = $request->state;
$userInfo->bio = $request->bio;
}
if($user->save() && $userInfo->save()){
return response(array("status" => "success", 'statusCode' => 200, 'message' => 'Your Profile Was Updated'))->header('Content-Type', 'application/json');
}
I used AJAX to send the data to the controller. But when I click the update button it displays this error:
BadMethodCallException in Builder.php line 2451:
Call to undefined method Illuminate\Database\Query\Builder::save()
I keep getting this error. How do I resolve this?
Change the following. Also check if userinfo exists before saving since you have a condition where it might not.
$userInfo = Customer::where('user_id', $request->user_id)->first();
$userInfo = Employer::where('user_id', $request->user_id)->first();
if($user->save() && (isset($userInfo) && $userInfo->save())){}
Also your code can be refactored to reduce half the stuff you do. But that's out of scope for this question.
Call to undefined method Illuminate\Database\Query\Builder::save() results because you're calling the save() function on the builder rather than a collection. This is because you're not actually executing your query.
$userInfo = Employer::where('user_id', $request->user_id);
Should be
$userInfo = Employer::where('user_id', $request->user_id)->first();
save() method can save changes in one Eloquent object.
You dont have in $userInfo variable Eloquent object. Just calling where on Model does not return Eloquent object.
Change
$userInfo = Customer::where('user_id', $request->user_id)->first();
$userInfo = Employer::where('user_id', $request->user_id)->first();
Related
I am trying to perform validation without passing the $request variable, would someone be able to provide what I pass through the validator function? My code is below
public function change($id)
{
$user = User::find($id);
$user->name = request("name");
$user->date_of_birth = request("date");
$user->email = request("email");
$user->phone = request("phone");
$user->address = request("address");
$user->city = request("city");
$user->postcode = request("postcode");
$validator = Validator::make(request(), [
'name' => 'required',
'email' => 'email|string|max:255|unique:users',
'phone' => 'required|numeric',
'address' => 'required|numeric',
'postcode' => 'required|numeric',
'city' => 'required'
]);
if ($validator->fails()) {
return redirect("/user/edit")
->withErrors($validator)
->withInput();
}
$user->save();
return redirect('/user');
}
Again, i am trying this without $request
Any help would be appreciated!
You can do by passing an array, so all() returns all the fields passed, try using it like this:
$validator = Validator::make(request()->all(), ...
Another tip is to first make the validation then find the user and set its fields. The validation can be a guard in this case. You can also create a custom Form request and set the validation rules there.
You can use facade to fetch that data and pass it to validator,
$input = Request::all();
Here is the documentation.
If you don't want to use the $request you can just use the request() helper instead. For example:
$user = User::find($id);
$user->name = request("name");
$user->date_of_birth = request("date");
$user->email = request("email");
$user->phone = request("phone");
$user->address = request("address");
$user->city = request("city");
$user->postcode = request("postcode");
request()->validate([
'name' => 'required',
'email' => 'email|string|max:255|unique:users',
'phone' => 'required|numeric',
'address' => 'required|numeric',
'postcode' => 'required|numeric',
'city' => 'required'
])
$user->save();
return redirect('/user');
That works fine unless you want to specifically handle failed validation in a different way.
If you can't use $request, please use request helper which can be used as Request::all(); or request->all();
I have this if statement, if no $address information has been added to a user, then create, else go on.
public function getIndexEditClient(Request $request, $id) {
$regions = DB::table("regions")->pluck("name","id");
$address = Address::where('id', $request->address_id)->with('region')->first();
if(empty($address)){
$address = Address::create([
'street_name',
'house_number',
'postcode',
'city_id',
'country_id',
'region_id'
]);
// assiociate address with user
//then save it
}else{
$data = $this->data->getEditClient($id);
$admins = $this->data->getAdmin();
return view('client.edit', [
'client' => $data,
'admins' => $admins,
'regions' => $regions,
'address' => $address
]);
}
}
The only thing is, i have to associate the user(client) with the addres # the commented lines. I don't get it to work.
try this:
$user->address()->associate($address);
$user->save();
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 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);
So I'm trying to insert values into two tables from a single form using Laravel4.
this is my Store() function.Am i doing it right..?
I know i should be using two controllers AddressController.php and PeopleController.php.., but can i use a single controller to insert into two tables using a single form.?
public function store()
{
$rules = array(
'address_id' => 'required',
'contact_id' => 'required',
'prefix' => 'required',
'firstname' => 'required',
'middlename' => 'required',
'lastname' => 'required',
'suffix' => 'required',
'occupation' => 'required',
'gender' => 'required',
'comment' => 'required'
);
$validator = Validator::make(Input::all(), $rules);
$user= Auth::user();
if (!empty($user->id))
$user_id=$user->id;
// process the login
/*if ($validator->fails()) {
return Redirect::to('people/create')
->withErrors($validator);
} else {*/
// store
$person = new Person;
$person->user_id=$user_id;
$person->address_id =Input::get('address_id');
//$person->contact_id = Input::get('contact_id');
$person->prefix = Input::get('prefix');
$person->firstname =Input::get('firstname');
$person->middlename =Input::get('middlename');
$person->lastname =Input::get('lastname');
$person->suffix =Input::get('suffix');
$person->occupation =Input::get('occupation');
$person->gender =Input::get('gender');
$person->comment =Input::get('comment');
//$person->user_id =Input::get('user_id');
$person->save();
$validator = Validator::make($data = Input::all(), Address::$rules);
$address->address1 = Input::get('address1');
$address->address2 = Input::get('address2');
$address->apt = Input::get('apt');
$address->city = Input::get('city');
$address->state = Input::get('state');
$address->zip = Input::get('zip');
$address->country = Input::get('country');
$address->save();
// redirect
Session::flash('message', 'Successfully created new Employee!');
//return Redirect::to('addresses/create');
return Response::json($person);
}
As long as you've defined a relationship between the person and address models, then you can use the person model's push() method which is designed to save multiple related models in a single step
Note that if you had fillable defined for your models, you could also eliminate a lot of those boilerplate $person->prefix = Input::get('prefix'); statements from your code as well