Reputation: 1340
I've decided to upgrade a website from Laravel 4.3 to Laravel 5.1 and I'm facing a strange problem.
I'm trying to upload some pictures using the DropzoneJS library. I'm telling this library: "Before sending the pictures to /pictures/store
(with an Ajax POST method), adds the album_id
parameter to the request".
This part is working but in my PictureController
, the store
action is taking a Request
object that remains empty instead of containing all inputs and many other things.
View:
{!! Form::open(['url' => '/pictures/store', 'class' => 'dropzone', 'id' => 'myAwesomeDropzone']) !!}
{!! Form::hidden('album_id', $album->id) !!} // Gives a correct value here
{!! Form::close() !!}
JS:
var token = $('meta[name="csrf-token"]').attr('content');
Dropzone.options.myAwesomeDropzone = {
paramName : 'file',
maxFilesize : 8, // Mo
acceptedFiles : 'image/*',
headers : {
'X-CSRF-TOKEN' : token
},
sending : function(file, xhr, formData) {
formData.append('album_id', $('form input[name=album_id]').val()); // Still a correct value here
},
success : function(file, response) {
console.log(response); // Will display the Request object (see controller)
},
error : function(file, error) {
console.error(error);
}
}
Controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class PictureController extends Controller {
public function store(Request $request) {
return response()->json(['request' => $request]); // Returns the Request object
}
}
Here is my Request object:
Not containing anything... And the Ajax request:
-----------------------------98052356720717
Content-Disposition: form-data; name="album_id"
1
-----------------------------98052356720717
Content-Disposition: form-data; name="_token"
It1DQQiXuiLJGwgJwx5UVXe1QEP7TsC1uovglxD2
Upvotes: 1
Views: 1085
Reputation: 1340
Well, I found a solution but I'm pretty sure this isn't the best one.
In my PictureController
instead of getting the album_id
parameter like this:
$request->input('album_id')
I used the Input
facade like that (Laravel 4's style):
Input::get('album_id')
And I don't know why, but it works ! If you have a better solution, be sure that I will pick your answer as the best one. Meanwhile, mine is the best :D
Upvotes: 2