How to resolve this 'Undefined variable' error when accessing a view in Laravel 5.1?

Asked

Viewed 3,813 times

0

My problem is this:

I uploaded the data to the banner view, which is included in the main view, this in turn is expanded in the home view, is working well. But when I try to access another view (view create-motto) where the main view is also expanded, the following error arises:

Undefined variable: motto (View: C: xampp htdocs GEIGRE Resources views paginas banner.blade.php) (View: C: xampp htdocs GEIGRE Resources views paginas banner.blade.php) (View: C: xampp htdocs GEIGRE Resources views paginas banner.blade.php)

I need help. And from now on, thank you.

Here comes the code.

Lemacontroller

public function create()
{
    $lema=new Lema;
    $lema->texto_lm = Input::get('texto');
    $lema->refBilblica_lm = Input::get('refBiblica');
    $lema->status_lm = Input::get('status');
    $lema->save();
    return redirect('/');
}

Homecontroller

use App\Lema;
use Redirect;

class HomeController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $lema = Lema:: where('status_lm', 'Activo')->get();

        return view('home')->with('lema',$lema);
    }
 } 

Route

Route::get('criar-lema', function () {
    return view('lema.create-lema');
});


Route::post('criar-lema', 'LemaController@create');

Route::get('/', 'HomeController@index');

banner view

<div class="page-header page-header-home">

    <div class="container" style="margin-bottom: 0px; background-color: #85B200">
        <img src="/images/logo.png.png" alt="...">
        <a href="#"> <i class="glyphicon glyphicon-user" style="float: right; position: relative; margin-top: 100px; margin-right: 20px; color: #FFFFFF">Iniciar Sessão</i></a>
        <div style="background-color: #4C6600; width: 450px; height: 90px; border-radius: 15px; position: relative; float: right; margin-right: 190px; margin-top: 22px">
            <?php foreach ($lema as $key => $value): ?>
            <p style="padding: 0px; text-align: center; font-family: 'Tw Cen MT', Arial, 'Helvetica LT Std';font-size: 26px; color: #ffffff">{!! $value->texto_lm  !!}</p>

            <p style="padding: 0px; text-align: center; font-family: 'Tw Cen MT', Arial, 'Helvetica LT Std';font-size: 15px; color: #ffffff">{!! $value->refBilblica_lm  !!}</p>
            <?php endforeach; ?>

        </div>

    </div>

</div>

view main

<!DOCTYPE HTML>
<html>
    @include("paginas.head")
    <body>
        @include("paginas.banner")
        @include("paginas.menu")
        <div class="container">
            @yield("content")
        </div>
    </body>
</html>

home view

@extends('main')

@section('content')

    <div id="carousel-example-generic" class="carousel slide carousel-home" data-ride="carousel">
        <!-- Indicators -->
        <ol class="carousel-indicators">
            <li data-target="#carousel-example-generic" data-slide-to="0" class="active"></li>
            <li data-target="#carousel-example-generic" data-slide-to="1"></li>
            <li data-target="#carousel-example-generic" data-slide-to="2"></li>
        </ol>

        <!-- Wrapper for slides -->
        <div class="carousel-inner" role="listbox">
            <div class="item active">
                <img src="/images/17.jpg" alt="...">
                <div class="carousel-caption">
                    <p>Canguru </p>
                </div>
            </div>
            <div class="item">
                <img src="/images/12.jpg" alt="...">
                <div class="carousel-caption">
                    <p>Tubarão</p>
                </div>
            </div>
            <div class="item">
                <img src="/images/22.jpg" alt="...">
                <div class="carousel-caption">
                    <p>Cão</p>
                </div>
            </div>
        </div>

        <!-- Controls -->
        <a class="left carousel-control" href="#carousel-example-generic" role="button" data-slide="prev">
            <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
            <span class="sr-only">Previous</span>
        </a>
        <a class="right carousel-control" href="#carousel-example-generic" role="button" data-slide="next">
            <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
            <span class="sr-only">Next</span>
        </a>
    </div>

    <div class="row">
        <div class="col-sm-6 col-md-4">
            <div class="thumbnail">
                <img src="/images/logo1.jpg" alt="...">
                <div class="caption">
                    <h3>Thumbnail label</h3>
                    <p>...</p>
                    <p><a href="#" class="btn btn-primary" role="button">Button</a> <a href="#" class="btn btn-default" role="button">Button</a></p>
                </div>
            </div>
        </div>
    </div>

@endsection

view create-lema

@extends('main')

@section('content')
    <div style="background-color: #c9e2b3; padding: 5px 10px">
        <br />
            <h2>NOVO LEMA</h2>
        <br />

        <form action="{{url('criar-lema')}}" method="post" enctype="multipart/form-data">
            {!! csrf_field() !!}
            <div class="form-group">
                <label for="texto">Texto</label>
                <input name="texto" type="text" class="form-control" id="texto" placeholder="Insira aqui o texto do lema">
            </div>
            <div class="form-group">
                <label for="refBiblica">Ref. Bíblica</label>
                <input name="refBiblica" type="text" class="form-control" id="refBiblica" placeholder="Referência Bíblica">
            </div>
            <div class="form-group">
                <label for="status">Status</label>
                <input name="status" type="text" class="form-control" id="status" placeholder=" insira 'Activo' ou 'Não Activo'">
            </div>

            <button type="submit" class="btn btn-success">Salvar</button>
        </form>
    </div>

@endsection

1 answer

1


I believe the problem is this one:

Route::get('criar-lema', function () {
    return view('lema.create-lema');
});

In his method index(), you are passing the motto in the method ->with(). Already in the above method, you are returning the direct view on route.

If you route to a controller, you can reuse the logic of how you’re picking up the mottos and move back to the view whenever you need to. Example:

ROUTE:

Route::get('criar-lema', 'LemaController@showCreate');

CONTROLLER (The main one, Controller.php)

protected function getLemas() {
    return Lema::where('status_lm', 'Activo')->get();
}

CONTROLLER MOTTO

public function showCreate() {
    return view('lema.create-lema')->with('lema', $this->getLemas());
}

public function create()
{
    $lema=new Lema;
    $lema->texto_lm = Input::get('texto');
    $lema->refBilblica_lm = Input::get('refBiblica');
    $lema->status_lm = Input::get('status');
    $lema->save();
    return redirect('/');
}

HOME CONTROLLER

public function index() {
    return view('home')->with('lema', $this->getLemas());
}
  • This way it worked!!! It’s working the way you wanted it to. Thank you @Guh.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.