简体   繁体   中英

Issue with Form model binding in Laravel 5.4

This usually doesn't occur when I'm form model binding in Laravel, but for some reason, every time I pull up my binded form, I'm getting the exact same record.

{!! Form::model($contact, ['method'=>'PUT', 'route'=>['contact.update', $contact->id]]) !!}
{!! Form::label('firstname', 'First Name:') !!}
{!! Form::text('firstname', null, ['class'=>'form-control']) !!}
{!! Form::label('lastname', 'Last Name:') !!}
{!! Form::text('lastname', null, ['class'=>'form-control']) !!}
{!! Form::label('email', 'Email:') !!}
{!! Form::text('email', null, ['class'=>'form-control']) !!}
{!! Form::label('address', 'Address:') !!}
{!! Form::text('address', null, ['class'=>'form-control']) !!}
{!! Form::label('phone_number', 'Phone:') !!}
{!! Form::text('phone_number', null, ['class'=>'form-control']) !!}
{!! Form::submit('Update Contact', ['class'=>'btn btn-primary']) !!}
{!! Form::button('Close', ['class'=>'btn btn-default', 'data-dismiss'=>'modal']) !!}
{!! Form::close() !!}

Controller:

public function index()
{
    $user = Auth::user();
    $contacts = $user->contacts()->get();
    return view('contacts.index', compact('contacts','user'));
}

When I click this button, that form is popping up as a modal

<button class="btn btn-default editContact" data-toggle="modal" data-target="#editModal">Edit Contact</button>

I've used this same format before and it usually gives me different records with each click. But for some reason every record I click on to display the update form, it registers with the same record every time. Any ideas on how to fix this?

I think you can try this :

public function index()
{
    $user = Auth::user();
    $contacts = Contacts::where('user_id',$user->id)->first();
    return view('contacts.index', compact('contacts','user'));
}

Hope this help for you !!!

In addition to what has been said,

I am under the impression that the $contact in:

{!!
    Form::model($contact, [
         'method'=>'PUT',
         'route'=>['contact.update', $contact->id]
    ])
!!}

Should be $contacts .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM