【问题标题】:Laravel, cant' get property of colletionLaravel,无法获得收藏的财产
【发布时间】:2020-01-27 22:55:40
【问题描述】:

我有我的表单 obj 的集合,我试图让所有页面都属于该表单, 当我这样做时$survey = Forms::find(68)->with('pages')->get(); 我得到了这个:

Illuminate\Database\Eloquent\Collection {#522 ▼
  #items: array:18 [▼
    0 => App\Models\Forms {#562 ▼
      #table: "forms"
      #fillable: array:4 [▶]
      #connection: "mysql"
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #attributes: array:9 [▶]
      #original: array:9 [▶]
      #changes: []
      #casts: []
      #dates: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: array:1 [▼
        "pages" => Illuminate\Database\Eloquent\Collection {#596 ▼
          #items: array:5 [▼
            0 => App\Models\Pages {#628 ▶}
            1 => App\Models\Pages {#632 ▶}
            2 => App\Models\Pages {#633 ▶}
            3 => App\Models\Pages {#634 ▶}
            4 => App\Models\Pages {#635 ▶}
          ]
        }
      ]
      #touches: []
      +timestamps: true
      #hidden: []
      #visible: []
      #guarded: array:1 [▶]
    }
   ]
}

但无法获得关系的属性,当我执行 $form->pages 时,我得到了 null 它适用于其他属性,例如问题(相同的关系)

这是我的模型:

class Forms extends Model
{
    protected $table = "forms";
    protected $fillable = array('name', 'description', 'status', 'token');


    public function pages()
    {
        return $this->hasMany('App\Models\Pages');
    }
   public function questions()
    {
        return $this->hasMany('App\Models\Question');
    }
}

class Pages extends Model
{
    protected $table = "pages";
    public $timestamps = false;
    protected $fillable = array('name', 'description' ,'priority', 'token');

    public function form()
    {
        return $this->belongsTo('App\Models\Forms');
    }

}

最后,我试图获得结果的方法:

   public function index()
    {

        $survey = Forms::with('pages')->find(68);//Did update regarding to sugestion

        dd($survey);


        return view("pages.surveys", compact('survey'));
    }

感谢您的帮助。 格雷格

【问题讨论】:

  • 你在哪里调用 $forms->pages ?
  • 我替换 dd($survey);到 dd($survey->pages);并得到 null ,否则得到上面的内容

标签: php laravel eloquent


【解决方案1】:

我认为您应该颠倒 with()find() 函数的顺序。

$survey = Forms::with('pages')->find(68);

请注意find() 实际执行查询,因此您不必再次使用get()。根据经验:如果您只需要从查询中得到一个结果,您应该使用first()find()(后者也可用于检索集合),否则使用all()get() 来获取结果集合。

【讨论】:

  • 仍然为空
  • 您写道您正在使用$form->pages 来获取结果,但您的变量已命名为$survey,这是问题中的拼写错误还是您错误地输入了错误的变量名称?跨度>
  • 那是错字,当我做 dd($survey);
  • 您能否在获取结果并尝试访问该属性的位置添加更多代码来更新问题?
  • 请见下文
【解决方案2】:
$form = Forms::find(68);
dd($form->pages);

$form = Form::find(68)->pages->get(); //It will return all the pages having relation

试试这个,它会首先找到具有 id '68' 的表单,然后根据给定的关系获取记录

【讨论】:

  • 在 null 上调用成员函数 get()
  • 应该是 Form::find(68)->pages()->get();
【解决方案3】:

似乎您的代码几乎都可以。这可能只是您的外键的问题。在表单中的页面关系中,laravel 将期望页面表中有一个外键,即“form_id”或“forms_id”,因为您没有在页面关系中传递第二个参数。确保在您的页面迁移中,您的外键也设置为 form_id

【讨论】:

  • 表格页面使用外键作为“forms_id”
  • 谢谢你的问题,我不得不更改方法名称或添加外键名称值
【解决方案4】:

当我做 dd($survey);

   App\Models\Forms {#513 ▼
  #table: "forms"
  #fillable: array:4 [▶]
  #connection: "mysql"
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  #perPage: 15
  +exists: true
  +wasRecentlyCreated: false
  #attributes: array:9 [▶]
  #original: array:9 [▶]
  #changes: []
  #casts: []
  #dates: []
  #dateFormat: null
  #appends: []
  #dispatchesEvents: []
  #observables: []
  #relations: array:1 [▼
    "pages" => Illuminate\Database\Eloquent\Collection {#521 ▼
      #items: array:6 [▼
        0 => App\Models\Pages {#552 ▼
          #table: "pages"
          +timestamps: false
          #fillable: array:4 [▶]
          #connection: "mysql"
          #primaryKey: "id"
          #keyType: "int"
          +incrementing: true
          #with: []
          #withCount: []
          #perPage: 15
          +exists: true
          +wasRecentlyCreated: false
          #attributes: array:6 [▶]
          #original: array:6 [▶]
          #changes: []
          #casts: []
          #dates: []
          #dateFormat: null
          #appends: []
          #dispatchesEvents: []
          #observables: []
          #relations: []
          #touches: []
          #hidden: []
          #visible: []
          #guarded: array:1 [▶]
        }
        1 => App\Models\Pages {#553 ▶}
        2 => App\Models\Pages {#554 ▶}
        3 => App\Models\Pages {#555 ▶}
        4 => App\Models\Pages {#556 ▶}
        5 => App\Models\Pages {#557 ▶}
      ]
    }
  ]
  #touches: []
  +timestamps: true
  #hidden: []
  #visible: []
  #guarded: array:1 [▶]
}

【讨论】:

  • 如此雄辩地得到正确的关系(我能够看到它)但是当我做 $survey->pages 时,我得到了 null
  • 试试这个 $form = Form::find(68)->pages;
  • 获取 null,只有当我执行 Form::find(68)->pages()->get();
  • 为什么当我使用 '->with' 方法时,如果它在 dd($survey) 的输出中列出,我无法获得属性?\
  • 我不想在上面使用的原因是我想要获得多个表单(通过特定 id 查找只是一个测试)所以如果我想计算特定表单的页面我需要做很多查询(对于每个循环)
猜你喜欢
  • 2012-12-14
  • 1970-01-01
  • 1970-01-01
  • 2020-11-25
  • 2012-10-17
  • 1970-01-01
  • 2018-04-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多