Skip to content

Instantly share code, notes, and snippets.

@prasanth22
Last active November 22, 2020 05:09
Show Gist options
  • Select an option

  • Save prasanth22/aaf540a71365aa672e023d0d9d84a9cb to your computer and use it in GitHub Desktop.

Select an option

Save prasanth22/aaf540a71365aa672e023d0d9d84a9cb to your computer and use it in GitHub Desktop.

one to one :
user can have profile or address user has one profile:
add this in user model as:

public function profile()
{
 return $this->hasOne(Profile::class);
}

now create this model with migration using:
`php artisan make:model Profile -m`

add fields to the profile model:

public function up()
  {
      Schema::create('profiles', function (Blueprint $table) {
          $table->bigIncrements('id');
          $table->unsignedInterger('user_id');
          $table->string('website_url');
          $table->string('github_url');
          $table->string('twitter_url');
          $table->timestamps();
      });
  }

php artisan migrate

Add data in the tables
use tinker to see in action as:
php artisan tinker
get user data from table as:
$user = App\User::first();
Because we have one to one relationship with user and profile we can use the following command to get user profie:
$user->profile
and $user->profile->github_url

to see the sql queries which are running behing the seens use:(in tinker)
DB::listen(function ($sql) { var_dump($sql->sql, $sql->bindings);});

We can also use
DB::enableQueryLog();
DB::getQueryLog();

one to many :
user can have many posts
add this line in user model

public function posts()
{
  return $this->hasMany(Post::class);
}

create model along migration:
php artisan make:model Post -m

add the following code in migration

public function up()
  {
      Schema::create('posts', function (Blueprint $table) {
          $table->bigIncrements('id');
          $table->unsignedInteger('user_id');
          $table->string('title');
          $table->text('body');
          $table->timestamps();
      });
  }

to test we can hard code in table or we can use factories as:
php artisan tinker
factory(App\User::class)->create();

for the user we already have user factory
, for post we can create using php artisan make:factory PostFactory -m Post

$factory->define(App\Post::class, function (Faker $faker) {
    return [
        'user_id' => function(){
		return factory(App\User::class)->create()->id;
		},
        'title' => $faker->sentence,
        'body' => $faker->sentence,
    ];
});

php artisan tinker

factory(App\Post::class)->create();

it will create the post and respective user in user table as well

we can also pass the user id manually by overiding
factory(App\Post::class)->create(['user_id' => 2]);

php artisan tinker
$user = App\User::find(2);
$user->posts

for one to one and one to many we say user can have one profile and user has many post to reverse it we say profile blongs to user and post blongs to user we represent them on profile model as

public function user()
{
  return $this->belongsTo(User::class);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment