Answers for "laravel has many through relationship"

3

has many through laravel

class Country extends Model
{
    public function posts()
    {
        return $this->hasManyThrough(
            'App\Post',
            'App\User',
            'country_id', // Foreign key on users table...
            'user_id', // Foreign key on posts table...
            'id', // Local key on countries table...
            'id' // Local key on users table...
        );
    }
}

when
countries
    id - integer
    name - string

users
    id - integer
    country_id - integer
    name - string

posts
    id - integer
    user_id - integer
    title - string
Posted by: Guest on June-28-2020
0

laravel relation has many

// Post model 
public function comments()
{
  return $this->hasMany(Comment::class);
}

// Post controller
$comments = Post::find(1)->comments;
Posted by: Guest on May-11-2021
0

laravel has many through relationship

projects
    id - integer
    name - string

environments
    id - integer
    project_id - integer
    name - string

deployments
    id - integer
    environment_id - integer
    commit_hash - string
Posted by: Guest on July-24-2021

Code answers related to "laravel has many through relationship"

Browse Popular Code Answers by Language