Undefined variable no Laravel 5.7

-2

I'm having the following error message in my project:

  

Undefined variable: banner (View:   C: \ wamp64 \ www \ panelEda \ resources \ views \ alter-banner.blade.php)

While in my view banner.blade.php looks like this:

<div class="row">
    <div class="col-12">
        <form method="post" action="{{ route('banner.update', $banner->id) }}" enctype="multipart/form-data">
        {{ method_field('PUT') }
        {{ csrf_field() }}
        <div class="card">
            <div class="card-body">
                <div class="row">
                    <div class="col-lg-12">
                        <div class="form-row">
                            <div class="form-group col-lg-9">
                                <label for="simpleinput">Título</label>
                                <input type="text" value="{{ $banner->titulo or old('titulo') }}" id="simpleinput" class="form-control" name="titulo">
                            </div>
                            <div class="form-group col-lg-3">
                                <label for="example-date">Texto</label>
                                <input class="form-control" value="{{ $banner->imagem or old('imagem') }}" id="example-date" type="date" name="imagem">
                            </div>
                        </div>
                        <div class="form-row">
                            <div class="form-group col-lg-12">
                                <input class="form-control" value="{{ $banner->descricao or old('descricao') }}" name="descricao"></textarea>
                            </div>
                        </div>
                </div>
            </div>
        </div>
    </div>

The controller looks like this:

public function index(){
    $banners = Banner::all();
    $total = Banner::all()->count();
    return view('banner', compact('banners', 'total'));
}

public function create(){
    return view('adicionar-banner');
}

public function store(Request $request){
    $banners = new Banner;
    $banners->titulo = $request->titulo;
    $banners->descricao = $request->descricao;
    $banners->imagem = $request->imagem;
    $banners->save();
    return redirect()->route('banner.index')->with('message', 'Banner publicado com sucesso!');
}

public function show($id) {

}

public function edit($id) {
    $banners = Banner::findOrFail($id);
    return view('alterar-banner', compact('banner'));
}

public function update(Request $request, $id) {
    $$banners = Banner::findOrFail($id);
    $banners->titulo = $request->titulo;
    $banners->descricao = $request->descricao;
    $banners->save();
    return redirect()->route('banner.index')->with('message', 'Banner alterado com sucesso!');
}

Where am I going wrong?

    
asked by anonymous 31.10.2018 / 22:07

2 answers

0

With the controller it is easier to exemplify ... See if the way below solves your problem ...

public function update(Request $request, $id) {
    **$banner** = Banner::findOrFail($id);
    $banner->titulo = $request->titulo;
    $banner->descricao = $request->descricao;
    $banner->save();
    return redirect()->route('banner.index', ['banner' => $banner, 'message' => 'Banner alterado com sucesso!']);
}
    
01.11.2018 / 01:03
0

In the edit function of your controller, change the name of the banners variable:

 $banners = Banner::findOrFail($id);

For banner :

 $banner = Banner::findOrFail($id);

Since the compact function used in its return expects a local variable with the name banner and it is not being defined it will pass view as an undefined variable

    
01.11.2018 / 11:54