Logo

dev-resources.site

for different kinds of informations.

How to insert data in one to one relationship in database?

Published at
9/18/2023
Categories
laravel10
eloquent
relationships
howto
Author
devmahmoudadel
Author
14 person written this
devmahmoudadel
open
How to insert data in one to one relationship in database?

After we created a one-to-one relationship between the users table and the profiles table, and added the hasOne() method to the User model, and the belongsTo() method to the Profile model, it's time to find out how the data is saved in the database when we use this relationship. And what are the methods used for that?

These methods are divided into three main ways:

  1. Without using method profile.
  2. By using method profile.
  3. By using the inverse method user.

The best method to use depends on the specific needs of your application.
If you only need to save the profile associated with the user, then the first method is the simplest option.
If you need to get, update, or delete the profile, then the second method is a better option.
If you need to get, update, or delete the user, then the third method is a better option.

1. Without using method profile.

  • We first go to the routes/web.php file and add a new route so that we can test these method.
use App\Models\Profile;
use App\Models\User;
---
Route::get('/one-to-one', method () {
    $user = User::create(['username' => 'John Doe']);
    Profile::create([
        'user_id' => $user->id,
        'firstname' => 'John',
        'lastname' => 'Doe',
        'birthday' => '08-11-1991'
    ]);
    return response()->json([
        'username' => $user->username,
        'firstname' => $user->profile->firstname,
        'lastname' => $user->profile->lastname,
    ]);
});
Enter fullscreen mode Exit fullscreen mode
  • We opened the browser and went to the link http://127.0.0.1:8000/one-to-one. To our satisfaction, the user had been created successfully.
{
  "username": "John Doe",
  "firstname": "John",
  "lastname": "Doe"
}
Enter fullscreen mode Exit fullscreen mode

2. By using method profile.

  • We first go to the routes/web.php file and edit this route.
Route::get('/one-to-one', method () {
    $user = User::create(['username' => 'Tom Cruz']);
    $user->profile()->create([
      'firstname' => 'Tom',
      'lastname' => 'Cruz',
      'birthday' => '01-02-2000'
    ]);
    return response()->json([
        'username' => $user->username,
        'firstname' => $user->profile->firstname,
        'lastname' => $user->profile->lastname,
    ]);
});
Enter fullscreen mode Exit fullscreen mode
  • We open the browser again and go to this link: http://127.0.0.1:8000/one-to-one to find that the user has been created successfully.
{
  "username": "Tom Cruz",
  "firstname": "Tom",
  "lastname": "Cruz"
}
Enter fullscreen mode Exit fullscreen mode

3. By using the inverse method user.

  • We first go to the routes/web.php file and update this route.
Route::get('/one-to-one', method () {
    $user = User::create(['username' => 'Adam Smith']);
    $profile = new Profile([
        'firstname' => 'Adam',
        'lastname' => 'Smith',
        'birthday' => '01-01-1999'
    ]);

    $profile->user()->associate($user)->save();
    return response()->json([
        'username' => $profile->user->username,
        'firstname' => $profile->firstname,
        'lastname' => $profile->lastname,
    ]);
});
Enter fullscreen mode Exit fullscreen mode
  • We open the browser again and go to this link: http://127.0.0.1:8000/one-to-one to find that the user has been created successfully.
{
  "username": "Adam Smith",
  "firstname": "Adam",
  "lastname": "Smith"
}
Enter fullscreen mode Exit fullscreen mode
  • You can find the repo of this series on github here
eloquent Article's
30 articles in total
Favicon
Laravel Eloquent ORM in Bangla Part-3 (Models Retrieving)
Favicon
Prunable Eloquent Models
Favicon
Creating a Mini Blog API with Lithe and Eloquent
Favicon
Criando uma API de Mini Blog com Lithe e Eloquent
Favicon
Understanding Self-Relationships in Laravel Models: A Simple Guide
Favicon
Eloquent Trick: Laravel Model from Subquery
Favicon
Eloquent ORM: Accessor and Mutator
Favicon
Implementing User Suspension in Your Laravel Application
Favicon
Streamlining Eloquent Queries: Mastering User Scopes and Global Scopes in Laravel
Favicon
Getting data from resource responses before sending it to the client
Favicon
Including extra meta data with a resource response
Favicon
A Quick Intro To Eloquent Observers
Favicon
Customizing API Resource Pagination Structure
Favicon
What’s New in Laravel 11?
Favicon
Observing your integrity
Favicon
Laravel route binding for finite objects
Favicon
Simplify Slug Creation for Eloquent Models in Laravel
Favicon
Laravel: API Resources: With or Without "data"?
Favicon
Optimizing Laravel Eloquent queries
Favicon
Using Laravel push method to update your model and its relationships
Favicon
Laravel101: Exploring Entity-Model Relationships
Favicon
How to delete data from one to many relationship in Laravel?
Favicon
How to update a one-to-many relationship in Laravel?
Favicon
How can you retrieve data from a one-to-many relationship in Laravel?
Favicon
How to insert data in one to many relationship in database?
Favicon
How to create a One-To-Many relationship in Laravel?
Favicon
How to delete data from one to one relationship in Laravel?
Favicon
How to update a one-to-one relationship in Laravel?
Favicon
How can you retrieve data from a one-to-one relationship in Laravel?
Favicon
How to insert data in one to one relationship in database?

Featured ones: