I'm trying to upload videos using laravel and I'm releasing that error, even putting the {!! csrf_field() !!}
and {{ csrf_token() }}
keeps throwing me the error, also deactivates in the kernel the \App\Http\Middleware\VerifyCsrfToken::class,
, and here something curious happens too, in the form I have 2 validations required for 2 fields and when I send the form I get an error that those fields are required even when I fill them out.
If I remove the validation from required to these fields, it is the only form that is sent but to the BBDD
nothing arrives, if someone knows how I can solve mainly the error of Token
.
Vista
@extends('layouts.app') {{-- Plantilla maestra a utilizar --}}
@section('content')
<div class="container">
<div class="row">
<h2>Crear un Nuevo Video</h2>
<hr>
<form action=" {{ route('saveVideo') }} " method="POST" enctype="multipart/form-data" class="col-lg-7">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
{!! csrf_field() !!} <!-- Token de seguridad que laravel obliga a poner -->
@if($errors->any()) <!-- Errores de validacion -->
<div class="alert alert-danger">
<ul>
@foreach($errors->all() as $error)
<li> {{$error}} </li>
@endforeach
</ul>
</div>
@endif
{{-- old('name'), es un helper que si el formulario arroja un error le da el ultimo valor al campo definido con el helper --}}
<div class="form-group">
<label for="title">Titulo</label>
<input type="text" class="form-control" name="title" value="{{ old('title') }}">
</div>
<div class="form-group">
<label for="description">Descripción</label>
<textarea type="text" class="form-control" name="description">{{ old('description') }}</textarea>
</div>
<div class="form-group">
<label for="image">Miniatura</label>
<input type="file" class="form-control" name="image">
</div>
<div class="form-group">
<label for="video">Video</label>
<input type="file" class="form-control" name="video">
</div>
<button type="submit" class="btn btn-success">Crear Video</button>
</form>
</div>
</div>
@endsection´
Driver
public function saveVideo(Request $request){
// Validaciones del Formulario
$validateData = $this->validate($request, [
'title' => 'required|min:5',
'description' => 'required',
]);
$video = new Video(); // Instanciamos el modelo creado
$user = \Auth::user(); // Para conseguir el usuario identificado
$video->user_id = $user->id;
$video->title = $request->input('title');
$video->description = $request->input('description');
// Subida de la miniatura
$image = $request->file('image');
if($image){
$image_path = time().$image->getClientOriginalName(); // Para obtener el nombre del fichero, se pone el time() para que guarde el tiempo en que se cargo la imagen de forma que nunca se repita el nombre
\Storage::disk('images')->put($image_path, \File::get($image)); // Obtenemos el fichero a guardar
$video->image = $image_path;
}
// Subida de el Video
$video_file = $request->file('video');
if($video_file){
$video_path = time().$video_file->getClientOriginalName();
\Storage::disk('videos')->put($video_path, \File::get($video_file));
$video->video_path = $video_path;
}
$video->save();
return redirect()->route('home')->with(array(
'message' => 'El video se a subido correctamente'
));
}
}
Routes
// Rutas del controlador de Videos
Route::get('/crear-video', array(
'as' => 'createVideo', // Nombre de la ruta
'middleware' => 'auth', // Middleware a usar
'uses' => 'VideoController@createVideo' // Que controlador y accion a usar
));
Route::post('/guardar-video', array(
'as' => 'saveVideo', // Nombre de la ruta, (PUEDE SER USADO EN LOS ACTION DE LOS FORM)
'middleware' => 'auth', // Middleware a usar
'uses' => 'VideoController@saveVideo' // Que controlador y accion a usar
));