Laravel,blade, vue.js and #if (#{{ }}) - php

I am having a problem with variables devielve vue.js me ... I explain better
<tr
#if(#{{users.id}} != 1) // this is the error
Class="danger"
#else
class="success"
#endif
>
I can not define a #if (# {{user.id in laravel
before defined
#foreach($datos as $dato)
<tbody id={{$dato->id}}>
<tr
#if($dato->id !=1)
class="danger"
#else
Class="success"
#endif >
and it worked but I had to put a select box, and Use varaible vue.js to collect and return the data I...
excuse my English is not my mother tongue and hinders me

I do not believe you can use vuejs code (such as your #{{users.id}}) inside a laravel #if() because everything inside the #if() is being treated as PHP code. If it makes sense in your particular application, make use of vuejs's v-if implementation, such as <div v-if="users.id !== 1"> INSERT WHATEVER HERE </div>

Related

Alternating rows background depending a changing value in Laravel

Here is a blade template of my table, where I have issues changing rows depending on a value. If the command number changes I want the row and all the following rows with that command number to change and then alternate between white/grey colors everytime it's a different one.
I know it looks pretty basic but I can't make the condition in my algorithm. My first if looks if the previous iteration has a different command number and change the class, but I'm missing the one checking if the previous iteration has the same command number so keep the same background. I don't know how to check something css class and alternate the 2 backgrounds white/grey. I tried with $attributes from Laravel but it is always undefined.
I hope I was clear with my issue, also the table is already sorted by increasing command_number.
If you need the controller I can provide it.
Thanks
tr.second th, tr.second td {
background-color: #D1D1D1;
}
//////
<tbody>
#foreach($data as $key => $line)
#if($key > 0)
<tr #if ($data[$key]['command_number'] != $data[$key-1]['command_number']) class="second"
#elseif()
#endif>
#foreach($line as $item)
#if(($item == $line['quantity']) || ($item == $line['delivery']))
<td>
<strong>{{$item}}</strong>
</td>
#elseif($item == $line['hours'])
<td>
<strong style="color: red;">{{$item}}</strong>
</td>
#else
<td>
{{$item}}
</td>
#endif
#endforeach
</tr>
#endif
#endforeach
</tbody>
If you want to change the class based on a previous result, why don't you track the css class to use with a toggle variable outside of the loop like so:
// in your blade file
#php
$toggle = false;
#endphp
#foreach(range(0, 10) as $number)
<div class="#if($toggle) someClass #else otherClass #endif unrelatedClasses">
</div>
#php
$toggle = !$toggle
#endphp
#endforeach
Alternatively, the toggle should function in your case:
// You could change the value of toggle based on your use case, like:
#php
$toggle = ($data[$key]['command_number'] != $data[$key-1]['command_number']);
#endphp
Instead of using $key, you can also use $loop->index to get the current iteration.
Loop variable for even/odd cases
For other use cases, the loop variable has several properties and methods you can use (like even, odd). Check out the Blade documentation.
CSS selector for even/odd cases
There are also CSS solutions using selectors like: nth-child(even) and nth-child(odd) for even and odd numbers respectively.
<tbody>
#php($currentKey = null)
#foreach($data as $key => $line)
#if($key > 0)
<tr #if ($data[$key]['command_number'] != $currentKey) class="second"
#elseif()
#endif>
#foreach($line as $item)
#if(($item == $line['quantity']) || ($item == $line['delivery']))
<td>
<strong>{{$item}}</strong>
</td>
#elseif($item == $line['hours'])
<td>
<strong style="color: red;">{{$item}}</strong>
</td>
#else
<td>
{{$item}}
</td>
#endif
#endforeach
</tr>
#endif
#php($currentKey = $data[$key]['command_number'])
#endforeach
</tbody>

Laravel 8: How to list data on blade with foreach() loop

I want to show some data on blade by a foreach loop like this:
#foreach($topAnswered as $topAns)
<tr>
<td style="text-align:right;">
<h3 class="h5 mb-0">{{ $topAns->title }}</h3>
</td>
<td style="text-align:right;">
<div>{{ $topAns->answers->count() }}</div>
</td>
<td style="text-align:right;">
<div>{{ $topAns->likes->count() }}</div>
</td>
</tr>
#endforeach
But I need to retrieve these data descending based on $topAns->likes->count(). So how can I add this condition to the foreach() loop.
I would really appreciate if you share any idea or suggestion from you guys...
Thanks in advance.
You can use the method sortBy for collections, learn more here
#foreach($topAnswered->sortBy(function($answer, $key){return $answer->likes->count();}) as $topAns)
...
#endforeach
Some advices:
I think you should do this sorting on the controller, so in the blade file it is already sorted
On your controller you should eager load the likes so it doesn't make a new query for every likes relationship, ->with('likes')
You also can load ->withCount('likes') and sort by likes_count
learn more about relationships and eager loading here

Laravel "No query results for model [App\Schedule]" error when not calling model

I have a perplexing issue that I cannot seem to figure out. I am trying to load a webpage from a button, but it keeps poping up the error No query results for model [App\Schedule]. I have no idea why it's doing that, as there is nowhere I am trying to access that model when trying to load the webpage. Below is what I have so far (it's a work in progress)
Here is the link:
<a class="btn btn-primary" role="button" href="/schedule/availability">Set Availability</a>
Here is the Route:
Route::get('/schedule/availability', 'ScheduleController#getAvailabilityCalendar');
Here is the method inside of the ScheduleController:
public function getAvailabilityCalendar(){
$dt = Carbon::now('America/Los_Angeles');
$return['DayOfWeek'] = $dt->dayOfWeek;
$return['DisplayDays'] = (($dt->daysInMonth + $return['DayOfWeek'] + 1) <= 35) ? 34:41;
return view('contractors.availability', compact('return'));
}
And here is the view that is returned:
<div class="table-responsive">
<table class="table">
<thead>
<tr>
<th>Sunday </th>
<th>Monday </th>
<th>Tuesday </th>
<th>Wednesday </th>
<th>Thursday </th>
<th>Friday </th>
<th>Saturday </th>
</tr>
</thead>
<tbody>
<tr>
#for ($i = 0, $j=-$return['DayOfWeek']; $i <= $return['DisplayDays']; $i++, $j++)
#if($i < $return['DayOfWeek'])
<td></td>
#else
#if($i %7 == 0)
</tr><tr>
#endif
#if($j < 30)
<td>{{\Carbon\Carbon::now('America/Los_Angeles')->addDays($i-$return['DayOfWeek']+1)->format('F jS')}}
<hr>
<div class="form-group">
<label data-toggle="popover" data-trigger="hover" data-content="Add Times available in the following format: 10:00am-4:00pm. You may also add multiple blocks of time separated by comma. Example: 10:00am-12:00pm, 2:00pm-4:00pm">Available Time</label>
<input type="text" class="form-control availability" />
</div>
<div class="checkbox">
<label>
<input type="checkbox" class="all-day-check" />All Day</label>
</div>
</td>
#else
<td></td>
#endif
#endif
#endfor
</tr>
</tbody>
</table>
</div>
<script>
$(document).on('change', '.all-day-check', function(){
if (this.checked) {
$(this).closest("input.availability").prop("disabled", true);
}else{
$(this).closest("input.availability").prop("disabled", false);
}
});
</script>
I can't find anywhere that would need a reference to the App\Schedule model, which is a model in my project. I am at a total loss as to what it could be or why it's trying to run something I never asked it to run.
Edit: There is also no other instance of /schedule/availability in the routes file as well, I triple checked.
Update: Here is the Constructor for ScheduleController:
public function __construct()
{
$this->middleware('auth'); //Check is user is logged in
}
Here is the stacktrace I received (For some reason, it didn't show up in my error log so I only have the screenshot)
As it turns out in the discussion in comments above. The culprit is narrowed down to a conflicting route.
Route:something('/schedule', ...)
was found before
Route::get('/schedule/availability', 'ScheduleController#getAvailabilityCalendar');
and the Route:something fires before the desired route. It should be noted that route defined first will be the first one to serve. This is the reason why Route:resource should be put after your custom Route:get or another routes to avoid confusion.
Next item in the list when changing routes but it does not updated, alwasy fire a php artisan route:clear. It will clear the route caches - can check in the docs.

How can i use if condition with laravel blade and angular

i pass value from laravel controller to angular and then show data from my database to blade view laravel. i want to try if condition with angular data from angular controller how can i do it??
i try this code:
<td>
#if(#{{pending.step1}}=='Pending')
// here some code///
#endif
</td>
how can i use if condition with laravel blade and angular??
I think you can try this if you need use for angular if condition :
<td ng-if="pending.step1 =='Pending'">
----------- code here -------------------
</td>
OR you can use laravel if condition then you should use this :
<td>
#if(pending.step1 =='Pending')
// here some code///
#endif
</td>
Hope this work for you!
We can use an Angular variable in blade file like this:
Normal use
Angularjs variable = data;
#{{ data }}
Use in condition
Angularjs variable = data;
#if( #data == 0 )
#else
#endif

Best way to fill a roster schedule

Fellow programmers,
I got a roster application build in PHP with the framework : Laravel. And i have the following issue:
My application has : Users, Taskdate's, and TaskdateTime. Each user can get a TaskdateTime assigned to them. That TaskdateTime is a child of Taskdate. I want to populate a schedule where each user can see all their times. The schedule looks like this :
What i do in my application is : Fetch all users, Fetch all Taskdate's and according to the Taskdate's, fetch the TaskdateTime's for the right Taskdate. I do this in the following way :
$taskdates = Taskdate::with('users')->where_date($date)->get();
$employees = Usergroup::with(array('users', 'users.workdates'))->where('usergroup_title', '=', 'Employees')->first();
You can see that i fetch usergroups, with the relationship : Users and with the User->workdates relationship. Where the workdates returns the TaskdateTime objects as mentioned above.
Now in my view, i do this to populate it with data. And this seems like a lot of extra hassle and i think it could me much easier but i can't wrap my head around it. Anyway, this is the code :
<table id="plan" class="table table-bordered">
<thead>
<th></th>
#foreach($taskdates as $taskdate)
<th id="{{ $taskdate->id }}">{{ $taskdate->subject }}</th>
#endforeach
</thead>
<tbody>
#foreach($employees as $employee)
<tr id="{{ $employee->id }}">
<td>{{ $employee->name }}</td>
#foreach($taskdates as $taskdate)
<?php
$result = array_filter($employee->workdates, function($user) use ($taskdate){
return $user->id === $taskdate->id;
});
?>
#if( empty($result) )
<td></td>
#else
<td id="{{$result[key($result)]->pivot->id }}" style="{{ ($result[key($result)]->pivot->taskdate_isread) ? '' : 'font-weight:bold;'}}">
{{date('H:i', strtotime($result[key($result)]->pivot->taskdate_time))}}
</td>
#endif
#endforeach
</tr>
#endforeach
</tbody>
</table>
So i am filtering the whole workdates relationship of each user, if its hold the Taskdate id. And if it is empty, then don't echo anything.
The syntax of the HTML/PHP is in BLADE, so it might not seem correct PHP parsing, but it is.
Edit :
Question :
My code feels bloated. I don't think this is the most efficient way to achieve the result. What is your opinion on this? I like to be as efficient as possible.

Categories