Latest

Tuesday, July 11, 2017

laravel edit and update doesn't work

Asked by: mohamed gaber


I have a CRUD with simple date. But I can't get the update method to work.

This is my controller

public function show(Customer $customer)
{
    return view('customer.show',compact('customer'));

}


public function edit(Customer $customer)
{
    return view('customer.edit', compact('customer'));

}

public function update(CustomerRequest $request, Customer 
$customer,$id)
{
    $customer = Customer::find($id)->update($request->all());

    return redirect()->route('customer.index',compact('customer'));
}

and that is my view

 <form method="POST" action="{{route('customer.update',$customer->id ) }}">
            {{--{{dd($customer)}}--}}
            {{method_field('PUT')}}

                {{ csrf_field() }}

                <div class="form-group">
                    <label for="firstName">Voornaam</label>
                    <input type="text" class="form-control"  name="firstName" value="{{$customer->firstName}}">
                </div>

                <div class="form-group">
                    <label for="lastName">Achternaam</label>
                    <input type="text" class="form-control"  name="lastName" value="{{$customer->lastName}}">
                </div>

                <div class="form-group">
                    <label for="email">Email</label>
                    <input type="email" class="form-control"  name="email" value="{{$customer->email}}">
                </div>

                <div class="form-group">
                    <label for="phone">Telefoonnummer</label>
                    <input type="text" class="form-control"  name="phone" value="{{$customer->phone}}">
                </div>


                <div class="form-group">
                    <button type="submit" class="btn btn-primary">Update</button>
                </div>
            </form>

I can go to the edit page. But, after changing the data nothing change it look like that I miss save somewhere but I don't know, now I get that that error too few arguments to function New Controller ::update () 2 passed and exactly 3 expected.

any help will be appreciated.


Answers

Answered by: Tim van Uum at 2017-07-11 01:25PM Accepted



Change:

public function update(CustomerRequest $request, Customer $customer,$id)

To:

public function update(CustomerRequest $request, $id)

Or:

public function update(CustomerRequest $request, Customer $customer)

With the last one you can remove Customer::find($id) and just use $customer

EDIT If you look at the update route: route('customer.update',$customer->id ) you see it only takes 1 argument. The controller expects 2 because one is the request and the other is the ID.




Source

No comments:

Post a Comment

Adbox