0

So I have a little project, in it, there's the possibility to upload banners images to be shown on the main page. All the stuff related to the DB are already created and the option to create a banner is working, it creates the banner and then stores the image on the DB for use later. Now I'm trying to work on an edit function so I can change the description under the bannners. I have an Edit route in my controller which returns a view where I edit said banner then it calls the update function on the controller. But no matter what I put here, I'm always getting the Missing Required Parameters error once I try to Save the edit and open my controller through the Update function. Here's the code as it is now:

The route definition:

Route::resource('banner', 'BannerController');

The edit function on my controller:

    public function edit($id)
{

    return view('admin/edit-banners',['id'=>$id]);
}

The update function has not been implemented because I always start with a dd() function to check if everything is working fine:

    public function update(Request $request, $id)
{
    dd($request);
}

And here's the form line in my edit view that is trying to call the update route:

<form class="card-box" action="{{ route('banner.update',[$banner]) }}">

I also added this at the beginning of the view to store the data from the DB into a variable:

@php
    use\App\Banner;
    $banner = Banner::where('id','=',$id)->get();
@endphp

The $banner variable contains all the information on the banner being edited, and I can get the new description at the controller with the $request variable, so I honestly don't know what should I put here as parameters, any ideas?

6
  • provide the route definition for this route Commented Sep 21, 2020 at 20:27
  • just added it now Commented Sep 21, 2020 at 20:29
  • where did this $banner variable come from? it looks like you are only passing id to your view Commented Sep 21, 2020 at 20:31
  • It is the variable I used to store the data retrieved from the DB, it contains the description of the banner (which is being edited) and the path to the image of the banner itself. Commented Sep 21, 2020 at 20:34
  • where does it come from though? you are only passing a variable named id to your view ... and if the route parameter is named banner you should be passing it as ['banner' => ...] to the route helper Commented Sep 21, 2020 at 20:35

3 Answers 3

1

The $banner variable is not a Model instance, it is a Collection.

Adjust your controller to pass this to the view instead of dong the query in the view:

public function edit($id)
{
    $banner = Banner::findOrFail($id);

    return view('admin.edit-banners', ['banner' => $banner]);
}

You could also use Route Model Binding here instead of doing the query yourself.

Remove that @php block from your view.

The form should be adjusted to use method POST and spoof the method PUT or PATCH (as the update route is a PUT or PATCH route) and you should adjust the call to route:

<form class="card-box" action="{{ route('banner.update', ['banner' => $banner]) }}" method="POST">
    @method('PUT')
Sign up to request clarification or add additional context in comments.

Comments

0

If you include $id in your function declaration then when you call the route helper it expects you to give it an id parameter. Try with

<form class="card-box" action="{{ route('banner.update',['id' => $id]) }}">

You should be able to retrieve the form data just fine form the $request variable. More info here.

1 Comment

I tried that and it results in the same error. I even tried removing the $id from the function declaration but it still returns the same error message
0

The code below should be the error source. $banner variable then is an array but the update function accept object or id.

   @php
   use\App\Banner;
   $banner = Banner::where('id','=',$id)->get();
   @endphp

You should try to replay this code by this one...

   @php
   use\App\Banner;
   $banner = Banner::find($id);
   //you should put a dd here to view the contain of $banner if you like
   @endphp

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.