在 Laravel 中从 <table> 获取特定数据(使用 Id)

Fetching particular data from <table> (using Id) in Laravel

查看:

<td>
 <div class="template-demo">
  <button type="button" onclick="location.href=' {{ route ('SupAd.View_PL_Accnt/{$id}') }}'"  class="btn btn-outline-info btn-icon-text">
   <i class="ti-search btn-icon-append"></i>View
  </button> 
 </td>

路线:

`Route::get('View_PL_Accnt', [SupAdController::class, 'View_PL_Accnt'])->name('SupAd.View_PL_Accnt/{$id}');`

控制器:

public function View_PL_Accnt(Request $request){
    $id = $request->id;
    $data = User::find($id);
    return view('dashboards.SupAd.pages.index.View_PL_Accnt', compact (['data', 'id']));
 }

View_PL_Accnt.blade.php :

<h3 class="SupAd_name">{{ data['name'] }}</h3>

错误:

使用未定义的常量数据 - 假定 'data'(这将在 PHP 的未来版本中引发错误)(查看:C:\Users\CuatrosMarias\Documents\GitHub\IPS\resources\views\dashboards\SupAd\pages\index\View_PL_Accnt.blade.php )

Error

拼写错误 {{ $data['name'] }} $ 在视图中丢失

您需要在控制器中使用 with() 发送变量

    return view('dashboards.SupAd.pages.index.View_PL_Accnt')->with('data',$data)->with('id',$id);

您的视图Accnt.blade.php您已将数据用作常量您需要将其用作变量

Eloquent 结果给出对象,因此您可以访问结果对象的名称 属性,如下所示

{{ $data->name }}

试试这个在我身边总是很好..

->with(compact('data', 'id'));

查看:

<button type="button" onclick="location.href='  {{ route ('SupAd.View_PL_Accnt', [$user->id]) }}'"  class="btn btn-outline-info btn-icon-text">
 <i class="ti-search btn-icon-append"></i>View
 </button>

路线:

 Route::get('View_PL_Accnt/{id}', [SupAdController::class, 'View_PL_Accnt'])->name('SupAd.View_PL_Accnt');

控制器:

public function View_PL_Accnt($id){
    $data = User::find($id);
    return view('dashboards.SupAd.pages.index.View_PL_Accnt', compact (['data', 'id']));

View_PL_Accnt.blade.php :

 <h3 class="SupAd_name">{{ $data->name }}</h3>