Answers for "one to many laravel relationship"

PHP
0

1 to many relationship in Laravel

class Post extends Model
{
   // 1 Post has many comments
    public function comments()
    {
        return $this->hasMany(Comment::class);
    }
}
Posted by: Guest on July-18-2021
1

many to many relationship laravel

use AppModelsUser;

$user = User::find(1);

$user->roles()->attach($roleId);
Posted by: Guest on February-13-2021
0

many to many relationship laravel example

<?php namespace App; use IlluminateDatabaseEloquentModel; class UserRole extends Model{     }
Posted by: Guest on July-27-2021
0

one to many laravel

Suppose you have a Post model with a hasMany relationship with Comment. You may insert a Comment object related to a post by doing the following:

$post = Post::find(1);
$commentToAdd = new Comment(['message' => 'This is a comment.']);
$post->comments()->save($commentToAdd);
Posted by: Guest on July-14-2021

Code answers related to "one to many laravel relationship"

Browse Popular Code Answers by Language