I have list of bookings in one table and I have another table spots that has the event info such as : date, day , event_name, price, duration etc..
I need to groupBy bookings using days, which works fine but the problem comes when there are spots without any bookings and my join query matches spots and bookings table but if there arent any bookings for a given spot then the groupby for that particular day fails.
Example : I have spots on Mon, Tue and Wed. If there are no bookings for monday, I wont get a totalbooking count a zero. I dont get anything for that day. inshort, it doesnt exist.
Why I need is because : I have a progress bar on front-end which shows no. of bookings for each day, so if no bookings for given day I endup with no progress bar.
Controller :
//This query does not return bookings for tuesday because I dont have bookings.
$days = DB::table('spots')
->join('bookings','spots.id','=','bookings.spot_id')
->where('spots.event_id','=',$id)
->select('day',DB::raw('count(bookings.spot_id) as bookingcount'))
->groupby('spots.day')
->get();
View :
#foreach($days as $day)
<div class="col-sm-6 col-md-3">
<div class="tablet tablet-stat">
<h4 class="tablet-header">{{ $day->day }}</h4>
<div id="circle">
<div class=" c100 p{{round(($day->bookingcount/$totalspace->totalspace)*100,0)}}">
<span> {{ round(($day->bookingcount/$totalspace->totalspace)*100,2) }} </span>
<div class="slice">
<div class="bar"></div>
<div class="fill"></div>
</div>
</div>
</div>
</div>
</div>
#endforeach
I am not really looking for a hack, like simply find the day that has no bookings and create another foreach loop to show those days with zero bookings. I prefer getting count as zero in my query.
Try it like this:
$days = DB::table('spots')
->leftJoin('bookings','spots.id','=','bookings.spot_id')
->where('spots.event_id','=',$id)
->select('day',DB::raw('count(bookings.spot_id) as bookingcount'))
->groupby('spots.day')
->get();
*corrected the previous answer, didn't make much sense
An outer join should do what you want.
Related
Limit to item list:
More Description from the Pics:
I want to limited max item list at Laravel, As you if select all filter in admin panel, its look like this. So how can limited first 5 item to popular filters ?
<div class="g-attributes">
<span class="attr-title" style="color: orange"><b><i class="icofont-medal"></i> {{$translate_attribute->name ?? ""}}:</b> </span>
#foreach($termsByAttribute as $term )
#php $translate_term = $term->translateOrOrigin(app()->getLocale()) #endphp
<span class="item {{$term->slug}} term-{{$term->id}}" style="color: green" >{{$translate_term->name}}</span>
#endforeach
</div>
You can use the Collection's take() method to grab the first 5 elements:
#foreach($termsByAttribute->take(5) as $term)
Your question isnt very clear, but I am assuming that you want to be able retrieve the last 5 rows of the table, in your controller, you can get the records like this
$termsByAttribute = Table::latest()->take(5)->get();
These are my two table's as:
company_industry_type:
law_master:
table company_industry_type has columns lm_id that is assigned to particular id.
i want to fetch the lm_id and law_name from table law_master
with respect to the lm_id assigned to id of table company_industry_type
please help me with this, i'm new to laravel.
<?php
$law = DB::table('tbl_company_industry_type')->pluck('lm_id');
$law_d = DB::table('tbl_law_master')->whereIn('id',$law)
->select('id','lm_id','law_name')->get();
$res_lms = '';
foreach($law_d as $law_details)
{
?>
<span id="sublaw_data">{{ $law_details->lm_id }}
({{ $law_details->law_name }}) <i class="fa fa-remove deleteclass"
onclick="delete_law('<?php echo $law_details->id?>')"></i></span>
<?php
$res_lms .=$law_details->id.",";
}
$res_lawids=trim($res_lms,',');
?>
my code returns only one id's data i.e 1 and not for 3,4 for last record of
company_industry_type
Using below query you can get result as per your requirements.
DB::table('company_industry_type')
->join('company_industry_type', 'company_industry_type.lm_id', '=', 'law_master.lm_id')
->select('law_master.lm_id', 'law_master.law_name', 'company_industry_type.*')
->get();
I have two tables.
contenttype
content
contenttype returns me list of content types and I show them on page with foreach. e.g. Ambulance service, Blood Bank , clinic etc. as shown in snapshot.
At the same time I am fetching total number of contents of each type from another table(contents).
I was successful to get total number of contents of each type and show on the blade with foreach.
But situation is I want to show the number of contents on every content type.
Like this
Ambulance sevice 8,
Blood Bank 7,
Clinic 4.
My controller method is:
public function index()
{
if (Gate::allows('edit-content', auth()->user())) {
// below line returns list of content type e.g Ambulance service
$type = DB::table('contenttype')->distinct('label')->orderBy('label', 'asc')->paginate(10);
//below line counts the number of each content type e.g. Ambulance service 10.
$count = Content::selectRaw('type, count(*)total')->groupBy('type')->get();
return view('admin.content_listing', compact('type', 'count'));
} else {
abort(403, "Unauthorized");
}
}
This is blade code:
#foreach ($count as $c)
<span class="label label-danger">{{ $c->total }} </span>
#endforeach
This red number list is output:
#foreach ($type as $t)
<div class="list-group-item">
<a href="{{ route('content.type.listing', $t->type ) }}" > {{ $t->label }}
<span class=" pull-right glyphicon glyphicon-search"></span></a>
<span class="col-md-1 glyphicon glyphicon-plus-sign"></span>
</div>
#endforeach
Output is:
If I place above loop in second loop then Of course it will become nested loop that I don't want.
I need to show Ambulance 8,
Beauty clinic 8,
Blood Bank 1,
etc.
If anybody knows the solution kindly share it!
I have tried different ways but no success.
Rather than creating two queries and attempting to combine their results in the view, have you tried performing a join in a single query? Making certain assumptions about your column names and leaving aside the pagination, the actual SQL would be something akin to:
SELECT contenttype.*, count(content.id) FROM contenttype LEFT JOIN content ON contenttype.id = content.type GROUP BY contenttype.label ORDER BY contenttype.label ASC;
The code necessary to implement this query with Laravel's query builder functionality is pretty well documented in the documentation.
I am making a time table in Laravel where user can select one field at say 10:00, and reserve equipment until say 12:00.
I have trouble displaying the range from when to when the equipment is reserved
#while($scheduler_start_time < $scheduler_end_time)
<tr>
<td>{{$scheduler_start_time->format('H:i:s')}}</td>
#foreach($equipment as $instrument)
<td>
<a href="#">
#if($instrument->reservations->where('reserved_from','=', $scheduler_start_time)
->where('reserved_to','<=', $scheduler_end_time)->first() != null)
HERE
#else
#endif
</a>
</td>
#endforeach
<?php $scheduler_start_time->addMinutes(30) ?>
</tr>
#endwhile
One instrument can have many reservations:
And this is what I get when getting reservation where reserved_from equals time. If I use >= I am fetching both records. I need a way to see that for example: Instrument3 is reserved from 6:30 up to 7:30, and then from 9:30 to 10:00
Unless I misunderstood your problem, I think you just need to add an upper limit on the 'reserved_from'. Would this work?
#if($instrument->reservations->where('reserved_from','>=', $scheduler_start_time)->where('reserved_from', '<', $scheduler_end_time)->where('reserved_to','<=', $scheduler_end_time)->first() != null)
UPDATE: Solved by OP
#if($instrument->reservations ->where('reserved_from','<=', $scheduler_start_time) ->where('reserved_to','>=', $scheduler_start_time)->first() != null
This is my table NewTheme_Comment:
id
parent_id
id_theme
user
text
upVotes
downVotes
And this is my controller:
class Comments extends Controller {
public function GenerateComments($id){
$Comments = NewTheme_Comment::where('id_theme', $id)->paginate(5);
return view('comments', ['Comments'=>$Comments]);
}
Id is the link to the general post(each posts have different section of comments), so dynamically at the click of user, user is redirected to the ('comments view') section of comments accordingly.
So I have the array Comments which is populated with the values from the table NewTheme_Comment, the problem for me consists of how can I use the values to create a Threaded Comments section.
Like so in my view(the problem is that this makes a comment section as like as every parent_id is equal to 0 which is not that I am looking for:
#foreach ($Comments as $Comment) {{-- Comments threads --}}
<div class="media">
<div class="media-left">
</div>
<div class="media-body">
<p class="media-heading">{{ $Comment->text }}</p>
<p class="media-heading">{{ $Comment->user }} / {{ $Comment->created_at }} </p>
</div>
</div>
#endforeach {{-- Comments threads --}}
The end result:
0
1
2
3
2
1
1
0
1
0
My idea is to make comments like(with parent_id):
0
1
2
3
2
1
1
0
1
0
But I can't find the right way to do this thing logically, so that in the end to look like a simple threaded comment section the same that reddit uses( for my little web application ).
If my approach is bad, I would greatly appreciate other better ways on how to solve this.
It is a multi-step solution. You should prefer using lazychaser/laravel-nestedset package as the threaded comment is a form of nested set.
The other way round, if you want to continue with your current approach, will be having child function in your Comment Model, as follows
public function children(){
return $this->hasMany('App\Models\Comment', 'parent_id'); //Change as per your application architecture
}
Then in views, you can check for the children of a given node by calling a partial view recursively, containing
//print the details of $Comment
#foreach($Comment->children as $childComment)
//call this partial view again for $childComment & print the details
#endforeach
List every comment that doesn't have a parent_id, then when you load your comments check if any of those comments have childs comments and list those with indentation.