Undefined variable in a single view of laravel

3

good I find myself doing a library application in laravel at the time of making an individual view of the database using the driver show function I do it in the following way

public function show($id)
{
    $libros = Libro::find($id);
    if (!is_null($libros))

        return view('libro.show', compact('libro'));
    else
        Session::flash('message','404 Libro no existente');
        return Redirect::to('libro');
}

up here without problems until using the function dd ($ books-> title, $ books-> language); it returns the correct results but at the time of calling the show.blade.php method I get the following error:

Please, if someone understands what mistake I am making, it is grateful to clarify. annex the route and the code of the show

show.blade.php

@extends('layouts.admin')

@section('content') 

@include('alerts.request')

<h2>{{$libro->titulo }}</h2>
<p>{{$libro->idioma }}</p>

@endsection

and the route

Route::resource('libro','LibroController');
route::get('libro/{id}/show', 'LibroController@show')->where(['id' => '[0-9]+']);
    
asked by Santiago Avila 14.02.2017 в 02:52
source

2 answers

1

I think it's a pretty simple error, you're not defining anywhere the variable $libro , instead you're defining $libros .

Because of what you see in your code, it seems it's just to define $libro instead of $libros .

public function show($id)
{
    $libro = Libro::find($id);
    if (!is_null($libro))

        return view('libro.show', compact('libro'));
    else
        Session::flash('message','404 Libro no existente');
        return Redirect::to('libro');
}
    
answered by 14.02.2017 / 03:06
source
1

You can put it like this if you want to keep the name of the variable:

return view('libro.show', ['libro' => $libros]);
    
answered by 14.02.2017 в 10:24