Skip to content

Instantly share code, notes, and snippets.

@alexweissman
Last active January 11, 2016 18:40
Show Gist options
  • Save alexweissman/bc82d664853ae83dc489 to your computer and use it in GitHub Desktop.
Save alexweissman/bc82d664853ae83dc489 to your computer and use it in GitHub Desktop.
hasOne relationship modeling in Eloquent
// $app->user is a \UserFrosting\User object
// This works
$tutor = \UserFrosting\Tutor::where('user_id', $app->user->id)->first();
error_log($tutor->first_name);
// This does not
error_log(print_r($app->user->tutor->first_name, true));
namespace UserFrosting;
class Tutor extends UFModel {
/**
* Get the User object for this Tutor.
*/
public function user() {
return $this->belongsTo('UserFrosting\User');
}
}
namespace UserFrosting;
// UFModel extends Eloquent\Model
class User extends UFModel {
public function tutor(){
return $this->hasOne('UserFrosting\Tutor');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment