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.
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
- part 1 laravel hasOne() relationship
- part 2 laravel one to many relationship
- part 3 Has One Of Many relationship
- part 4 laravel hasOneThrough relationship
- part 5 laravel hasManyThrow() relationship
- part 6 laravel many to many relationship
- part 7 one to one polymorphic relationship
- part 8 laravel one to many polymorphic relationship
- part 9 many-to-many polymorphic relationship
- part 10 laravel eloquent has vs whereHas method
- part 11 laravel whereDoesntHave and whereDoesntHave
- part 12 laravel withCount() method
- part 13 eager vs lazy loading