3
votes

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.

1

1 Answers

4
votes

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()