【问题标题】:Laravel ->with() can't be shown in blade (Undefined property)Laravel ->with() 不能在刀片中显示(未定义的属性)
【发布时间】:2017-06-11 22:06:13
【问题描述】:

我正在尝试使用 laravel 5.3 将文章从我的控制器传递到我的视图 如果我只使用 {{ $selectedArticle }} 我会得到完整的输出:

[{"id":5,"title":"Up up and awayy",
  "link":"www.google.be",
  "points":0,
  "userID":1,
  "isDeleted":"FALSE",
  "created_at":"2017-01-25 23:53:19",
  "updated_at":"2017-01-25 23:53:56"
}]

但是当我尝试使用$selectedArticle->id 时,我收到以下错误:

未定义属性:Illuminate\Support\Collection::$id

如何分别调用文章的属性? (调用 id、title、...)

我的控制器代码:

public function index($id)
{

    $comments = DB::table('comments')->orderBy('id')->where(['artikelID' => $id, 'isDeleted' => 'FALSE'])->get();
    $article = DB::table('articles')->orderBy('id')->where(['id'=> $id])->get();
    return view('comments.comments')
        ->with('storedComments', $comments)
        ->with('artikelid',$id)
        ->with('selectedArticle', $article);
}

【问题讨论】:

    标签: arrays laravel-5.3 blade laravel-blade


    【解决方案1】:

    您传递给视图的变量是一个数组,要显示值,您应该像这样循环遍历数组。

    @foreach($selectedArticle as $article)
    
        {{ $article->id }}
    
    @endforeach
    

    或者,如果您只想使用first() 函数选择一个对象来获取第一行,如下所示:

    $article = DB::table('articles')->orderBy('id')->where(['id'=> $id])->first();
    

    【讨论】:

      猜你喜欢
      • 2020-07-14
      • 2019-06-28
      • 2018-03-12
      • 2016-06-26
      • 2023-04-08
      • 2020-03-18
      • 1970-01-01
      • 2018-11-26
      • 1970-01-01
      相关资源
      最近更新 更多