Answers for "laravel hasMany relationship"

PHP
0

hasmany relationship with created at in laravel example

return $this->hasMany('App\Comment', 'foreign_key');

return $this->hasMany('App\Comment', 'foreign_key', 'local_key');
Posted by: Guest on August-02-2021
7

one to many laravel

For example, a blog post may have an infinite number of comments. And a single
comment belongs to only a single post  

class Post extends Model
{
    public function comments()
    {
        return $this->hasMany('App\Models\Comment');
    }
}

class Comment extends Model
{
    public function post()
    {
        return $this->belongsTo('App\Models\Post');
    }
}
Posted by: Guest on October-16-2020
3

laravel belongs to

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Phone extends Model
{
    /**
     * Get the user that owns the phone.
     */
    public function user()
    {
        return $this->belongsTo('App\Models\User');
    }
}
Posted by: Guest on November-01-2020
0

laravel has many

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    /**
     * Get the comments for the blog post.
     */
    public function comments()
    {
        return $this->hasMany('App\Models\Comment');
    }
}
Posted by: Guest on October-27-2020
0

laravel hasMany relationship

// hasMany
$this->hasMany(Model::class);
// invers 
$this->belongsTo(Model::class);
Posted by: Guest on October-22-2021
1

eloquent relationships

$roles = App\User::find(1)->roles()->orderBy('name')->get();
Posted by: Guest on June-15-2020

Code answers related to "laravel hasMany relationship"

Browse Popular Code Answers by Language