laravel - 分配 ID 以在表上创建“查看详细信息”以链接到视图
问题描述
我有一个表格,想为每一行添加一个“查看详细信息”按钮,该按钮链接到更多详细信息页面,我很容易添加了该按钮,但我正在寻找一些帮助,以便能够使其打开它的特定消息也有关系。
我目前的看法:
@extends ('layout')
@section('content')
<script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="/filtering.js"></script>
<link href="https://nightly.datatables.net/css/jquery.dataTables.css" rel="stylesheet" type="text/css" />
<script src="https://nightly.datatables.net/js/jquery.dataTables.js"></script>
<script src="https://code.jquery.com/jquery-3.3.1.js"> </script>
<script src="https://cdn.datatables.net/1.10.20/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/fixedheader/3.1.6/js/dataTables.fixedHeader.min.js"></script>
<div class="row align-items-center">
<div class="col">
<h1>
MESSAGES R US
</h1>
</div>
<div class="col ">
<a href="/create" class="btn btn-primary">CREATE MESSAGE</a>
</div>
<div class="col ">
<a href="/details" class="btn btn-primary">VIEW DETAILS</a>
</div>
</div>
<div class="form-group"> <!-- This works-->
<table id="userTable" data-page-length='5' cellspacing="0"
class="table table-bordered table-striped table-hover table-condensed"
role="grid">
<thead>
<tr>
<th scope="col">CREATED</th>
<th scope="col">USERNAME</th>
<th scope="col">TO/FROM</th>
<th scope="col">UPDATED</th>
<th scope="col">MESSAGE ID</th>
<th scope="col">STATUS</th>
<th scope="col">ACTION</th>
</tr>
</thead>
<tbody>
@foreach ($messages as $message)
<tr>
<td>{{$message->created_at}}</td>
<td>{{$message->username}}</td>
<td class="text-center">
@if ($message->direction == 'from')
<span class='badge badge-warning'>from</span>
@else
<span class='badge badge-success'>{{$message->direction}}</span>
@endif
</td>
<td>{{$message->updated_at}}</td>
<td>{{$message->message_id}}</td>
<td class="text-center">
@if ($message->status == 'FAIL')
<span class='badge badge-danger'>FAIL</span>
@elseif ($message->status == 'received')
<span class='badge badge-info'>received</span>
@elseif ($message->status == 'delivered')
<span class='badge badge-primary'>delivered</span>
@elseif ($message->status == 'queued')
<span class='badge badge-warning'>queued</span>
@elseif ($message->status == 'read')
<span class='badge badge-success'>read</span>
@endif
</td>
</td>
<td>
<div class="col "><a href="/details" class="btn btn-primary">VIEW</a></div>
</td>
</tr>
@endforeach
</tbody>
</table>
</div
@endsection
我在 Laravel 中构建了这个,当单击 VIEW 时,它只显示同一个表,而不是特定于消息的行,我不知道如何链接它以进一步显示 MESSAGE ID 详细信息。这是一个休闲项目,所以请原谅格式和 UI。我以前使用 ASP.NET MVC 是 laravel 的新手。任何帮助表示赞赏。
我理解我需要让它发挥作用的概念。使用 message_ID 作为按钮的键,然后使用该表数据 (MESSAGE_ID - MESSAGE_TEXT) 我只需要帮助来连接点。:)
解决方案
首先,您必须在您的路线中声明“详细路线”。你的路线可能看起来像这样
Route::get('/message/{id}/details', 'YourController@details')->name('message.detail');
然后在您的视图中,该按钮将被称为此路线。所以你的按钮视图看起来像
<a href="{{ route('message.detail', $message->id) }}" class="btn btn-primary">VIEW</a>
最后在你的控制器中。你必须有你之前在路由中声明的方法(在我的例子中,方法名称是“details”)。
public function details(Request $request, $id){
// your detail code here
}