Level 63
If you use blade to display the views, why are you using Laravel in API mode ?
An API is useful when you have a separate frontend like VueJS, React, ...
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I'm starting to create a rest API with Laravel, which I have a question about since before when I did the validations in the controller, I sent it like this
$rules = [
'receptor' => 'required',
'accion' => 'required',
'state' => 'required',
'fech_ini' => 'required',
'fech_fin' => 'required',
];
$message = [
'receptor.required' => 'Receiver is Required',
'accion.required' => 'Acción is Required',
'state.required' => 'State is Required',
'fech_ini.required' => 'Start Date is Required',
'fech_fin.required' => 'End Date is Required',
];
$validator = Validator::make($request->all(), $rules,$message);
if($validator->fails()):
return back()->withErrors($validator)->with('message','An error has occurred:')->with('typealert','danger')->withInput();
else:
//save
endif;
and in my template blade it showed it like this
<div class="social_media">
@if (Session::has('message'))
<div class="container mt-3">
<div class="alert alert-{{ Session::get('typealert') }}" style="display:none;text-align: left;">
{{ Session::get('message') }}
@if ($errors->any())
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
@endif
<script>
$('.alert').slideDown();
setTimeout(function(){ $('.alert').slideUp(); }, 10000)
</script>
</div>
</div>
@endif
</div>
but when making an api it is done differently since the error is sent by a json and in this way I did not get to the answer of how to show the messages on the blade since when consuming the Session api it does not work for me on the blade
$rules = [
'receptor' => 'required',
'accion' => 'required',
'state' => 'required',
'fech_ini' => 'required',
'fech_fin' => 'required',
];
$message = [
'receptor.required' => 'Receiver is Required',
'accion.required' => 'Acción is Required',
'state.required' => 'State is Required',
'fech_ini.required' => 'Start Date is Required',
'fech_fin.required' => 'End Date is Required',
];
$validator = Validator::make($request->all(), $rules,$message);
if($validator->fails()):
return response()->json([
'validator'=> $validator,
'message'=> 'An error has occurred',
'typealert' => 'danger',
]);
else:
//save
endif;
Please or to participate in this conversation.