How to set cookie in laravel ajax request - laravel

I have a search form in laravel and I want to save filtered values in cookie in laravel.
I have followed the laravel tutorial and tried to set cookie like this :
$request->cookie('name', json_encode($_POST), 84600);
But it is not saving cookie

Using the cookie method on the request is only for retrieving a value.
If you want to add a value to the upcoming response you can use the queue() method with the cookie() helper function:
cookie->queue('name', json_encode($_POST), 84600);

Using javascript try
$.cookie("name", JSON.stringify($("#youcookiesId_hidden filed").data()));

Related

How to use PUT method in Laravel API with File Upload

Having read this SO link,
PUT is most-often utilized for update capabilities, PUT-ing to a known resource URI with the request body containing the newly-updated representation of the original resource.
From this answer,
... we need to send all the parameters of the data again.
In my controller, I have:
$student = Student::find($student_id);
$student->username = $request->username;
$student->email = $request->email;
$student->password = $request->password;
$path = $request->file('passport')->store('upload');
$student->passport = $path;
I have once used this same code for POST method and it worked, but while using it for APIs, I used POSTMAN form-data and got $request->all() to be null. Some said I should use x-www-form-urlencoded but this does not allow files upload.
This is actually an incapability of PHP itself. A PUT/PATCH request with multipart/form-data just will not populate $_FILES, so Laravel has nothing to work with.
Every once in a while, people report bugs like this when they find $request->all() returns null, thinking it's Laravel's fault, but Laravel can't help it.
Files are best sent as multipart/form-data and that sort of request will only populate $_FILES if it's a POST. No $_FILES, no $request->file().
In lieu of having this work as-expected in PHP, if it works using a POST, just use a POST.
When the form contains uploaded file it works only with POST method - probably an issue with PHP/Laravel
If someone wants to use a PUT or PATCH request for form containing file uploads
<form action="/foo/bar" method="POST">
#method('PUT')
...
</form>
via any javascript framework like vue
let data = new FormData;
data.append("_method", "PUT")
axios.post("some/url", data)
Using _method and setting it to 'PUT' or 'PATCH' will allow to declare route as a PUT route and still use POST request to submit form data
I have answered a similar question How to update image with PUT method in Laravel REST API?

Laravel $request->all() correctly returns data, but $_POST completely empty

I am making an ajax post request to the server, posting json data. In firebug I can see the network post call going through along with the json data.
In Laravel I was trying to do a simple var dump of the $_POST data and have just wasted a fair bit of time being confused as to why this should be completely empty. However, when I use the Request facade, my data is there.
ie. this just gives me an empty array:
public function test(){
Log::info($_POST);
}
...yet this prints my data, as I expect:
public function test(Request $request){
Log::info($request->all());
}
Why?
Edit
Thanks, #Webdesigner. The http verb is definitely post, as my method is called in my routes file via
Route::post('/image-upload', 'EntryController#test'); // Note "post" verb
I don't think $request->post() is valid in Laravel 5.4 as this throws an BadMethodCallException: Method post does not exist. error. However, I can confirm that
Log::info($request->method()); // POST
also tells me the method is post.
Very strange. I guess you're right that some part of the app is overwriting the $_POST global, though I have no idea why/where/how. Probably not relevant, but this call is being made from Angular 4.
Thanks for your help anyway!
This is not the normal behavior of Laravel. I tested this on a fresh Laravel 5.5 site and just did a Form submit and an Ajax POST request to the same Route.
Both give me the same result. A POST Request should have at least the CSRF Token as _token with a value.
One other point is $request->all() is not only the the content of $_POST so to have a fair compression you should try $request->post().
BTW only because you did a POST request do not mean that the data is send by the POST Method, it could be that the data you see in $request->all() is from $_GET and $_COOKIE, etc and only the Method was a POST.
Last but not least there it the option that some part of your APP is deleting the content of the Superglobal Variables. $_POST and the others are not like constants, so they can be changed during runtime e.g. $_POST = [];
I don't thing that there is a difference in Laravel 5.4.27.

Postman send value in laravel is null

I'm trying to send value in laravel controller using postman but it send null value.
UserController:
Route:
Postman:
There is no problem in get method
I am not sure which one are you accessing since you set the value in header but your variable named input. So, I will just provide both methods since it should be helpful!
There are two different method for accessing the body of the request or accessing the header of the request
Accessing the header request
$val = $request->header('val');
Accessing the body request
$val = $request->input('val');

request post open cart to codeigniter

I am trying to find out what is similar to the open cart $this->request->post['permission'] method and what is the same in codeigniter.
I know you have $this->input->post(); which is posting data.
But then I think you have $this->input->get();
Would the input get in codeigniter be closest to $this->request->post['permission']
In every request codeIgniter wraps the $_POST array and you can get values by calling $this->input->post('myKey'). And the same behaviour with $_GET array - $this->input->get('myKey').
So, if you want to get values of posted data, you need to call $this->input->post('myKey').

failed to return AJAX call when use_session_sid

I am trying to use php's use_trans_sid, so I will have phpsessid in all urls.
But, when I set use_trans_sid to 1, AJAX call did not get result properly.
Somehow the result truncated.
When I set use_trans_sid back to 0, AJAX call get result properly again.
What would be the problem?
I am using the Yii framework.
Check ajax url requests when use_trans_sid=1, if they haven't phpsessid var than you need manually add it to request url as GET parameter.
use_trans_sid=1 rewrites only page url, not js and ajax. When it using, PHP try to find session id in get parameter, if not found - created new session.
When you using use_trans_sid=0 ajax works, because PHP work over cookie session id
I have set php's use_trans_sid to 1.
With that, PHP will insert code to propagate session id.
In my case, PHP inserted a hidden variable in a form which has been encoded into json object.
As the result, ajax call get it as a request error (it did not get a json object).
I confirmed this by replacing the form with string like 'Hello'.
With that, PHP did not insert code to propagate session id. And ajax call get it as a good json object.
Now the problem has changed into 'how to make such interruption from PHP will not interfere ajax call?'

Resources