Query to showing popularity rented car Laravel - php

Hi i want do somethink like that: Page where user can look popularity rented cars.
I almost do this but i get some problems and i dont know how figure it.
I made relationship many-to-many and its working great.
View:
<table class="table table-striped">
<thead>
<tr>
<th>#</th>
<th>Name Car</th>
<th>Popularity</th>
</tr>
</thead>
<tbody>
<?php $i = 1;?>
#foreach($cars as $car)
#foreach($car->rentcar as $rentcar)
<tr>
<td>{{ $i++ }}</td>
<td>{{$rentcar->name}}</td>
#foreach($car_pops as $car_pop)
<td>
#if($car_pop->total == '1')
<span class="glyphicon glyphicon-star"></span>
#elseif($car_pop->total == '2')
<span class="glyphicon glyphicon-star"></span>
<span class="glyphicon glyphicon-star"></span>
#endif
</td>
#endforeach
</tr>
#endforeach
#endforeach
</tbody>
</table>
Controller:
public function popularityCar()
{
$cars = User::with('rentcar')->get();
$car_pops = DB::table('rent_car')
->select('car_id', DB::raw('count(*) as total'))
->groupBy('car_id')
->get();
return view('user.popularityCar', ['cars' => $cars, 'car_pops' => $car_pops]);
}
in pivot table user can rent more then one time car with thats same name and i want display only one name car i mind i got audi and user rent it and second user rent it too but on other date so i want display only one time car audi and display popularity using bootstrap icon. and its almost working but showing more cars with thats same name so i trying groupBy like with checking popularity car but i got problem and i dont know how use it.
Model User:
public function rentcar()
{
return $this->belongsToMany(Cars::class, 'rent_car', 'user_id', 'car_id')->withTimestamps()->withPivot('start', 'end');
}
Model Cars:
public function caruser()
{
return $this->belongsToMany(User::class, 'rent_car', 'car_id', 'user_id');
}
So maybe is easy way to make query mysql with i can get car name and go to pivot table to get total car_id.

Related

Laravel 5.8: How to show a column information of a pivot table in Many To Many relationship

