【问题标题】:Trying to get details of posts in single page试图在单页中获取帖子的详细信息
【发布时间】:2020-06-13 11:10:55
【问题描述】:

我正在尝试在单个页面中获取帖子内容的详细信息,并且我的 web.php 中有此内容

Route::get('/{category}/{post}','PropertyController@show')->name('posts.show');

我的控制器里有这个

public function show(Property $property)
    {
        $title = "Single Post";
        return view('properties.show', compact('title','property'));
    }

在我的刀片中我也有这个<h4>{{$property->title}}</h4>

但它显示的是空标签而不显示标题

我做错了什么?

【问题讨论】:

  • 如果你要使用路由模型绑定,你会希望路由参数名和方法的变量名匹配,否则你只会得到一个新的模型实例作为依赖.. .post != property

标签: php laravel


【解决方案1】:

变量名与方法不匹配 您可以更新代码以使其匹配,然后将标题作为数组属性的元素传递。

  public function show($post)
    {
      //Use the $post to get property info
      $property = [] //you can fetch this from your db
      $property['title'] = "Single Post";
      return view('properties.show', compact($property,'property'));
    }

【讨论】:

    【解决方案2】:

    您可以在 html 中显示标题,例如:

    <h4>{{$title}}</h4>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-05
      • 2021-01-20
      • 1970-01-01
      • 2011-04-21
      • 1970-01-01
      • 2018-11-28
      • 1970-01-01
      • 2022-07-27
      相关资源
      最近更新 更多