Multiple Eager Loading With Pagination in laravel - laravel

I want to return paginated data of the eager loaded relations so as I can display them using pagination in my view template
public function show(Group $group)
{
$data = ['members', 'assets', 'liabilities', 'loans','shares'];
$group = $group->load($data);
return view('admin.groups.show', compact('group'));
}
This is my Group Model
<?php
namespace App;
class Group extends BaseModel
{
use Enumable;
public function constitution()
{
return $this->hasOne(Constitution::class);
}
public function members()
{
return $this->hasMany(Member::class);
}
public function assets()
{
return $this->hasManyThrough(Asset::class, Meeting::class)
->orderByDesc('meeting_id')
->orderBy('member_id');
}
public function loans()
{
return $this->hasManyThrough(Loan::class, Member::class)
->orderByDesc('meeting_id')
->orderBy('member_id');
}
public function shares()
{
return $this->hasManyThrough(Share::class, Member::class)
->orderByDesc('meeting_id')
->orderBy('member_id');
}
public function liabilities()
{
return $this->hasOne(Liability::class)->latest();
}
}
I need to paginate $data array

Use setRelations():
public function show(Group $group)
{
$group->setRelations([
'members', $group->members()->paginate(5),
'assets', $group->assets()->paginate(5),
'liability', $group->liability()->paginate(5),
'loans', $group->loans()->paginate(5)
]);
return view('admin.groups.show', compact('group'));
}

Related

Laravel Excel export

While exporting the data, it seemed to add the conditions for exporting. So, I made the constructor method and passed the data from the controller.
public function __construct($company_pan, $user_pan)
{
$this->company_pan = $company_pan;
$this->user_pan = $user_pan;
}
public function collection()
{
return PurchaseDetail::where([
['pan_code', $this->user_pan],
['s_pan', '=', $this->company_pan]
])->get();
}
In the Controller,
public function export(Request $request)
{
$company_pan = $request->pan;
$user_pan = Auth::user()->pan_code;
return Excel::download(new ConfirmationExport($company_pan, $user_pan), 'Confirmation.xlsx');
}
All the output is blank excel. But if I changed the code in the collection of s_pan as
public function collection()
{
return PurchaseDetail::where([
['pan_code', $this->user_pan],
['s_pan', '=', '303030333']
])->get();
}
It exports the data as expected. What actually the problem is?
Have you defined the attributes of your class? Like this?
<?php
namespace App\Exports;
use Maatwebsite\Excel\Concerns\FromCollection;
class ExampleExport implements FromCollection
{
protected $company_pan;
protected $user_pan;
public function __construct($company_pan, $user_pan)
{
$this->company_pan = $company_pan;
$this->user_pan = $user_pan;
}
public function collection()
{
return PurchaseDetail::where([
['pan_code', $this->user_pan],
['s_pan', '=', $this->company_pan]
])->get();
}
}

How to load a Category Page with Products using pagination and filtering? Laravel

