2017-02-22 96 views
0

我正在試圖用Laravel製作這個小的CV類型配置文件,我遇到了這個問題,我不知道它應該如何解決,正確的方法。 在用戶配置文件中,我有一個類別,用戶可以添加工作經歷。顯然,用戶可以在他工作的地方添加多個地方。我使用模態來做這件事,它可以工作,我可以將它們存儲在數據庫中。編輯多條記錄

事情是,現在我想讓用戶能夠編輯他輸入的內容。所以,我做了一個編輯按鈕,觸發一個模式窗口,他可以編輯數據庫記錄。 我不知道的是我可以如何獲取記錄的特定ID,以便填充模式窗口,然後保存所有更改。

要,因爲我不知道我是否是很清晰總結這件事..

我在DB 3項爲每個條目工作經歷+ 3個編輯聯繫。 然後,該編輯鏈接應導致用戶可編輯它的特定條目的模式窗口。

編輯:

我到了一個點以下一些對你有幫助。但是之後,我再次卡住..

所以,我必須在用戶配置文件這一工作經歷,這裏是如何我顯示出來:

 @foreach ($employment as $empl) 
     <input type="hidden" name="emplID" value="{{ $empl->id }}"> 
     <button data-toggle="modal" data-target="#edit-empl" href="#edit-empl" class="btn btn-default" type="button" name="editbtn">Edit</button> 
     <h3 class="profile-subtitle">{{ $empl->company }}</h3> 
     <p class="profile-text subtitle-desc">{{ $empl->parseDate($empl->from) }} - {{ $empl->parseDate($empl->to) }}</p> 
     @endforeach 

正如你所看到的,我有一個隱藏的輸入從哪裏獲得就業的ID。現在,這個ID我都將它傳遞給模態窗口,我可以編輯記錄.. 我想將它傳遞給模態,以便我可以顯示當前值從數據庫中:

@foreach ($employment as $empl) 
    @if ($empl->id == $emplID) 
     <div class="form-group"> 
     <label for="company">Company:</label> 
     <input type="text" name="company" value="{{ $empl->company }}"> 
     </div> 

     <div class="form-group"> 
     <label for="month">From:</label> 
     <input type="date" name="from" value="{{ $empl->from }}"> 
     </div> 

     <div class="form-group"> 
     <label for="to">To:</label> 
     <input type="date" name="to" value="{{ $empl->to }}"> 
     </div> 
    @endif 
    @endforeach 

這是我在想這樣做,但我不知道如何傳遞$ EMPLID ......在我返回輪廓視圖控制器,我試圖通過它像這樣:

$emplID = Input::get('emplID'); 
return view('user.profile', compact(['employment','emplID'])); 

但是,如果我的DD($ EMPLID)我得到空出於某種原因...

+0

既然你沒有發佈任何代碼,沒有人可以給你一個具體的答案,但一般的想法是你在按鈕的某個地方存儲ID,你點擊打開模式,然後用它來提取你的信息需要。 – Samsquanch

回答

0

我認爲,當你將數據發送到您的看法,您與id和任何給他們別的你想要的。 id是最重要的,因爲它是獨一無二的。假設您的工作經歷包含在$history變量中。在你的模式,做

{!! Form::model($history, ['method' => 'PATCH', 'route' => ['history.update', $history->id]]) !!} 
    ... //your inputs here like company, role, description, etc. 
    ...submit button 
{!! Form::close() !!} 

當您提交它關係到你的控制器,並且定義了路由,鏈接到控制器的更新功能。爲了便於解釋,我將編制一些模型。

public function update(Request $request, $id) 
{ 
    $history = History::find($id); //check if the history exist 
    if (!$history) return redirect()->back(); 

    $history->company = $request->company; 
    ... 
    $history->save(); 

    return redirect()->back()->with('message', 'Successfull'); //Flash message. 
}