编辑多个记录

时间:2017-02-22 19:06:33

标签: laravel laravel-5

我正在尝试使用Laravel创建这个小型CV类型的配置文件,我遇到了这个问题,我不确定应该如何解决,正确的方法。 在用户个人资料中,我有一个用户可以添加就业历史的类别。 显然,用户可以添加他工作的多个地方。我这样做是使用模态,它可以工作,我可以将它们存储在数据库中。

问题是,现在我希望用户能够编辑他输入的内容。所以,我创建了一个编辑按钮,触发一个模态窗口,在那里他可以编辑数据库记录。 我不知道的是如何获取记录的特定id,以便我可以填充模态窗口然后保存任何更改。

总结一下,因为我不确定我是否足够清楚......

我在DB中有3个条目的就业历史+ 3个每个条目的编辑链接。 然后,该Edit链接应该指向特定条目的模态窗口,用户可以在其中对其进行编辑。

编辑:

在得到你的一些帮助之后,我得到了一点。但是,我再次陷入困境......

所以,我在用户档案中有这个就业历史,这是我如何展示它:

      @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)我因某种原因而变空了......

2 个答案:

答案 0 :(得分: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.
}

答案 1 :(得分:0)

如果您只想将变量id传递给模态窗口,这将对您有所帮助。 https://stackoverflow.com/a/10635652/4668162