gpt4 book ai didi

php - 如何在 laravel 5.1 中通过 url(路由)传递两个参数 id 和 slug

转载 作者:行者123 更新时间:2023-12-02 00:37:28 25 4
gpt4 key购买 nike

很简单但是我不知道怎么做?我想在像“http://localhost:8000/blog/27/this-is-test-post”这样的 URL 中显示帖子 ID 和 slug,但它不起作用

我的路线

Route::get('blog/{id}/{slug}',['as'=>'blog.single','uses'=>'PageController@getSingle']);

我的 Controller

public function getSingle($id)
{
$article=Article::where('id','=','$id')->first();
return view('/articles/article')->withArticle($article);
}

“我想通过 id 获取数据,所以我在这里只使用 id”

我的看法

当我使用它时,URL 是完美的,但它显示错误“尝试获取非对象的属性”

<a href="blog/{{$article->id}}/{{$article->slug}}" > {{$article->title}}</a></h3>

当我使用它时

<a href="blog/{{$article->id}}{{$article->slug}}" > {{$article->title}}</a></h3>

然后我的 URL 像 http://localhost:8000/blog/27this-is-test-post (没有任何错误)

enter image description here

最佳答案

使用 route helper 。

route('blog.single', ['id' => $article->id, 'slug' => $article->slug]);

在您的链接中:

<a href="{{ route('blog.single', ['id' => $article->id, 'slug' => $article->slug]) }}">{{ $article->title }}</a>

关于php - 如何在 laravel 5.1 中通过 url(路由)传递两个参数 id 和 slug,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48815749/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com