
ELOQUENT RELATIONSHIPS
Eloquent
relationships are defined as methods on your Eloquent model classes. Since
relationships also serve as powerful query builders,
defining relationships as methods provides powerful method chaining and
querying capabilities.
ONE TO ONE RELATIONSHIP
A
one-to-one relationship is a very basic type of database relationship. When one
table refers to single row in another table that is called a one-to-one
relationship. There should only be one matching record found in another table
where you have one-to-one relationship defined.
Firstly
we create tables for one to one relationship using migration method in laravel
framework
To create second table additionally to add Foreign
Key for table connectivity
public function up()
{
Schema::create('phones', function (Blueprint $table) {
$table->id();
$table->string('phone');
$table->unsignedBigInteger('user_id');
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
Then insert values in that tables