How can i create relationship with Eloquent ORM Relationship?
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Profiles extends Model
{
protected $table = 'profiles';
protected $fillable = [
'user_id', 'phone', 'address'
];
}
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Users extends Model
{
protected $table = 'users';
protected $fillable = [
'name', 'email', 'password'
];
public function profile()
{
return $this->hasOne(Profiles::class);
}
}
$app->get('/create_user_profile', function($request, $response){
$user = Users::find(2);
$profile = new Profiles([
'phone' => '02183129312931',
'address' => 'Kota Magelang'
]);
$user->profile()->save($profile);
return $user;
});