I am creating a website of job offers in Laravel 5.5, and when trying to implement a functionality that allows you to add sectors (for example, IT and Hospitality) to filter the offers for those sectors, from time to time I get the error
htmlspecialchars () expects parameter 1 to be string, array given (View: C: \ xampp \ htdocs \ bagsemployment \ resources \ views \ offers \ index.blade.php)
To temporarily "fix" that error I have to log out of the logged-in user and restart it.
This is my code:
SectorController.php
public function arraySectors(Request $request) {
// Generar Array
if($request->sector == null || $request->sector == "") {
return back()->with('message', ['danger', __('Por favor selecciona un Sector')]);
} else {
$array[] = $request->session()->get('sectors', 'sectors');
$datos = array_merge($array);
$request->session()->put('sectors', $datos);
if(count($request->session()->get('sectors')) >= 5) {
return back()->with('message', ['warning', __('Sólo se pueden seleccionar 5 sectores al mismo tiempo')]);
} else {
$request->session()->push('sectors', $request->sector);
$request->session()->save();
$sectors = session('sectors');
// Consulta
$array = array_values($sectors);
$nOfertas = Oferta::whereIn('sector', $array)->latest()->paginate(5);
return back()->with(compact('sectors', 'nOfertas'));
}
}
}
index.blade.php
<tbody>
@if(!isset($nOfertas))
@forelse($ofertas as $oferta)
<tr>
<td class="py-3">{{ $oferta->titulo }}</td>
<td class="py-3">{{ $oferta->descripcion }}</td>
<td class="py-3">{{ $oferta->empresa }}</td>
<td class="py-3">{{ $oferta->sector }}</td>
<td class="py-3">{{ $oferta->fecha_limite }}</td>
<td class="py-3">
<form class="form-horizontal" method="POST" action="offers/{{ $oferta->id }}/register">
{{ csrf_field() }}
<input type="hidden" name="id_user" value="{{ Auth::user()->id }}">
<input type="hidden" name="id_oferta" value="{{ $oferta->id }}">
<button type="submit" name="registerOferta" class="btn btn-primary">
Inscribirse
</button>
</form>
</td>
</tr>
@empty
<p>
No hay ofertas actualmente
</p>
@endforelse
@else
@forelse($nOfertas as $oferta)
<tr>
<td class="py-3">{{ $oferta->titulo }}</td>
<td class="py-3">{{ $oferta->descripcion }}</td>
<td class="py-3">{{ $oferta->empresa }}</td>
<td class="py-3">{{ $oferta->sector }}</td>
<td class="py-3">{{ $oferta->fecha_limite }}</td>
<td class="py-3">
<form class="form-horizontal" method="POST" action="offers/{{ $oferta->id }}/register">
{{ csrf_field() }}
<input type="hidden" name="id_user" value="{{ Auth::user()->id }}">
<input type="hidden" name="id_oferta" value="{{ $oferta->id }}">
<button type="submit" name="registerOferta" class="btn btn-primary">
Inscribirse
</button>
</form>
</td>
</tr>
@empty
<p>
No hay ofertas actualmente
</p>
@endforelse
@endif
</tbody>
web.php
Route::post('/offers/add_sector', 'SectorController@arraySectors');