我在数据库中有一些大于1的数据。当我单击按钮href并将其传递到另一个视图时,我想传递数据,但是只接受比单击的数据。有办法吗?
@foreach ($data as $datas)
<div class="card">
<img src={{$datas->gambar}} alt="Avatar" style="width:100%">
<div class="container">
<h4><b>{{$datas->judul}}</b></h4>
<p>{{$datas->artis}}</p>
<p>Stock = {{$datas->stok}}</p>
</div>
<a href="/details " class="button">Rental</a>
</div>
@endforeach
答案 0 :(得分:1)
在链接中添加$data
ID,例如:
<a href="/details/{{ $data->id }}" class="button">Rental</a>
然后,您将需要一个接受id
作为参数的相应路由:
Route::get('/details/{id}', 'DataController@show');
当然还有控制器方法的实现:
// DataController
public function show(Request $request, $id) {
return view('data.show', ['single_data' => Data::find($id)]);
}
更新
如果您要使用与id
以外的其他字段作为主键,请相应地更新模型:
class Data extends Model {
protected $primaryKey = 'judul';
}
答案 1 :(得分:0)
// view
@foreach ($data as $datas)
<div class="card">
<img src={{$datas->gambar}} alt="Avatar" style="width:100%">
<div class="container">
<h4><b>{{$datas->judul}}</b></h4>
<p>{{$datas->artis}}</p>
<p>Stock = {{$datas->stok}}</p>
</div>
<a href="{{ route('details',$datas->your_value) }}" class="button">Rental</a>
</div>
@endforeach
//route
Route::get('/details/{data}', ['uses'=>'YourController@your_func', 'as'=>'details']);
// Controller
public function your_func($data){
return view('your_view')->with('data',$data);
}
//next view
{{ dd($data) }}