【问题标题】:Laravel: Cannot retrieve an relationLaravel:无法检索关系
【发布时间】:2023-03-26 14:55:01
【问题描述】:

当我使用 ::where() 语法时,Laravel 4 似乎无法获得关系。所以这就是我所拥有的:

<?php
  // works fine
  $questions = Auth::user()->questions;

  // error
  $questions = User::where('profilename', '=', $username)->questions;

  // error
  $questions = User::where('profilename', '=', $username)->get()->questions;
?>

第一种方法很好用,但第二种和第三种不行。

这些给出以下错误: "未定义属性:Illuminate\Database\Eloquent\Builder::$questions"

知道如何使这项工作发挥作用吗?谢谢。

【问题讨论】:

    标签: php laravel laravel-4 eloquent


    【解决方案1】:

    试试:

    $questions = User::where('profilename', '=', $username)->first()->questions;
    

    关系仅适用于模型,而不适用于使用 get() 时拥有的模型数组

    【讨论】:

      猜你喜欢
      • 2018-01-06
      • 2013-09-14
      • 2021-04-06
      • 1970-01-01
      • 2022-01-10
      • 2020-08-23
      • 2015-03-26
      • 2020-08-20
      • 2013-06-06
      相关资源
      最近更新 更多