簡體   English   中英

Laravel 5.4中的表單模型綁定問題

[英]Issue with Form model binding in Laravel 5.4

當我在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() !!}

控制器:

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

當我單擊此按鈕時,該表單將作為模式彈出

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

我以前使用過這種相同的格式,通常每次點擊都會給我不同的記錄。 但是由於某種原因,我單擊以顯示更新表單的每條記錄,它每次都向同一記錄注冊。 有想法該怎么解決這個嗎?

我想你可以試試這個:

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

希望對您有幫助!

除了所說的,

我的印象是$contact在:

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

應該是$contacts

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM