I have the following relationships
a movie has many episodes
an user has a watchlist
a watchlist has many movies
I get my episodes with eloquent
$latestshows = episode::with('movies') ->where('category', 'tvshow')->take(10) ->get();
I then display it with
#foreach($latestshows as $show)
#if (Auth::guest())
<tr>
#else
<tr class="{{ Auth::user()-> watchlist **...** ? 'alert-danger' : '' }}">
#endif
<td>{{ $show->movie->title }}</td>
<td>{{ $show->number }}</td>
<td>{{ $show->created_at }}</td>
</tr>
#endforeach
How do I check if the logged in used has the show in watchlist? I want to display it with a different color in that case.
Sorry if this is a silly question, I'm just a beginner and experimenting.
You can use in_array or array_diff (Both native PHP functions) to see which movies are the same, or you can use the ->diff() or ->has() methods from the Collection objects that you are dealing with.
<td>
<a ... {{ Auth::user()->watchlist->movies->find($show->movies->id) ? 'alert-danger' : '' }}>
{{ $show->movie->title }}
</a>
</td>
This code gave me the expected result
{{ Auth::user()->watchlist->movies->find($show->movies->id) ? 'alert-danger' : '' }}
Thank you Oscar, you were my inspiration :P
Related
I am working in Laravel 7, pulling data from my db and showing the student his/her score on the scores page in percentage via the following codes.
In my StudentOperationController.php
public function show_result($id) {
$data['result_info'] = Oex_result::where('id', $id)->get()->first();
$data['student_info'] = Oex_students::select(['oex_students.*', 'oex_exam_masters.title', 'oex_exam_masters.exam_date'])->join('oex_exam_masters', 'oex_students.exam', '=', 'oex_exam_masters.id')->where('oex_students.id', Session::get('id'))->get()->first();
return view('student.show_result', $data);
}
and in my show_result.blade.php
<h2>Result Information</h2>
<table class="table">
<tr>
<td>Number Correct</td>
<td>{{ $result_info->yes_ans }}</td>
</tr>
<tr>
<td>Number Incorrect</td>
<td>{{ $result_info->no_ans }}</td>
</tr>
<tr>
<td>Total</td>
<td>{{ round($result_info->yes_ans / ($result_info->yes_ans + $result_info->no_ans) * 100 , 1) }} %</td>
</tr>
</table>
The reason I show this is that it works as expected. this is within the students dashboard. In my admin, I am trying to do the same thing and show that students data for his/her exam result. In my database, I have a json type data for example: {"2":"YES","3":"YES","8":"YES"}. I am still learning Laravel and am stuck on how to get this data to my admin. I am clearly doing something wrong and am in need of assistance. Here is what I have tried:
AdminController.php
public function manage_students($id)
{
$data['result_info'] = Oex_result::where('id', $id)->get()->first();
// dd($data);
$data['student_info'] = Oex_students::select(['oex_students.*', 'oex_exam_masters.title', 'oex_exam_masters.exam_date'])->join('oex_exam_masters', 'oex_students.exam', '=', 'oex_exam_masters.id')->where('oex_students.id', Session::get('id'))->get()->first();
$data['exams'] = Oex_exam_master::where('status', '1')->get()->toArray();
$data['students'] = Oex_students::select(['oex_students.*', 'oex_exam_masters.title as exam_name'])
->join('oex_exam_masters', 'oex_students.exam', '=', 'oex_exam_masters.id')
->get()->toArray();
return view('admin.manage_students', $data);
}
and in my manage_students.blade.php, I have
<tbody>
#foreach($students as $key => $student)
<tr>
<td>{{ $key+1 }}</td>
<td>{{ $student['name'] }}</td>
<td>{{ $student['email'] }}</td>
<td>{{ $student['mobile_no'] }}</td>
<td>{{ $student['exam_name'] }}</td>
{{-- <td>N/A</td> --}}
<td>{{ round($result_info->yes_ans / ($result_info->yes_ans + $result_info->no_ans) * 100 , 1) }} % </td>
#if($student['status']== 1)
<td><input data-id="{{ $student['id'] }}" class="student_status" type="checkbox" name="status" checked></td>
#else
<td><input data-id="{{ $student['id'] }}" class="student_status" type="checkbox" name="status"></td>
#endif
<td>
Edit
Delete
</td>
</tr>
#endforeach
</tbody>
In doing this I get the error Too Few arguments to function ... 0 passed and exactly 1 expected
I am unable to replicate what I have for the student dashboard and because of my lack of Laravel knowledge, I am having trouble solving this issue. So, any help would be greatly appreciated. If I am missing anything, please let me know so I can edit my question. Thanks in advance.
Edit: manage_students route:
Route::get('admin/manage_students', 'AdminController#manage_students');
Edit Number 2:
Too few arguments to function App\Http\Controllers\AdminController::manage_students(), 0 passed and exactly 1 expected
C:\laragon\www\lionsfieldtest\app\Http\Controllers\AdminController.php:146
Referring to this line:
public function manage_students($id)
Edit Number 3:
in resources/views/layouts/app.blade.php
<li class="nav-item">
<a href="{{ url('admin/manage_students') }}" class="nav-link">
<i class="nav-icon fas fa-school"></i>
<p>
Student Management
</p>
</a>
</li>
Edit number 4:
The manage_student page brings in data from a number of student's exam results
This function request $id;
public function manage_students($id)
This route doesn't pass one
Route::get('admin/manage_students', 'AdminController#manage_students');
As I can see you are filtering by results which in this case you need $result id
Route::get('admin/manage_student/{id}', 'AdminController#manage_students');
So you have to pass result ID, but lets assume there is a default results id
public function manage_students($id=2)
then this route works because $id is optional/predefined.
Route::get('admin/manage_students', 'AdminController#manage_students');
And if you pass an ID it will use
Route::get('admin/manage_student/{id}', 'AdminController#manage_students');
Make 2 routes and make parameter optional/predefined.
if you go to /admin/manage_student it will use ID 2
but if you pass /admin/manage_student/5 it will use 5
So I'm using laravel, and I have a table with id, category_name, parent_id, desc, and url.
Then in the view, I foreach the table with this code:
#foreach($categories as $category)
<tr class="gradeX">
<td>{{ $category->id }}</td>
<td>{{ $category->name }}</td>
<td>{{ $category->parent_id }}</td>
<td>{{ $category->description }}</td>
<td>{{ $category->url }}</td>
<td class="center">
Edit
Delete
</td>
</tr>
#endforeach
now, instead of showing parent_id, I want to show the parent category name.
I've tried a lot of things but nothing work.
I know this might be a very easy question, but I'm just started learning web development by myself. So please bear with me.
Thank you.
Define the parent relationship in the Category model. https://laravel.com/docs/5.7/eloquent-relationships
Eager load that relationship in the query where you are fetching the categories. https://laravel.com/docs/5.7/eloquent-relationships#eager-loading
Access the loaded relationship in the view, eg. $category->parent->name
I'm new in Laravel. I'm storing 3 types of user in my users table:
admin : user_type_id = 1
agent : user_type_id = 2
farmer : user_type_id = 3
user_type_id column is in the users table.
In one of my Blade files, I want to only show the names of the agents. Here is my foreach loop, which is importing all 3 types of the user (it's showing the names of admins, agents and farmers).
#foreach($farmerPoint->user as $agent)
<tr>
<td>{{ $agent->name }}</td>
<td>{{ $agent->phone }}</td>
</tr>
#endforeach
This is probably more of a logic issue than a Laravel issue. NOTE that I would suggest you limit the $agents instead using your query (where) rather than this way, BUT:
#foreach($farmerPoint->user as $agent)
#if ($agent->user_type_id === 2)
<tr>
<td>{{ $agent->name }}</td>
<td>{{ $agent->phone }}</td>
</tr>
#endif
#endforeach
You can use a simple blade #if() statement like so:
#foreach($farmerPoint->user as $agent)
#if ($agent->user_type_id === 2)
<tr>
<td>{{ $agent->name }}</td>
<td>{{ $agent->phone }}</td>
</tr>
#endif
#endforeach
Or you can use a collection where() since all eager / lazy loaded relations are returned in collections:
http://laravel.com/docs/5.1/collections#method-where
#foreach($farmerPoint->user->where('user_type_id', 2) as $agent)
<tr>
<td>{{ $agent->name }}</td>
<td>{{ $agent->phone }}</td>
</tr>
#endforeach
My View
<tbody>
#foreach($categories as $category)
<tr>
<td>{{ $category->name }}</td>
<td>{{ $category->slug }}</td>
<td>{{ ($category->TermTaxonomy ? $category->TermTaxonomy->description : '') }}</td>
<td>
{{ Form::open(['method' => 'DELETE', 'route' => ['admin_posts_categories_destroy', $category->term_id]]) }}
{{ Form::submit('Delete'); }}
{{ Form::close() }}
</td>
</tr>
#endforeach
</tbody>
then result in inspect element
so i can't delete the first row , but i can delete the other , why this things happen ? and how to fix it. already try in the other browser and still same.
Never mind , in have 2 form in my view the first is static form to add term, the second is loop form for restful delete, and missing {{ Form::close() }} in the first form (static) , so just put {{ Form::close() }} in the first form (static) and both form static and looping work like a charm. thanks for all.
I need to fill table in TWIG with data from database. Everything is Fine with the exception of this:
I need to have column with DATEDIFF property to get number of days.
TODAY-dateFromDateBase
Question is:
How to get number of days in loop in twig?
here is my twig:
<table>
<thead>
<tr>
<form action="" method="post" {{ form_enctype(searchform) }} class="form-index-permits">
<td>L.p </td>
<td>ID PRZEPUSTKI {{ form_widget(searchform.PermitId) }}</td>
<td>Name{{ form_widget(searchform.Permitname) }}</td>
<td>Surname {{ form_widget(searchform.Permitsurname) }}</td>
<td>Company {{ form_widget(searchform.Company) }}</td>
<td>GW {{ form_widget(searchform.Contractor) }}</td>
<td>Dayleft {{ form_widget(searchform.Dayleft) }}</td>
<td>End date {{ form_widget(searchform.date, { 'attr': {'class': 'datepicker'} }) }}</td>
</form>
</tr>
</thead>
{% for permit in permitcollection %}
<tbody>
<td>{{ loop.index }}</td>
<td>{{ permit.getPermitid()|number_format(0, '.', ' ') }}</td>
<td>{{ permit.getPermitname() }}</td>
<td>{{ permit.getPermitsurname() }}</td>
<td>{{ permit.getPermitsCompany().getName() }}</td>
<td>{{ permit.getPermitsContractor().getName() }}</td>
<td> HERE I WANT TO DISPLAY DAYS LEFT</td>
<td>{{ permit.getExpirationdate()|date('Y-m-d') }}</td>
</tbody>
{% endfor %}
</table>
Is something like this possible?
{{ permit.getExpirationdate()|date('Y-m-d') - "now"|date('Y-m-d') }}
First Solution (recommended) "Use an existing library":
You can use the KnpTimeBundle
In the Twig:
This compare with the current date:
{# Returns something like "3 minutes ago" #}
{{ time_diff(permit.expirationDate) }}
This compare with the another date:
{# Returns something like "3 minutes ago" #}
{{ time_diff(permit.expirationDate, anotherDate) }}
Second Solution "Do it yourself":
Make diff via php function:
$calcFrom = permit.getExpirationdate()
$now = new \DateTime('now');
$now->diff($calcFrom)->format("%a")
And make it available via a Twig extension or directly in an helper method in the entity.
Another possible solution is to write register a custom DQL Function to do the work in the repository
Hope this help