In this tutorial, today we discuss laravel hasOneThrough Eloquent relationship. Eloquent ORM means Object-relational Mapping and laravel provides a beautiful ActiveRecord structure. so we can easy to interact with the application database. let’s start about hasOneThrough eloquent relationship.

The “HasOneThrough” relationship links models through a single intermediate relation. For example, if each category has one product, and each product is associated with one product order record, then the category model may access the order through the product.

here, see below database structure.

Products

– id

– category_id

– title

Categories

– id

– name

Orders

– id

– product_id

Setting Database Configuration

After complete installation of laravel. we have to database configuration. now we will open the .env file and change the database name, username, password in the .env file. See below changes in a .env file.

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=Enter_Your_Database_Name(has_one_through_relation)
DB_USERNAME=Enter_Your_Database_Username(root)
DB_PASSWORD=Enter_Your_Database_Password(root)

Create Table using migration

Now, We need to create a migration. so we will below command using create the products, categories and orders table migration.

php artisan make:migration create_products_table --create=products
php artisan make:migration create_categories_table --create=categories
php artisan make:migration create_orders_table --create=orders

After complete migration. we need below changes in the database/migrations/create_products_table, database/migrations/create_categories_table and database/migrations/create_orders_table file.

create_products_table.php

<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateProductsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('products', function (Blueprint $table) {
            $table->bigIncrements('id');
			$table->bigInteger('category_id')->unsigned()->index();
            $table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
            $table->string('title');
            $table->timestamps();
        });
    }
    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('products');
    }
}
?>

create_categories_table.php

<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateCategoriesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('categories', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name');
            $table->timestamps();
        });
    }
    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('categories');
    }
}
?>

create_orders_table.php

<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateOrdersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('orders', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->integer('product_id')->unsigned();
            $table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
            $table->timestamps();
        });
    }
    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('orders');
    }
}
?>

Run the below command. after the changes above file.

php artisan migrate

Create Model

Here below command help through we will create the Product, Category and Order model. we will also use “hasOneThrough()” method relations based on the foreign key between any two table.

php artisan make:model Product
php artisan make:model Category
php artisan make:model Order

Product.php

<?php

namespace App;
use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    public function orders()
    {
        //return $this->hasOneThrough('App\Order', 'App\Product');
		
		
		return $this->hasOneThrough(
            '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...
        );
    }
}

?>

Category.php

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
    
}
?>

Order.php

<?php
 
namespace App;
 
use Illuminate\Database\Eloquent\Model;
 
class Order extends Model
{
    //
}

?>

Route and Controller

We have to need put below code route in routes/web.

<?php
 
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
 
Route::get('/', function () {
    return view('welcome');
});
 
Route::get('product','productsController@index');

?>

Here below command help to create the product controller.

php artisan make:controller ProductsController

ProductsController.php

<?php
namespace App\Http\Controllers;
use App\Product;
use App\Category;
use Illuminate\Http\Request;
class ProductsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $product = Product::find(1);	
        $supplier_one = $product->orders;
        //dd($supplier_one);
    }

}
?>