Laravel:如何在路由中传递单个 ID
Posted
技术标签:
【中文标题】Laravel:如何在路由中传递单个 ID【英文标题】:Laravel: How can I pass a single ID in route 【发布时间】:2021-01-04 15:03:36 【问题描述】:我正在构建一个系统,以便用户可以请求多个事物,并且我希望能够可视化系统中提出的每个请求(单独)的详细信息。 当前的方法一次给了我所有东西,而不是分开。
这是部分代码
控制器
public function verDetalhesKeys($id)
$detalhes = DB::table('keysRequest')
->where('keysRequest.id','=', $id)
->select('keysRequest.*')
->get();
return view('Admin.keysRequestDetails',compact('detalhes'));
路线
Route::get('/detalhesK/id', 'AdminController@verDetalhesKeys') -> name('detalhesK');
显示细节的按钮
<table class="table table-striped">
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Department</th>
<th>Site</th>
<th>Pourpose</th>
<th>From</th>
<th>To</th>
</tr>
</thead>
<tbody>
@foreach($keys as $s)
<tr>
<td>$s->name</td>
<td>$s->email</td>
<td>$s->department</td>
<td>$s->site</td>
<td>$s->purpose</td>
<td>$s->till</td>
<td>$s->until</td>
<td style="display:none;">$s->id</td>
<td><a class="btn btn-info btn-md" href=" route('detalhesK',['id' => $s->id])" style="background-color:#00a3b3;"><i class="fa fa-eye" aria-hidden="true"></i></a></td>
</tr>
@endforeach
</tbody>
</table>
详情页
<div class="container">
<div class="row justify-content-center">
<div class="col-16 col-md-12 col-lg-12 pb-7">
<!--Form with header-->
<form method="post">
@foreach($detalhes as $d)
<div class="card border-primary rounded-0">
<div class="card-header p-0">
<div class="bg-info text-white text-center py-2">
<h3>Requisição de Chaves Detalhes || Keys Request Details</h3>
</div>
</div>
<div class="card-body p-3">
<!--Body-->
<div class="form-group">
<label>Nome || Name</label>
<input type="text" class="form-control" placeholder="$d->name" readonly>
</div>
<div class="form-group">
<label>Email</label>
<input type="email" class="form-control" placeholder="$d->email" readonly>
</div>
<div class="form-group">
<label>Departamento || Department</label>
<textarea class="form-control" placeholder="$d->department" readonly></textarea>
</div>
<div class="form-group">
<div class="input-group mb-2">
<label>Local || Site</label>
<textarea class="form-control" placeholder="$d->site" readonly></textarea>
</div>
<br>
<div class="form-group">
<label>Finalidade || Purpose</label>
<input type="Quantidade" class="form-control" placeholder="$d->purpose" readonly>
</div>
<div class="form-group">
<label>Período de uso das chaves || Period of use of the keys</label>
<textarea class="form-control" placeholder="$d->till - $d->until" readonly></textarea>
</div>
<div class="text-center">
<a class="btn btn-danger" href=""><font color="white"> Rejeitar || Refuse </font></a>
<a class="btn btn-success" href=""><font color="white"> Validar || Validate </font></a>
</div>
</div>
@endforeach
</div>
</form>
<!--Form with header-->
</div>
【问题讨论】:
所以你想做一个“比较产品”页面? 不,我想对上述产品页面做一些完整的细节 【参考方案1】:首先是“id”列到数据库表中的主键,您需要确保它是这样,这样您就不会拥有重复的产品。将您的查询编辑到$detalhes = DB::table('keysRequest')->find(id);
。
之后,不需要使用循环来输出您的结果,因为它只有一项。只需使用$detalhes->something
【讨论】:
以上是关于Laravel:如何在路由中传递单个 ID的主要内容,如果未能解决你的问题,请参考以下文章
将 ID 从 Laravel show 传递到 Datatables 路由