Last active
January 11, 2016 18:40
-
-
Save alexweissman/bc82d664853ae83dc489 to your computer and use it in GitHub Desktop.
hasOne relationship modeling in Eloquent
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// $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)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace UserFrosting; | |
class Tutor extends UFModel { | |
/** | |
* Get the User object for this Tutor. | |
*/ | |
public function user() { | |
return $this->belongsTo('UserFrosting\User'); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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