Undefined variable in Laravel 5.7

Asked

Viewed 859 times

-2

I’m getting the following error message in my project:

Undefined variable: banner (View: C: wamp64 www painelEda Resources views alterar-banner.blade.php)

While in my view banner.blade.php is this way:

<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 is 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?

  • You are probably not passing the variable banner controller for your view. Or the variable name is incorrect. Edit the question and enter the contents of your controller

  • I edited the question and added the controller.

2 answers

1

In your controller’s Edit function, change the variable name banners:

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

To banner:

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

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

  • Show! It worked. Thank you so much!

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!']);
}
  • I added my controller so you can understand

Browser other questions tagged

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