Estoy tratando de visualizar el listado de los registros guardados en un crud, estoy recibiendo el siguiente error: rying to get property 'departamento' of non-object (View: C:\xampp\htdocs\hoysi\resources\views\entregas\listado-entregas.blade.php)
anteriormente e agregado la ruta del app, departamento y municipio
use App\Departamento;
use App\Municipio;
codigo del controlador
la función ShowEntregas
anteriormente e usado el código para listar los registros de otros registros
public function showEntregas(Request $request)
{
$count = Entrega::count();
$departamentos_lista = Departamento::all();
if ($request->cmbDepartamento) {
$departamento = Departamento::find($request->cmbDepartamento);
$geocodigoDepartamento = $departamento->geocodigo;
$municipios_lista = DB::select('CALL SP_Fill_Municipios(?)', array($geocodigoDepartamento));
} else {
$municipios_lista = Municipio::all();
}
$filtro = "1";
if ($request->cmbDepartamento) {
$filtro .= " AND iddepartamento = '".$request->cmbDepartamento."'";
}
if ($request->cmbMunicipio) {
$filtro .= " AND idmunicipio = '".$request->cmbMunicipio."'";
}
if ($request->txtBusqueda) {
$filtro .= " AND nombre LIKE '%".$request->txtBusqueda."%'";
}
$entregas = Entrega::whereRaw($filtro)->paginate(15);
return view('entregas.listado-entregas', [
'count' => $count,
'entregas' => $entregas,
'departamentos_lista' => $departamentos_lista,
'municipios_lista' => $municipios_lista,
]);
}
vista
aquí esta el código del archivo lista de entregas
<table class="table table-hover table-striped table-bordered">
<thead style="background: #84BE38; color: white;">
<tr>
<th style="vertical-align: middle; text-align: center;" class="col-md-4 col-sm-3 col-xs-3">NOMBRE DEL BENEFICIARIO</th>
<th style="vertical-align: middle; text-align: center;">TIPO DE AYUDA</th>
<th style="vertical-align: middle; text-align: center;">MIEMBROS</th>
<th style="vertical-align: middle; text-align: center;">DEPARTAMENTO</th>
<th style="vertical-align: middle; text-align: center;">MUNICIPIO</th>
<th style="vertical-align: middle; text-align: center;" nowrap>ACCIONES <i class="fa fa-cogs" aria-hidden="true"></i></th>
</tr>
</thead>
<tbody>
@foreach ($entregas as $item)
<tr>
<td>{{ $item->nombre }}</td>
<td>{{ config('const.entrega1')[$item->tipo_entrega] }}</td>
<td align="center">{{ $item->num_miembros }}</td>
<td>{{ $item->departamento->departamento}}</td>
<td>{{ $item->municipio->nombre }}</td>
<td class="text-center d-flex">
<a href="{{ route('entregas.show', $item->identrega) }}" class="btn btn-primary btn-xs mx-1">
<i class="fa fa-eye" aria-hidden="true"></i>
</a>
<a href="{{ route('entregas.edit', $item->identrega) }}" class="btn btn-success btn-xs mx-1">
<i class="fa fa-edit" aria-hidden="true"></i>
</a>
<form action="{{ route('entregas.destroy', $item->identrega)}}" method="post">
{{ csrf_field() }}
{{ method_field('DELETE') }}
<button class="btn btn-danger btn-xs mx-1" type="submit">
<i class="fa fa-trash btn-xs" aria-hidden="true"></i>
</button>
</form>
</td>
@endforeach
</tr>
</tbody>
</table>
Rutas
Route::match(['GET', 'POST'],'/lista-entregas','EntregasController@showEntregas')->name('entregas.index');
class Departamento extends Model
{
protected $table = "departamento";
protected $primaryKey = 'iddepartamento';
public $timestamps = false;
}
class Municipio extends Model
{
protected $table = "municipio";
protected $primaryKey = 'idmunicipio';
public $timestamps = false;
}
modelo
/**
* Relacion departamento
*/
public function departamento()
{
return $this->hasOne('App\Departamento', 'iddepartamento', 'iddepartamento');
}
/**
* Relacion municipio
*/
public function municipio()
{
return $this->hasOne('App\Municipio', 'idmunicipio', 'idmunicipio');
}
de ante mano muchas gracias por su tiempo.
Entrega::with('departamento')