繁体   English   中英

根据 Id 打开 Laravel 链接

[英]Opening Laravel links based on their Id

我创建了一个 laravel 博客,我想使用以下 laravel 链接在他们自己的页面上打开每一篇文章。

 <a href="{{route('about.show'.$about_us)}}" class="text-success text-decoration-none">{{$about_us->button}}</a> which I trying to open using the show method and the routes. This is how I have set up the show method and it's route.

public function show($id)
{
    //
    return view('about.show')
}

Route::get('/about.show','App\Http\Controllers\AboutController@show')->name('about.show');

当我尝试时,它给了我一个错误 Route [about.show{"id":1,"title":"My Agenda","description":"My Four",... not defined。

当我尝试使用它显示的 index 方法打开它时,它会选择两个 id。 当我删除.$about_us 时,它会显示一个空白页。 这是一个菜鸟问题,但我很挣扎。 谁能帮忙

我不知道您的代码,所以这就是为什么我给出一些示例如何实现这件事。

Route::get('/show/{id}','App\Http\Controllers\AboutController@getPostById');
public function getPostById($id){
  $request=app('request');
  $data=Model::where('id',$request->id)->get();
  return response()->json([$data]);
}
  1. 您需要在路由中接受参数。 Route::get('/show/{id}','App\Http\Controllers\AboutController@getPostById')->name('about.show');
  2. 正如我在您的代码中看到的,您的$about_us似乎是 object。 因此,您不能将整个 object 作为参数传递给命名路由。 而是喜欢.. <a href="{{route('about.show', $about_us->id)}}" class="text-success text-decoration-none">{{$about_us->button}}</a>
  3. 最后在您的getPostById() function 中,您可以接受该 id 变量。 public function getPostById($id){dd($id);}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM