I can not get inputs file from a put or patch request in Laravel - laravel

Description:
I can not check exist file if using put/patch method.
Image description
The picture put method when using post man
Expected:
I can check exist file if using put/patch method similar using post method.
Image description
The picture post method when using post man
My code example:
public function update(Request $request, $id){
dd($request->hasFile('logo'));
}

check your form's attribute . it must have enctype="multipart/form-data"

I can't see all of your data but make sure you are including CRSF token.
https://laravel.com/docs/5.7/csrf
Hope this helps.

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 - INPUT ERROR - The POST method is not supported for this route. Supported methods: GET, HEAD

I'm trying to create an input form but couldn't find the solution from the error
This is what it said
My View Code
My Controller
My Router
I've tried some tips online but it still won't work
It's because your route name is not defined. you should add the route name like this
Route::post('CreateItem', 'CreateItem#insert')->name('CreateItem');
I think is related to the route name. Have you check which uri is posting the form? Can you try:
Route::post('/createItem', function () {
//
})->name('createItem');
Sorry, got the answer
Route::post('CreateItem','CreateItem#insert');
to
Route::post('/create_item','CreateItem#insert');
and
class CreateItem extends Controller
in my question, I typed the wrong controller name, it should be CreateItem
Thanks for the effort to answer my question tho..

Which HTTP request should I use while using updateOrCreate() method in Laravel?

I'm using updateOrCreate but should I use Post Http Request or Put request while I'm using it?
We doesn't know your architecture so it's difficult to give you an appropriate answer.
But, speaking in general termes, A CRUD use the methods as follow:
GET index()
GET create()
POST store()
GET edit($id)
PATCH update($id)
DELETE destroy($id)
In your scenario, I assume you doesn't know the ID of your resource.
In that case it can't be a PATCH (or PUT). The left over would be POST.

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.

Laravel-4 Empty PUT data being sent to a resourceful controller

G'day,
I'm having issues with PUT requests made via Chrome Postman to a controller, the PUT data is not present, POST data works fine.
I had performed a composer update prior to ensure that the latest version of vendor products where available and even removed bootstrap/compiled.php.
Is anybody else having similar issues?
The update function with both section_id and data being empty in the response:
public function update($id)
{
$section_id = Input::get('section_id');
$data = Input::all();
return Response::json(array('id' => $id, 'section_id' => $section_id, 'data' => $data));
}
I've debugged the code all the way to ParameterBag.php and $this->request's parameter list is empty, I'm not sure what's supposed to contain any values but all through the code the input values are empty. Not sure what to do now, short of using post instead of put.
PUT parameters don't work "out of the box" because PHP itself has some security restrictions around them. See: http://www.php.net/manual/en/features.file-upload.put-method.php
Laravel does implement a common workaround for this, though.
In Postman (or your form, or curl, or whatever client you're using), simply add a URL parameter name: "_method" value: PUT
Example 1:
?_method=PUT
Example 2:
<input type="hidden" name="_method" value="PUT" />
Laravel uses the symfony Http Foundation which checks for the _method variable and if it's present it routes based on its value, instead of the actual HTTP method used.
You have to send a POST request with adding an extra parameter _method with value PUT and it will works fine.

Resources