ErrorException in Laravel

0

Index.blade.php code

@extends('layouts.master')

@section('content')

<div class="container">
<div class="row">
    <div class="col-md-6">

        <h2>entrevistas </h2>

        <table class="table table-striped">
                <tr>
                    <th>nombre_entrevista</th>
                    <th>duracion_entrevista</th>
                    <th>bloque_entrevista</th>
                    <th>Opciones</th>
                </tr>
                @foreach($entrevista as $entrevistas)
                <tr>
                    <td>{{$entrevistas['nombre_entrevista']}}</td>

                    <td>{{$entrevistas['duracion_entrevista']}}</td>
                    <td>{{$entrevistas['bloque_entrevista']}}</td>
                    <td>
                        <a href="{{route('entrevista_editar', ['id' => $entrevistas['id']])}}" class="btn btn-default">Editar</a>
                        <a href="{{route('entrevista_eliminar', ['id' => $entrevistas['id']])}}" class="btn btn-danger"">Eliminar</a>
                    </td>
                </tr>

                @endforeach


        </table>

    </div>
    </div>
   </div>

   @stop

Interview Controller

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Requests;
use App\Noticia;
use App\Entrevista;
use Input;
use Redirect;

class entrevistaController extends Controller
{
  public function index($id){

    $entrevista = Entrevista::get();

    $entrevista = Noticia::find($id)->entrevistas;

    return view('entrevista/index', ['entrevista' => $entrevista]);

}

public function show($id)
{
    $entrevista = new Entrevista;
    $entrevistas = $entrevista->find($id);
    $noticias = Noticia::get();
    return view('entrevista/editar', ['entrevista' => $entrevistas], ['noticias' => $noticias] );
}

Routes

Route::get('entrevista/editar/{id}', 
'EntrevistaController@show')->name('entrevista_editar');
Route::post('entrevista/editar/{id}', 'EntrevistaController@update');

Route::get('entrevista/eliminar/{id}',   
'EntrevistaController@destroy')->name('entrevista_eliminar');


 Route::post('/noticia/nuevo', 
 'NoticiaController@store')->name('nuevo_noticia');

    Route::get('/noticia/nuevo', function () {
        return view('/noticia/nuevo');
    });

    Route::post('/nota/nuevo', 'NotaController@store')->name('nota_noticia');
    Route::get('/nota/nuevo', 'NotaController@listar');

    Route::post('/entrevista/nuevo', 'EntrevistaController@store')->name('entrevista_noticia');
    Route::get('/entrevista/nuevo', 'EntrevistaController@listar');
    
asked by CARLOS OMAR BLANCO RODRÍGUEZ 12.03.2018 в 17:58
source

1 answer

0

The problem you have in the controller in the index method.
You are sending the variable $entrevista with value NULL and you are wanting to make a foreach of that null

This is my suggestion

// Controller

public function index($id){

    $entrevistas = Entrevista::all();

    // $entrevista = Noticia::find($id)->entrevistas; // Esta linea está sobeescribiendo los datos de $entrevista
    $noticia = Noticia::find($id);

    return view('entrevista/index', [
            'entrevistas' => $entrevistas, 
            'noticia' => $noticia
        ]);

}

// index.blade.php

@extends('layouts.master')
@section('content')
    <h1>Entrevistas</h1>
    @foreach($entrevistas as $entrevista) 
        nombre_entrevista: {{ $entrevista->nombre_entrevista }}
        duracion_entrevista: {{ $entrevista->duracion_entrevista }} 
        bloque_entrevista: {{ $entrevista->bloque_entrevista }} 
        id: {{ $entrevista['id']}} 
        <div class="btn btn-default">Editar $entrevista->id])}}</div>
        <div class="btn btn-danger">Eliminar $entrevista->id </div>
    @endforeach

    <h1>Noticia</h1>
    @if($noticia) 
        {{--  {{ $noticia }}  --}}
            @foreach($noticia->entrevistas as $entrevista) 
                duracion_entrevista: {{ $entrevista->duracion_entrevista }} 
                bloque_entrevista: {{ $entrevista->bloque_entrevista }} 
                id: {{ $entrevista['id']}} 
                <div class="btn btn-default">Editar $entrevista->id])}}</div>
                <div class="btn btn-danger">Eliminar $entrevista->id </div>
            @endforeach
    @endif
@endsection
    
answered by 12.03.2018 / 20:04
source