For getting every new tutorial link Please join our telegram group

Part 4 laravel hasOneThrough() relationship with example or video tutorial in hindi

In this laravel tutorial, we will discuss eloquent model hasOneThrow() relationship with a video in Hindi and source code explanation.

Also ask hasOneThrow() relationship not working, an example of hasOneThrow relationship, laravel hasOneThrow tutorial and laravel relationship to connect to tables or models which are not connected directly.

Laravel hasOneThrow() relationship with example

When we want to connect two models that are not connected directly but they connected throw a third model then use the laravel hasOneThrow() relationship. In simple words we have three tables the first table has a foreign key of the second table and the second table has a foreign key of the third table then we connect the first and third table by hasOneThrow() relationship. hasOneThrow throw relationship connects with all tables by one-to-one relationship.


Tables
orders => id, title, price
order_histories => id, order_id, address, mobile
payments => id, orders_history_id, amount, account_no

Let explain by an example we have three tables orders, order_histories and payments. orders table connect with order_histories table and order_histories connect with payments table then if we want to access Payment data by Order model then define hasOneThrow() relationship in Order model and pass first argument Payment model class and second argument OrderHistory model class.

Order model
<?

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

class Order extends Model {
    
    public function orderHistoryPayment() {
        return $this->hasOneThrough(Payment::class, OrderHistory::class);
    }
}
OrderController
<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Order;

class OrderController extends Controller{
    public function index(){
       $orders = Order::with('orderHistoryPayment')->get();

	dd($orders);
    }
}

Customize hasOneThrough() relationship when keys name changed

When we customize our table primary key and foreign key column name then pass both foreign keys in third and fourth argument and local keys in fifth and sixth argument see blow model code example with customizing key names.

<?php

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

class Order extends Model {
    
    public function orderHistoryPayment() {
        return $this->hasOneThrough(
        	Payment::class,
        	OrderHistory::class,
        	'orders_id', // Foreign key on the OrderHistory table
        	'orders_history_id', // Foreign key on the Payment table
         	'id', // Local key on the orders table
        	'id', // Local key on the order_histories table
        );
    }
}

You read this laravel tutorial on advanced web tutorial with hindi video. here we provide laravel beginners to advanced tutorial.

php laravel developer anmol sharma

Anmol Sharma

I am a software engineer and have experience in web development technologies like php, Laravel, Codeigniter, javascript, jquery, bootstrap and I like to share my deep knowledge by these blogs.

Related tutorial links