0

I'm trying to make this small CV type profile with Laravel and I encountered this problem that I'm not sure how it should be resolved, the correct way. In the user profile, I have a category where the user can add Employment History. Obviously, a user can add multiple places where he worked. I do this using modals and it works, I can store them in the database.

The thing is, now I want the user to be able to edit what he entered. So, I made an edit button which triggers a modal window where he can edit the database record. What I don't know is how I can take the specific id of the record so I can populate the modal window and then save any changes.

To sum this up because I'm not sure if I was clear enough..

I have 3 entries in DB for Employment History + 3 Edit links for each entry. Then, that Edit link should lead to a modal window of the specific entry where the user can edit it.

EDIT:

I got to a point after following some of your help.. However, I'm stuck again..

So, I have this employment history in the user profile, here's how I display it:

      @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

As you can see, I have a hidden input where I get the employment id.. Now, this id I have to pass it to the modal window where I can edit the records.. I want to pass it to the modal so I can display the current values from the database:

  @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

This is how I was thinking to do so but I'm not sure how to pass that $emplID... In the controller where I return the profile view, I tried to pass it like this:

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

But if I dd($emplID) I'm getting null for some reason...

Ovidiu G
  • 1,253
  • 5
  • 25
  • 47
  • Since you didn't post any code no one can give you a specific answer, but the general idea is you store the ID somewhere in the button you click to open the modal and then use that to pull in the information you need. – Samsquanch Feb 22 '17 at 20:47

2 Answers2

0

I assume when you send data to your views, you send them with id and anything else you want. id is the most important since it's unique. Let's assume your Employment History is contained in the $history variable. In your modal, do

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

When you submit it goes to your controller and you define a route, linked to the update function of that controller. I will just make up some models for ease of explanation.

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.
}
EddyTheDove
  • 12,979
  • 2
  • 37
  • 45
0

if you just want to pass the variable id to the modal window this will help you. https://stackoverflow.com/a/10635652/4668162

Community
  • 1
  • 1
Onix
  • 2,129
  • 2
  • 17
  • 26