Answers for "laravel hasmanythrough example"

PHP
1

laravel hasmanythrough example

<?php
  
namespace App;

use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
    public function orders()
    {
        return $this->hasManyThrough(
            'App\Order',
            'App\Product',
            'category_id', // Foreign key on products table...
            'product_id', // Foreign key on orders table...
            'id', // Local key on categories table...
            'id' // Local key on products table...
        );
    }
}

// full answer is in source: https://xpertphp.com/laravel-hasmanythrough-eloquent-relationship-tutorial-example/
Posted by: Guest on August-03-2021
1

laravel has one through

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Mechanic extends Model
{
    /**
     * Get the car's owner.
     */
    public function carOwner()
    {
        return $this->hasOneThrough('App\Owner', 'App\Car');
    }
}
Posted by: Guest on July-14-2020
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

Browse Popular Code Answers by Language