Laravel Show方法未获取任何要显示的内容

46qrfjad  于 2023-03-04  发布在  其他
关注(0)|答案(1)|浏览(146)

呼叫显示方法

<td><a href="/pages/{{$page->id}}">{{$page->pages}}</a></td>

这是我的控制器

public function show(Posts $posts)
{

    //$id = $posts;
    //dd($id);
    return view('dashboard.demo',compact('posts',$posts));

}

当我尝试dd()时,我得到了这个

Posts {#227 ▼
  #fillable: array:13 [▶]
  #connection: null
  #table: null
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  #perPage: 15
  +exists: false
  +wasRecentlyCreated: false
  #attributes: []
  #original: []
  #changes: []
  #casts: []
  #dates: []
  #dateFormat: null
  #appends: []
  #dispatchesEvents: []
  #observables: []
  #relations: []
  #touches: []
  +timestamps: true
  #hidden: []
  #visible: []
  #guarded: array:1 [▶]
}

Laravel显示方法没有得到任何东西显示这是唯一的我得到了当我使用dd函数

5t7ly7z5

5t7ly7z51#

请像这样修改您的紧凑函数-

return view('dashboard.demo', compact('posts'));

我已经删除了**$posts**,因为您已经在函数中定义了posts,所以在契约中不需要它。
现在,确保你的路线像这样-

Route::get('pages/{posts}', 'YourController@show');

如果您使用了除posts之外的任何其他变量,那么您的show页面将无法工作。方法参数和路由参数名称应该匹配。
如果您使用的是Route::resource(),那么我确信您的路由是这样生成的-

Route::get('pages/{pages}', 'YourController@show);

在这种情况下,你需要像这样改变你的方法-

public function show(Posts $pages)
{
    return view('dashboard.demo', ['posts' => pages]);
}

相关问题