简体   繁体   English

Laravel 5.4中的表单模型绑定问题

[英]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. 当我在Laravel中进行表单模型绑定时,通常不会发生这种情况,但是由于某种原因,每次我提起绑定的表单时,我都会得到完全相同的记录。

{!! 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: 我的印象是$contact在:

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

Should be $contacts . 应该是$contacts

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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