This question is an exact duplicate of:
I am using Laravel 5.1
The form on the view looks like:
{!! Form::open(['url' => '/admin/episode', 'method' => 'post', 'files' => 'true']) !!}
{!! Form::label('Episode Name') !!}
{!! Form::text('name', null) !!}
{!! Form::label('Description') !!}
{!! Form::text('description', null) !!}
{!! Form::label('Channel') !!}
{!! Form::select('channel', $channels) !!}
{!! Form::label('image') !!}
{!! Form::file('image', ['accept' => 'image/*', 'id' => 'image']) !!}
{!! Form::label('audio', 'Upload Audio file') !!}
{!! Form::file('audio', ['accept' => 'audio/*', 'id' => 'audio']) !!}
{!! Form::submit('SUBMIT') !!}
{!! Form::close() !!}
In my controller, I have two methods to handle the uploads of each of the media files, one for the image and the other for the audio file:
public function getImageFileUrl()
{
$filename = Input::file('image');
Cloudder::upload($filename, null);
$imgUrl = Cloudder::getResult()['url'];
return $imgUrl;
}
and
public function getAudioFileUrl()
{
$filename = Input::file('audio');
Cloudder::upload($filename, null);
$audioUrl = Cloudder::getResult()['url'];
return $audioUrl;
}
Then the create method in my controller:
public function create(Request $request)
{
$image = $this->getImageFileUrl();
$audio = $this->getAudioFileUrl();
Episode::create([
'episode_name' => $request->name,
'episode_description' => $request->description,
'image' => $image,
'audio_mp3' => $audio,
'view_count' => 0,
'channel_id' => $request->channel
]);
}
The trouble is, when I try to create a new Episode from the view, I get TokenMismatchException in VerifyCsrfToken.php line 53:
I thought it is because I have two file uploaders
in the same form?
What is the workaround for this? I need both uploaders in the same form. I am open to best way to go about this.
Thanks.
</div>
Are you using {!! Form::open() !!}
to open your forms? If you aren't, you need to include the CSRF token by using the helper csrf_field()
.
For example:
<form method="POST" action="{{ route('episode.create') }}" enctype="multipart/form-data">
{!! csrf_field() !!}
If you are using {!! Form::open() !!}
, please include your entire form code, it would help to debug your issue.
I realized that it is because i have two file uploaders in the same form.
This wouldn't cause your issue, you can have as many file fields as you please in a form.