I have a Many To Many relationship between User Model & Wallet Model:
Wallet.php:
public function users() {
return $this->belongsToMany(User::class,'user_wallet','user_id','wallet_id');
}
And User.php:
public function wallets() {
return $this->belongsToMany(Wallet::class,'user_wallet','user_id','wallet_id');
}
And the pivot table of this relationship goes like this:
So I can properly show Wallet name at the Blade:
#forelse($user->wallets as $wallet)
<tr>
<td>
{{ $wallet->name }}
</td>
<td>
{{ $wallet->balance }}
</td>
</tr>
#empty
<td colspan="5" class="text-center">
No wallet exist
</td>
#endforelse
But the wallet balance data does not appear (because it's in the pivot table and not the wallets table).
So how to show this custom column in this case?
use withPivot and mention additional column name
public function wallets()
{
return $this->belongsToMany(Wallet::class,'user_wallet','user_id','wallet_id')->withPivot('balance');
}
then in view
<td>{{ $wallet->pivot->balance }}</td>

Laravel get All with Relationships and Paginate relationship data

I am trying to get all users and their associated (1:n) journals. However I want to add pagination to the associated journals, not the users
My Controller:
public function index()
{
$users = User::with(['journal'])->orderBy('name', 'asc')->get();
return view('journals/journals', ['users' => $users]);
}
My Blade:
#foreach($users as $user)
<a class="list-group-item list-group-item-action" data-toggle="collapse" href="#collapse{{$user->id}}" role="button" aria-expanded="false" aria-controls="collapse{{$user->id}}">
{{$user->name}}
</a>
<div class="collapse mt-1" id="collapse{{$user->id}}">
<div class="card card-body">
<div class="list-group">
<table class="table table-sm table-hover">
<thead>
<tr>
<th scope="col">Kunde</th>
<th scope="col">Betreff</th>
<th scope="col">Leistungsart</th>
<th scope="col">Beginn</th>
<th scope="col">Ende</th>
<th scope="col">Dauer</th>
<th scope="col">Arbeitszeit</th>
</tr>
</thead>
<tbody>
#foreach($user->journal as $journal)
<tr onclick="window.location='{{route('journal.show', [$journal->id])}}'">
<th scope="row">{{$journal->customer->name}}</th>
<td>{{$journal->title}}</td>
<td>{{$journal->type}}</td>
<td>{{$journal->started}}</td>
<td>{{$journal->ended}}</td>
<td>{{$journal->duration}}</td>
<td>{{$journal->worked}}</td>
</tr>
#endforeach
</tbody>
</table>
</div>
</div>
</div>
#endforeach
My Journal Model:
public function user(){
return $this->belongsTo('App\User', 'user_id', 'id');
}
My User Model:
public function journal(){
return $this->hasMany('App\Journal', 'user_id', 'id');
}
Again, what I am trying to achieve is that I can print out every user and paginate their journals, not paginate the users
I really hope someone can help me there
Render in blade
Above your journals for-each loop in your blade, try and put something like this:
#php
$paginated_journals = $user->journal()->paginate(10);
#endphp
And then, your for-each loop should look like this:
#foreach($paginated_journals as $journal)
...
#endforeach
After the for-each loop you can just put:
$paginated_journals->links()
to get the links for pagination
Pre-load data in controller
You can do the same thing server-side. Create a custom array that is empty, go through each user, and add sub array to that custom array:
array_push($custom_array, ['user' => $user, 'journals' => $user->journal()->paginate(10)])
This way you can send the custom array to your blade, loop through it, and render user data and paginated journals.

How to paginate and show all records using hasMany in laravel 5.6?

I have two tables one table for general information about the order named orders and a detail table for information like products, notes and delivery date.
Basically, I want to show in the same page the table orders and for each order can be multiple products, notes, dates from the table detail, and also I want to paginate in order to show exactly 20 orders on each page and every product, note, date row with the same id_order.
In conclusion, on each page there are 20 orders from table orders but there can be 40 rows from table detail with products, notes, delivery date, because one order can contain more than one product. in fact I want to use rowspan html in order to show on the same row the detail.
This is my Orders model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class order extends Model
{
public $timestamps = false;
protected $fillable = ['client','number_order','file'];
public function details()
{
return $this->hasMany('App\Detail','orders_id')->orderBy('id', 'desc');
}
}
Detail model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Detail extends Model
{
protected $table = 'detail';
public function order()
{
return $this->belongsTo('Detail','orders_id');
}
}
My controller:
public function index()
{
$orders = Order::first(); //This works only with one record I cant show all records, all() is not working
$orders->setRelation('details', $orders->details()->paginate(10));
return view('orders.index', compact('orders'));
}
View:
<tbody>
#foreach ($orders->details as $detail)
<tr id="{{ $detail->orders_id }}">
<td>{{ $detail->product }}</td>
</tr>
#endforeach
</tbody>
You can use eager load details on order like this
public function index()
{
$orders = Order::with('details')->paginate(20);
return view('orders.index', compact('orders'));
}
In view
<table>
<tbody>
#foreach($orders as $order)
<tr>
<td>{{ $order->created_at }}</td>
<td>... put any order info here</td>
<td>
<label>Order Details:</label>
<table>
#foreach ($order->details as $detail)
<tr id="{{ $detail->orders_id }}">
<td>{{ $detail->product }}</td>
<td>{{ $detail->note }}</td>
</tr>
#endforeach
</table>
<td>
</tr>
#endforeach
</tbody>
</table>

Display total amount for a charity in Laravel 5.2

I have a charity_donations table which holds a charity_id, amount donated for that charity, among other fields. This is how it looks.
What I need to do is I need to groupBy each charity ID, then count how much money was donated for that particular charity. And most importantly, I need to display that in a view.
Some thing like this:
Charity ID | Total
1 | $1,200
....
I have tried this,
$displayPerCharity = CharityDonation::select(DB::Raw('charity_id, COUNT(*) as count'))->groupBy('charity_id')->get();
dd($displayPerCharity);
That counts the charity IDs, then gives me the total for each charity. But I need total amount for each charity, then display in view.
Got it!
$displayPerCharity = DB::table('charity_donations')
->select(DB::raw('SUM(amount) as charity_amount, charity_id'))
->groupBy('charity_id')
->orderBy('charity_amount', 'desc')
->get();
Then in view:
#foreach ($displayPerCharity as $group)
<h1> Charity ID {{ $group->charity_id }} received {{ $group->charity_amount }}</h1>
#endforeach
Well how you display it is entirely up to how you want it to look.
If you're just stuck on how to get each charity ID and amount out, then you can just do this with a foreach loop.
Here's an example using a Bootstrap responsive table. This assumes that after you have run your query you have passed it on to the view as a variable called $displayPerCharity.
<div class="table-responsive">
<table class="table">
<thead>
<tr>
<th>Charity ID</th>
<th>Amount ($)</th>
</tr>
</thead>
<tbody>
#foreach($displayPerCharity as $display)
<tr>
<td>{{ $display->charity_id }}</td>
<td>{{ $display->charity_amount }}</td>
</tr>
#endforeach
</tbody>
</table>
</div>

PHP Like System: Change Image Attribute on Displayed Listing if Record is within User Favorites(pivot table)

I am attempting to develop an "add to favorites" system in which a user can "like" a book and it gets added to the pivot table(user_id and book_id go to table book_user). I currently have a many to many relationship set up and I loop through the list of all books(as shown in the #foreach loop in view) from my books table.
The question is how can I change the image shown(in the view which in this case is a div id ="heart-size") depending on whether the user has added that item to their favorites (book reference exists within the book_user table attributed to that user)? That is, if the book is in the user favorites display a red heart, if not leave it grey and continue listing the rest of the books.
Tables
books
id | book_name | book_author | book_url
users
id | user_id | password
book_user
id | user_id(foreign key from users) | book_id(foreign key from books)
Model Book.php
class Book extends Eloquent{
/**
* The database table used by the model.
*
* #var string
*/
protected $table = 'books';
public function users() {
return $this->belongsToMany('User');
}}
Controller BooksController.php
class BooksController extends BaseController {
protected $layout = "layouts.main";
public function Booklist() {
$books = Book::all();
$this->layout->content = View::make('books.booklist', array('books' => $books));
}}
View booklist.blade.php
<div class="booklist">
<table class="table table-striped table-condensed table-hover" width = "70">
<thead>
<tr>
<th></th>
<th>Book Name</th>
<th>Author Name</th>
<th>URL</th>
</tr>
</thead>
#foreach ($books as $book)
<tbody>
<tr>
<td>
<div id ="heart-size">
{{ HTML::link('book/like', '', array('id' => 'heart')) }}
</div>
</td>
<td> {{ $book->book_name }} </td>
<td>{{ $book->book_author}} </td>
<td>{{ $book->book_url}} </td>
</tr>
</tbody>
#endforeach
</table>
</div>
Add the following to your Book model:
public function hasLiked($userid) {
return in_array($userid, array_fetch($this->users->toArray(), 'id'));
}
And call it from your view (example):
#if $book->hasLiked($loggedinuserid)
<div id ="heart-size">
{{ HTML::link('book/like', '', array('id' => 'heart')) }}
</div>
#endif

Categories