laravel hasone relation code example
Example 1: laravel hasOne
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
/**
* Get the phone record associated with the user.
*/
public function phone()
{
return $this->hasOne('App\Phone');
}
}
Example 2: eloquent relationships
$roles = App\User::find(1)->roles()->orderBy('name')->get();
Example 3: laravel relation has one
// User model
public function phone()
{
return $this->hasOne(Phone::class);
}
// User controller
$phone = User::find(1)->phone;