Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.


1 Answers

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

like image 171
Christian Avatar answered Jan 24 '26 19:01

Christian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!