Facing this error message but not sure what is wrong with it? Tried the solutions at StackOverflow and other forums but didn't work either.
The error is at line $review->title=$request->title;
public function updateReview(Request $request)
{
// dd($request->all());
$review = Review::find($request->id);
$review->id=$request->id;
$review->title=$request->title;
$review->review=$request->review;
$review->rating=$request->rating;
$review->save();
return redirect('/');
}
A dd($request->all()); returns the following:
array:5 [▼
"id" => "3"
"title" => "User 3 has updated title"
"review" => "User 4 just updated review"
"rating" => "5"
"_token" => "pGFVAzHNg7HmXbkMXylxcM6biqaGnwFmsxjsrTgl"
]
And these are my routes:
Route::get('/edit_review/{id}', 'App\Http\Controllers\UserController#editReview');
Route::post('/update', 'App\Http\Controllers\UserController#updateReview')->middleware('user');
You're getting this error Warning: Creating default object from empty value because $review->title returns false.
find() takes an id and returns a single model. If no matching model exist, it returns null.
You can use findOrFail() instead of find(), if the record not matched then findOrFail() will throw a 404 error :
$review = Review::findOrFail($request->id);
I think you've missed to define a parameter on your function, that passes with your route, probaby its {id} :
public function updateReview(Request $request, $id)
{
$review = Review::findOrFail($id);
//...
}
Related
I'm having a problem executing a method that changes the password of the user logged into the system, because it is not recognizing array column to change password. The error is Attempt to read property "new_password" on array.
dd($validations)
array:3 [▼ // app/Http/Controllers/ChangePasswordController.php:22
"current_password" => "12345."
"new_password" => "67890."
"confirm_new_password" => "67890."
]
method
public function updatePassword(ChangePasswordRequest $request)
{
$user = Auth::user()->name;
$validations = $request->validated();
User::find(Auth::user()->id)->update(['password'=> Hash::make($validations->new_password)]);
return redirect('change_password')->with('success-update-password',"$user changed your password with sucess.");
}
User::whereId(Auth::user()->id)->update(['password'=> Hash::make($validations['new_password'])]);
i got object with some validationl, and the request validated method returns even fields that are not in validation rules. Here is how my validation class looks like.
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules()
{
return [
'terminal' => ['required'],
'terminal.followings' => ['boolean']
];
}
And this is how my json body on request looks like.
{
"client" : {
"pk" : "2128436070",
"username" : "miljan_rakita",
"daily_following" : 1000
},
"terminal" : {
"followings" : true,
"followingasdad" : "asdasdas",
"followers" : false,
"exact_account" : true
}
}
From this validation, i expect to get only this object:
{
"terminal" : {
"followings" : true,
}
}
But when i dd the validated data:
public function store(Test $request)
{
dd($request->validated());
}
Response is full terminal object, even non validated items.
array:1 [▼
"terminal" => array:4 [▼
"followings" => true
"followingasdad" => "asdasdas"
"followers" => false
"exact_account" => true
]
]
So i expect to get only terminal object with followings field with it. But i get all other fields that are in terminal object.
I can pull out only items i need from array, but i would like to know why i don't get only validated items.
The form validation only checks if the incoming request does at least contain all required values, so the request data you receive is as expected. Having said so you'd like to adjust the incoming data, which can be done in the latest release; laravel v5.8.33. You can do so by implementing the passedValidation() method in your formrequest (Test in your case).
public function passedValidation()
{
// '$this' contains all fields, replace those with the ones you want here
}
Below is my function in my controller.
public function getUserTimesheet($userId)
{
$user = User::find($userId);
if($user) {
$userTimesheets = $user->userTimesheets->where('created_at', '>=', Carbon::now()->month());
return response()->json([
"status" => true,
"data" => [
"user-timesheets" => $userTimesheets
]
], 200);
}
return response()->json([
"status" => false,
"message" => "User not found"
], 401);
}
I did create model for the same function.
Multiple timesheets will be uploaded for the same user but I want to return the data of latest upload. I have crated_at column in my respective database and want to compare it to get latest timesheet information by comparing months.
I am getting an error of call to a member function where() on null.
Am new to laravel and creating APIs. Thank you.
The error suggests that the userTimesheets relationship on the particular user is null. Check whether the user has any timesheets, if they have any check how the userTimesheets relationship is defined.
I want to pass the variable id from (the url is /xxxx/{{id}} ) to store
I have a modal box which opens a form. This info is then stored using the controller shown below:
I tried adding $id to pass through the controller (the url is /xxxx/{{id}} )
OpenHome Controller
public function store(Request $request,$id)
$option = new Option;
$option->time = $request->input('time');
$option->date = $request->input('date');
$option->save();
return view('option.create');
ERROR: Type error: Too few arguments to function
App\Http\Controllers\option::store(), 1 passed and exactly 2 expected
dd($request->all());
array:3 [▼
"_token" => "7O23EkldYOYj1d7Fc2xNflcPWMY67ytGTkrymy9g"
"time" => "00:12"
"date" => "12-05-1996"
]
In your question you imply an update of an existing record, but in your code it implies the creation of a new option.
If you wanted to update an option with the $id = 1 you need to have the following route.
Route::put('/options/{id}', 'OptionController#update');
Inside your controller.
public function update(Request $request, $id)
{
$option = Option::find($id);
$option->update([
'time' => $request->input('time'),
'date' => $request->input('date'),
]);
// Redirect to display the list of options.
return redirect('/options');
}
However if you just wanted to pass an additional variable to the store method for use inside the store method, make sure the Route only has single set of {} around the variable.
Route::post('/option/{variable}', 'OptionController#store');
public function store(Request $request, $variable)
{
// User the $variable passed as a route parameter.
}
I have a route like this:
web.php
Route::get('post/{slug}', [
'as' => 'post.single',
'uses' => 'PageController#getSingle',
]);
PageController.php
public function getSingle($slug)
{
//some db stuff and returning an array to view
return view('single', array('var1' => $var, 'var2' => $var2));
}
A post has a slug which is stored in the database.
If a post exists with slug, example: first-post
Then the route mysite.com/post/first-post works as expected.
But if a post doesn't exists, example: second-post
Then the route mysite.com/post/second-post gives me an error:
**ErrorException**
Trying to get property of non-object
I want it to show a 404 error page (404 page is already configured)
mysite.com/hellohello gives and 404 page, so it's working as expected.
Any suggestion what should I do?
Eloquent has a neat firstOrFail method which either returns the first entity it finds, or if there is none throws an ModelNotFoundException which laravel converts to a 404 page.
public function getSingle($slug)
{
$var = YourModel::where('slug', $slug)->firstOrFail();
return view('single', compact('var'));
}
I think you should update your function like:
And you can not close bracket in return view
public function getSingle($slug)
{
//some db stuff and returning an array to view
return view('single', array('var' => $var));
}
UPDATE after OP change the question content:
public function getSingle($slug)
{
//some db stuff and returning an array to view
$var = SOMEDBRESULT1;
$var2 = SOMEDBRESULT2;
return view('single', array('var1' => $var, 'var2' => $var2));
}
You can learn about how to get first record from db with laravel 5.4.