Response in laravel component - php

I have a component SeenRecentlyProduct and return code:
return response()->view('components.products.seen-recently-product', compact('seenRecentlyProducts'))
->withCookie($cookie);
I get an error:
Illegal offset type in isset or empty
If I write this code in the controller, it works well. If I remove response() from the component, the same works well. Help please resolve the issue with the response and cookie in the component.

Related

findOrFail Laravel 8 method not found

I am working on Laravel 8 , I have tried to use findOrFail method in order to set redirection to 404 error not found instead of displaying error but it is showing this error method findOrFail not found,
this is the line of code in ProfilesController that is causing the error
$user = User::findOrFail($user);
this is the output of the error
enter image description here
Thanks in advance,
Look like your code works fine. The issue Php Strom not able to identify methods.
I always use query() method to get autocomplete model methods
$user=User::query()->findOrFail($user);
or you might need to install any extension which can able to identify laravel methods

Laravel Controller saying undefined Request, Any error here?

I am developing a laravel project but some part of my codes is returning an error of undefined request
Seems like i forgot something or ..
Here is a snip
Of my codes
the problem is with how you defined Request Parameter in your function, It should be like
public function insertrecord(Request $request) {}
Now you can proceed... Hope this helps..

json_encode only works with JSON_PARTIAL_OUTPUT_ON_ERROR for Model with custom toArray

I have an Eloquent Model, for which I created a custom toArray()-method, to include fields from a meta table (which I get by using the eloquent-meta plugin):
class User extends Model{
// ... Other stuff
public function toArray(){
return array_merge(parent::toArray(), $this->getAllMeta()->toArray());
}
}
When I now try to send this model as a JSON response using Response::json(...), I get:
UnexpectedValueException in Response.php line 403: The Response content must be a string or object implementing __toString(),
"boolean" given.
I have traced the error to the JsonResponse.setData($data)-method, in which the json_encode-call returns false. The json_last_error()-method returns JSON_ERROR_SYNTAX and the json_last_error_msg()-method returns Syntax error.
Using the debugger I stopped on the line below and evaluated the statement myself. As expected, it does not work, however if I call it like this, it works:
json_encode($data, JSON_PARTIAL_OUTPUT_ON_ERROR);
This returns the complete, valid JSON that I expect, without any missing or NULL values.
What's even stranger is, if I stop in the toArray()-method and supply the merged array to json_encode, it works fine, even without partial.
Am I overlooking something obvious here??
The problem was with the eloquent-meta plugin I used. Here's the relevant part from my issue:
I traced the error back to the
Helpers.maybeDecode($value)-method:
The current implementation tries to parse the value with
json_decode($value) and checks, whether that worked, by checking the
json_last_error()-function. The problem is, that this doesn't reset
the last error.
When the Helpers.maybeDecode($value)-method is called, while
Laravel is encoding the model, and the value it tried to decode
was not an valid json (for example, a simple string), the error
code is set, causing the json_encode()-function to see it and return
null. The problem is with the global nature of the error-variable.
My proposed workaround for this is to reset the
json_last_error()-function after checking if decoing worked, and the
only way I have found to do this is by decoding something valid (even
if it's just an empty array).
Here is the Pull Request with the fix.

Laravel Passing Validation Errors to View

I have the following Laravel 4.2 controller code in a store method:
if ($validator->fails()) {
return Redirect::back()->withErrors($validator)->withInput();
}
I've tested this code with var_dump and the $validator is definitely returning the correct error messages.
The errors however are not being passed to the view. When attempting var_dump($errors) on the view I get the following exception
Undefined variable: errors.
I've also tried (by accident) var_dump($error)and it is of type object(Illuminate\Support\ViewErrorBag) however it does not contain any errors.
Does anyone know what the problem might be? Thanks in advance.
After looking into this issue for two whole days - I've finally fixed it! In the end I removed the vendor folder and ran composer update. I can only assume that something within Laravel was corrupt in the initial download.

why i am getting 404 on passing different function name in url?

I don't know how restler behaves. when i am passing getuser/2 it works fine, but when i am passing /getalluser it gives
{
error: {
code: 404,
message: "Not Found"
}
}
But when i changed the function name getalluser to buyalluser it works fine.
Can anybody tell me whats the problem with my function names in restler framework?
As you have already found out
Restler is using get, post, put, delete as method prefixes to automatically map them to respective HTTP method/verb GET is the default HTTP method, so if you dont prefix a method with any of the above, it will be mapped to GET method.

Categories