user1683333
user1683333

Reputation:

Laravel: Cannot retrieve an relation

It seems like Laravel 4 cannot get an relation when I use the ::where() syntax. So this is what I have:

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

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

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

The first method just works fine, but the second an third not.

These give the following error: "Undefined property: Illuminate\Database\Eloquent\Builder::$questions "

Any idea on how I can make this work? Thanks.

Upvotes: 3

Views: 2793

Answers (1)

Christian
Christian

Reputation: 436

Try:

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

Relationships only works from a model and not an array of models which you have when you use get()

Upvotes: 4

Related Questions