// CategoryController
class CategoryController extends Controller
{
public function show($slug)
{
$category = Category::with('products')
->where('slug', $slug)
->where('menu', 1)
->first();
return view('pages.category.show', compact('category'));
}
}
// Product_to_categories pivot table...
Schema::create('products_to_categories', function (Blueprint $table) {
$table->bigIncrements('id');
$table->bigInteger('category_id')->unsigned()->index();
$table->foreign('category_id')->references('id')->on('categories');
$table->bigInteger('product_id')->unsigned()->index();
$table->foreign('product_id')->references('id')->on('products');
$table->timestamps();
});
I cannot paginate the products on the page, how would you suggest paginating with Eager Loading or passing the variables separately...
I have a pivot table Category_Product also...
I also want to filter based on brand and price?
EDIT:
// product.pphp
class Product extends Model
{
public function categories()
{
return $this->belongsToMany(Category::class, 'products_to_categories', 'product_id', 'category_id');
}
}
```
Reverse it, meaning query for products instead:
class CategoryController extends Controller
{
public function show($slug)
{
$products = Product::with(['categories'])->whereHas('categories', function ($query) use ($slug) {
$query->where(['slug' => $slug]);
})->paginate();
return view('pages.category.show', compact('products'));
}
}
You can do that in two steps like this:
class CategoryController extends Controller
{
public function show($slug)
{
$category = Category::where('slug', $slug)->first();
$products = Product::where('category_id', $category->id)->paginate(20);
return view('pages.category.show', compact('category', 'products'));
}
}
This with Eager Loading
class CategoryController extends Controller
{
public function show($slug)
{
$category = Category::with('products')->whereHas('products' => function($query) {
if(request()->has('price')
{
$query->where('price',request()->input('price');
}
if(request()->has('brand')
{
$query->where('brand',request()->input('brand');
}
})->where('slug', $slug)->paginate(20);
return view('pages.category.show', compact('category'));
}
}
Thanks #Tpojka that answer provides the same results as this below...
class CategoryController extends Controller
{
public function show($slug)
{
$category = Category::where('slug', $slug)->first();;
$products = $category->products()->paginate(15);
return view('pages.category.show', compact('category', 'products'));
}
}

How to get model relations in Laravel?

I would like to get the model relations, in array;
My model look like:
class User extends Model
{
public function profile() {
return $this->haOne(Profile::class);
}
public function settings() {
return $this->morphOne(Settings::class, 'settingsable');
}
public function addresses() {
return $this->hasMany(Addresses::class);
}
}
And my code:
$user = User::with(['profile', 'settings', 'addresses'])->find(1);
$user->getRelations(); // return ['profile', 'settings', 'addresses'];
If I have more then 10 relation, I don't want to list all.
I would like to get like this:
$relations = ['profile', 'settings', 'addresses'];
Is this posible?
You could try adding a scope to the model, and so, you have to only write them once.
class User extends Model
{
public function profile() {
return $this->haOne(Profile::class);
}
public function settings() {
return $this->morphOne(Settings::class, 'settingsable');
}
public function addresses() {
return $this->hasMany(Addresses::class);
}
public function scopeWithRelations(Builder $query){
return $query->with([...]);
}
}
$users = User::withRelations()->get();
This way you only have to write them once there, and everywhere in the code you'll use the scope.
Not exactly 100% what you're asking, but this could be a solution.

Laravel 5.5. Many-to-Many. Receive the latest instance

I have such models (Many-to-Many)
class Driver
{
public function cars()
{
return $this->belongsToMany(Car::class);
}
}
class Car
{
public function drivers()
{
return $this->belongsToMany(Driver::class);
}
}
My Car has activated_at column, that can be null.
How can I retrieve the latest activated car per drivers in a specific parking?
Something like this
Parking::find(2)->drivers()->lastActivatedCars...?
You can just create a scope for the car.
// Car.php
public function scopeActivated($query)
{
return $query->whereNotNull('activated_at')->orderBy('activated_at', 'DESC');
}
And then in your Driver.php:
// Driver.php
public function latestActivatedCar()
{
return $this->cars()->activated()->first();
}
Then you can just:
$latest = $driver->latestActivatedCar();
Or you can create a relationship, so you can eager load.
// Driver.php
public function latestActivatedCar()
{
return $this->belongsToMany(Car::class)
->whereNotNull('activated_at')
->orderBy('activated_at', 'DESC')
->limit(1);
}
Then you can just:
$drivers = Parking::find(2)->drivers;
$drivers->load('latestActivatedCar');
I hope this would help:
Parking::with(['drivers' => function ($driverQuery) {
$driverQuery->with(['cars' => function ($carsQuery) {
$carsQuery->orderBy('activated', 'desc')
->limit(1);
}]);
}])->find(2);

how to save data in multiple tables with the one form

I have 2 tables: customers and address one form to save data into this tables. For this, I made the relation like:
Address model
public function customer()
{
return $this->belongsTo(Customer::class);
}
customer model
public function address()
{
return $this->hasMany(Address::class);
}
How can i save the address in table? I already done with customer i can save, update and delete. I read the DOC of Eloquent of laravel, but i don't really get it.
Here is my controller
class CustomerController extends Controller
{
public function index()
{
// $customer = Customer::all();
$customer = Customer::with('address')->get();;
return view('customer.index', compact('customer'));
}
public function create(Address $address)
{
$address = $address->all();
return view('customer.create');
}
public function store(CustomerRequest $request , Customer $customer)
{
$customer = Customer::create($request->all());
return redirect()->route('customer.index',compact('customer'));
}
public function show(Customer $customer)
{
return view('customer.show',compact('customer'));
}
public function edit(Customer $customer)
{
return view('customer.edit', compact('customer'));
}
public function update(CustomerRequest $request, $id)
{
$customer = Customer::find($id)->update($request->all());
return redirect()->route('customer.index',compact('customer'));
}
public function destroy($id)
{
Customer::destroy($id);
return redirect()->route('customer.index');
}
}
Any suggestions would be appreciated!!

Resources