【问题标题】:How to get Current page Title of Relationship Model in Laravel?如何在 Laravel 中获取关系模型的当前页面标题?
【发布时间】:2020-03-16 11:08:24
【问题描述】:

我的数据库表中有一些字段,我想根据每个页面显示titledescriptionkeywords,请告诉我如何显示这个。我的数据库字段名称是metatitle,metadesc,keyword

这是我显示属性列表的控制器代码...

    public function listtype($slug){
    $prtype= Listing::whereHas('proType',function($q) use($slug){
        $q->where('slug','like','%'.$slug.'%');
    })->paginate(50);
    return view('property-type',compact('prtype','title'));
}

【问题讨论】:

  • 你的Listing模型有metatitle,metadesc,keyword字段吗??
  • 我想要这个proType的标题,我可以很容易地从listing模型中得到标题。但我很难得到title from porType`
  • @sainitor 你可以使用with()方法。

标签: laravel eloquent laravel-6 laravel-6.2


【解决方案1】:

你可以这样做:

 public function listtype($slug){
    $prtype= Listing::whereHas('proType',function($q) use($slug){
        $q->where('slug','like','%'.$slug.'%');
    })->with('proType')->paginate(50);
    dd($prtype); /** for checking **/ 
    return view('property-type',compact('prtype', $prtype));
 }

【讨论】:

  • 谢谢兄弟..你太棒了......你解决了我的问题......我从过去的 5 个小时就陷入了困境。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-02
  • 1970-01-01
  • 2022-01-22
  • 1970-01-01
  • 2012-03-02
相关资源
最近更新 更多