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
Related
I am sorry but I don't even know how to title this question since I have no clue what to look for.
Here is my problem: I have the following list:
name
word
hit
Bill
performance
2
Anna
performance
5
John
performance
3
Bill
java
0
Anna
java
1
John
java
3
Bill
test
4
Anna
test
5
John
test
1
What I am trying to get is the following:
name
sum(hit)
words_count
Bill
6
performance = 2 java = 0, test = 4
Anna
11
performance = 5 java = 1, test = 5
John
7
performance = 3 java = 3, test = 1
I could sum all hits and group by words and so but I only need one dataset each name.
I think I need a subquery or so. How can I get the words_count column in that way?
Actually I need it a Laravel controller to display it in a datatable but an SQl statement would help me a lot also.
Appreciate your help.
Hi guys i thank you a lot for your answers.
I could manage it in SQL. So the statement is the following:
SELECT name, sum(hit) as points,
GROUP_CONCAT(concat(word, '->'), concat(hit, ' ') separator ' ' ) as words
FROM results
GROUP BY name;
This gives me exactly what i need.
I am display my data in a datatable (yajra). I will now "translate" this code to "laravel / php" code to feed my datatable.
Let's consider, you have a model named UserPerformance
$userPerformance = UserPerformance::all();
$result = $userPerformance->groupBy('name');
And in your blade template:
<table>
<thead>
<tr>
<th>name</th>
<th>sum(hit)</th>
<th>words_count</th>
</tr>
</thead>
<tbody>
#foreach($userPerformance as $name => $performance)
<tr>
<td>{{ $name }}</td>
<td>{{ $performance->sum('hit') }}</td>
<td>
{{ $performance->map(function($p) { return $p->word.' = '.$p->hit; })->implode(', ') }}
</td>
</tr>
#endforeach
</tbody>
</table>
You can also add your map() code to your model using an accessor to keep the blade template clean:
class UserPerformance extends Model
{
public function getWordCountAttribute()
{
return $performance->map(function($p) {
return $p->word.' = '.$p->hit;
})->implode(', ');
}
}
You can access it like this: $userPerformanceModel->word_count;
That also enables you, to re-use the code. You can also append this accessor during serialization to use it client-side.
I am trying to implement a conditional operator on a value returned from controller to create some custom view.
front.blade
#if({{count($users)}} <= 5) <!-- if total number of rows in users table is less than or equal to 5 -->
<h3> total number of rows less than or equal to 5 </h3>
#endif
controller
$users = User::all();
return view('front', [ 'users'=>$users]);
and the error is
syntax error, unexpected '<'(View: \resources\views\front.blade.php)
Tried all permutation combination of putting the condition within {{ }} or quoting the operator or the constant value 5 the error remains same. I am new to laravel and this might be a fundamental mistake in regards to laravel or php.
Just remove {{ and }}, they're not needed besides a Blade directive (#ifin this case)
You need to remove the {{ }} inside the if condition.
Like this.
#if(count($users) <= 5) <!-- if total number of rows in users table is less than or equal to 5 -->
<h3> total number of rows less than or equal to 5 </h3>
#endif
first you need to understand when you need to use curly braces.when you display your data in blade file you need to use curly braces. like
Hello, {{ $name }}.
You may construct if statements using the #if, #elseif, #else, and #endif directives. These directives function identically to their PHP counterparts:
#if (count($records) === 1)
I have one record!
#elseif (count($records) > 1)
I have multiple records!
#else
I don't have any records!
#endif
Your Solution
#if(count($users) <= 5) <!-- if total number of rows in users table is less than or equal to 5 -->
<h3> total number of rows less than or equal to 5 </h3>
#endif
for details go into laravel documentation https://laravel.com/docs/7.x/blade#if-statements
In Controller change your code like this -
$users = User::all();
return view('front', compact('users'));
Blade file code-
#if(count($users) <= 5) <!-- if total number of rows in users table is less than or equal to 5 --><h3> total number of rows less than or equal to 5 </h3>#endif
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 have a problem in my project. When I store value in my database it works perfectly fine. But when I want to show the value it gives me an undefined date that is not stored in my database.
Here In my database the created_at field value is 2017-02-18 16:23:37. But When I want to show this in my view page it gives me this
It gives me the value of 2017-02-02 09:19:40. I don't understand whats wrong.
I am only showing the created_at field. Please help me
My controller is
public function showApplication() {
$dataList = ApplicationModelOL::leftJoin('certificate', 'application.application_certificate_id', 'certificate.certificate_track_id')
->where('application_users_id', Auth::User()->users_track_id)
->get();
return view('frontend.pages.application', compact('dataList'));
}
My view is
#foreach($dataList as $data)
<tr>
<td>{{ $data->certificate_name_en }}</td>
#if($data->application_delivery_mode == 195126)
<td>Self</td>
#else
<td>Via Post</td>
#endif
<td class="hidden-phone">{{ $data->created_at }}</td>
<td class="hidden-phone">
<span class="label label label-warning">Pending</span>
</td>
<td class="hidden-phone">
<span class="label label-primary"><i class="fa fa-edit"></i></span>
<span class="label label-danger"><i class="fa fa-trash"></i></span>
</td>
</tr>
#endforeach
This problem happens when your MySQL timezone is different from php timezone which is set in your framework.
to sync your timezone you can use following codes:
PHP:
date_default_timezone_set('Asia/Tehran');
MySQL:
$timeZone = (new DateTime('now', new DateTimeZone('Asia/Tehran')))->format('P');
$pdo->exec("SET time_zone='$timeZone';");
With Mysql attributes, you can get different values whether id or created_at(Laravel) if you use join on tables. Say you join three tables the output will return the last column of the three "created_at". Therefore you could just check to make sure your joins are correct.
You can change your timezone in Laravel in your config/app.php.
The param below can be changed to whatever your preference is in line with the supported timezones...
'timezone' => 'Europe/Dublin',
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.