[英]Laravel pass ID back from controller to view via redirect with errors
How can I pass $id back from controller to the same view when update is failed and error is trigerred. 更新失败且引发错误时,如何将$ id从控制器传递回同一视图。
I have view which prints all items from database and also adds edit button to each of the items which triggers modal popup window. 我有一个视图,它可以打印数据库中的所有项目,还可以向每个项目添加编辑按钮,从而触发模式弹出窗口。
@for ($i =0; $i < count($inventory); $i++)
<tr>
<td> {{ $inventory[$i]->name }} </td>
<td>
<button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal{{ $inventory[$i]->id }}">
Edit
</button>
After editting all the data are passed to route via form 编辑后,所有数据都通过表单传递到路径
<form action="/updateInventory/{{$inventory[$i]->id}}" method="post">
Then it goes to my controller which validates input and then inserts input into database. 然后转到我的控制器,该控制器验证输入,然后将输入插入数据库。
public function update(Request $req,$id)
{
$this->validate($req, [
'name'=> 'min:2'
]);
$inventory = inventory::find($id);
$inventory->name = $req->input('name');
$response = $inventory->save();
if($response)
{
return redirect()->back()->with(['message'=>'gerai']);
}
return redirect()->back()->withErrors(['error'=>'negerai']);
//return redirect('/inventory');
}
But if input doesn't pass validation I'm printing error like that. 但是,如果输入未通过验证,我将输出类似的错误。
@include ('partials.notice')
@if($errors->any())
<script>
$(function() {
$('#myModal{{$id}}').modal('show');
});
</script>
@endif
How can I pass $id of element I just edited from controller back to the same view so when @if($errors->any()) triggers I can popup my modal $('#myModal{{$id}}').modal('show'); 如何将刚从控制器编辑的元素的$ id传递回相同的视图,因此当@if($ errors-> any())触发时,我可以弹出模式$('#myModal {{$ id}}') .modal( '节目'); with element I wanted to edit.
与我要编辑的元素。
In Laravel you can flash data into the Session. 在Laravel中,您可以将数据刷新到Session中。 In your example you can do something similar to:
在您的示例中,您可以执行以下操作:
$request->session()->flash('idOfError', $id);
You can access it with : 您可以通过以下方式访问它:
@if($errors->any())
<script>
$(function() {
$('#myModal{{session()->get("idOfError")}}').modal('show');
});
</script>
@endif
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.