![Theme Switcher Image](https://stackcoder.in/images/theme-switcher.png?cb=stackcoder90070)
Good content takes time and effort to come up with.
Please consider supporting us by just disabling your AD BLOCKER and reloading this page again.
Many times you might want to use this wonderful thing called Route Model Binding in Laravel but back off because you might need its related table data too. Yup! I too had come across this situation.
Example - You want to show Customer details and CustomerAddress details so you would have done something like the following
public function show($customer)
{
$customer = Customer::with('customerAddress')->where('id', $customer)->first();
return view('customer.show', [
'customer' => $customer
]);
}
But let me explain 2 simple approaches to achieve it.
public function show(Customer $customer)
{
$customerAddress = CustomerAddress::where('id', $customer)->get();
return view('customer.show', [
'customer' => $customer,
'customerAddress' => $customerAddress
]);
}
Now lets see the simple way
load
)public function show(Customer $customer)
{
/** Super Awesome Right? */
$customer->load(['customerAddress']);
return view('customer.show', [
'customer' => $customer
]);
}
Yup the following is the super cool line.
$customer->load(['customerAddress']);
Make sure to use this wisely as it will load all the related table every time you query.
class Customer extends Model
{
use SoftDeletes;
protected $with = ['customerAddress'];
}
Hope the article was helpful. Kindly share it with your friends.
Lazy Load Images In Chrome With This Simple Trick
Global Data In All Laravel Blade Pages
Generate RSS Feeds in PHP Laravel
Free SSL Certificate For CPanel
Create / Save / Download PDF From Blade Template In PHP Laravel
Redirect www to a non-www Website Or Vice Versa
Move Uploaded Files From Local Computer Or Server To Amazon S3 Bucket In PHP
Localization In Laravel REST API
SummerNote WYSIWYG Text Editor Save Images To Public Path In PHP Laravel
Laravel 7.x Multiple Database Connections, Migrations, Relationships & Querying
Foreign Key Migrations Simplified In Laravel 7.x
Create A Composer Package? Test It Locally And Add To Packagist Repository
Install Packages Parallel For Faster Development In Composer