How to display data from database in laravel - php

Am trying to display all the data on the admin end but i have failed. How
can i display it?
This is the method that i want to display all the data(Workflows):
public function getUserWorkflows(){
$user = $this->userRepo->users();
$active_module = AppConstants::$ACTIVE_MOD_USERS;
$appraisals = Appraisal::with(
['academicBackgrounds','keyDuties','additionalAssignments',
'competences','workflow', 'assignments','keyDuties'] )->
where('user_id','=',$user->id)->orderBy('created_at','desc')->get();
$summaries = DataGenerator::getAppraisalSummaries($appraisals);
$user = User::with(['department'])->find($user->id);
$appraisalsAssigned = DataGenerator::getAssingedAppraisals($user->id);
$assignedSummaries = DataGenerator::getAppraisalSummaries($appraisalsAssigned);
return view('user.tasks',compact('appraisals','users','active_module','summaries','appraisalsAssigned','assignedSummaries'));
}
Below is the code snippet from the view where i want the data to displayed. But it has totally failed to display. Because i think there is something that am not getting right:
<div class="col s12">
<div class="col s12">
<ul id="issues-collection" class="collection">
<li class="collection-item avatar">
<i class="material-icons circle blue darken-4">folder</i>
<span class="collection-header">Workflow</span>
</li>
#if(isset($appraisals) && count($appraisals) > 0)
#foreach($appraisals as $appraisal)
#if($loop->iteration <= 3)
<li class="collection-item">
<div class="row row-custom-modal-footer collections-title">
<div class="col s12"><strong>{{$loop->iteration}}. </strong>
{{str_replace('_',' ',$appraisal->document_name)}}</div>
</div>
<div class="row row-custom-modal-footer valign-wrapper">
<div class="col s3"><p>Date: <span class="text-primary">{{$appraisal->created_at}}</span></p></div>
<div class="col s5">
<span class="task-cat blue-stepper">
#if(isset($summaries))
#foreach($summaries as $summary)
#if($summary['id'] == $appraisal->id)
{{$summary['stage']}}
#endif
#endforeach
#endif
</span>
</div>
<div class="col s2">
<div class="progress">
<div class="determinate blue darken-4"
#if(isset($summaries))
#foreach($summaries as $summary)
#if($summary['id'] == $appraisal->id)
style="width: {{$summary['percentage']}};"
#endif
#endforeach
#endif></div>
</div>
</div>
<div class="col s1">
<p class=" camel-case"><a class="text-primary bold-text" href="{{route('view_appraisal',[$appraisal->id])}}">View</a></p>
</div>
<div class="col s1">
<p class=" camel-case"><a class="text-primary bold-text" href="{{route('view_appraisal',[$appraisal->id])}}">Stop Workflow</a></p>
</div>
</div>
</li>
#endif
#endforeach
<li class="collection-item">
<div class="row">
<div class="col s12 spacer-top">
View all your tasks
</div>
</div>
</li>
#else
<li class="collection-item">
<div class="col s12 bold-text">
<p>There are no pending tasks</p>
</div>
</li>
#endif
</ul>
</div>
</div>
I want when the person logs in as an admin, they are able to see all the data from the database once they access the view. But it only displays the data when the person logs in as a user.

I have found the solution hence i just needed to make these edits in my controller method:
public function getUserWorkflows(){
$user = $this->userRepo->users();
$active_module = AppConstants::$ACTIVE_MOD_USERS;
// Edited this line of code
$appraisals = Appraisal::with(
['academicBackgrounds','keyDuties','additionalAssignments',
'competences','workflow', 'assignments','keyDuties'] )->
orderBy('created_at','desc')->get();
$summaries = DataGenerator::getAppraisalSummaries($appraisals);
$user = User::with(['department'])->find($user->id);
$appraisalsAssigned = DataGenerator::getAssingedAppraisals($user->id);
$assignedSummaries = DataGenerator::getAppraisalSummaries($appraisalsAssigned);
return view('user.tasks',compact('appraisals','users','active_module','summaries','appraisalsAssigned','assignedSummaries'));
}
Thanks so much, i have found the solution after carefully going through the controller method's lines of code

Related

Add class only on first carousel-cell (flickity)

i would have the class "opacity" inserted only at the first "carousel-cell".
For the rest in the loop, they should not be there.
<div class="main-carousel">
#foreach($projects as $p)
#if($p->getMedia('teaser')->first() ?? "")
<div class="carousel-cell">
<div class="row">
<div id="project-info" class="col-lg-3 text-md-right pr-md-5 project-info">
<div class="project opacity">
<h2 class="project-title mb-0">{{$p->name}}</h2>
<h3 class="project-category">Foto / Video Produktion</h3>
</div>
</div>
<div class="col-lg-9 project-img opacity">
<a href="{{url('projects')}}/{{$p->slug}}">
<div class="start-teaser">
<img src="{{asset('storage')}}/{{$p->getMedia('teaser')->first()->id}}/{{$p->getMedia('teaser')->first()->file_name}}" alt="Land Rover">
</div>
</a>
</div>
</div>
</div>
#endif
#endforeach
</div>
You could use the $loop->first variable to add a class only on the first iteration of the loop.
Like this: <div class="carousel-cell {{ $loop->first ? 'opacity' : '' }}">
See more here:
https://laravel.com/docs/9.x/blade#the-loop-variable

Laravel 6.2- Using #php ...-. #endphp does not work but <?php ---- ?> does?

I was trying to use #php....#endphp to code some data in a Laravel blade file as follows
<div class="row">
#php
$count =count($doc_list);
if(!empty($chk_doc_list)){
$arrColumns = array_column($chk_doc_list,'docType');
}
#endphp
#for($i=0;$i<$count;$i+=2)
#php
$c1=$i;$c2=$c1+1;
$n1=$c1+1;
$n2=$c2+1;
#endphp
<div class="col-md-6">
<div class="row listview">
<div class="col-md-11">
{{$n1}}. {{$doc_list[$c1]->TDM_Doc_Name}}
</div>
<div class="col-md-1 listicon">
#if(!empty($arrColumns))
#if(in_array($doc_list[$c1]->TDM_Doc,$arrColumns))
<i class="fa fa-check text-success"></i>
#endif
#endif
<!-- <label><input type="checkbox" class="minimal"></label> -->
</div>
</div>
</div>
#if($c2<$count)
<div class="col-md-6">
<div class="row listview">
<div class="col-md-11">
{{$n2}}. {{$doc_list[$c2]->TDM_Doc_Name}}
</div>
<div class="col-md-1 listicon">
#if(!empty($arrColumns))
#if(in_array($doc_list[$c2]->TDM_Doc,$arrColumns))
<i class="fa fa-check text-success"></i>
#endif
#endif
<!-- <label><input type="checkbox" class="minimal"></label> -->
</div>
</div>
</div>
#endif
#endfor
</div>
However, the code didnt work. The browser (I tried with 4 of them) was unable to identify $count in #for($i=0;$i<$count;$i+=2) line.
But, when I changed #php to <?php and #endphp to ?> ,it worked.
So, what was the problem with the first one?

Trying to set default Cateogry View in frontend Laravel 6

I am a total amateur on Laravel and I am trying to find a way to display by default a specific category with it's services instead of showing ALL the services for alla the categories that exist.
<section class="categories sp-80-50 bg-dull">
<div class="container">
<div class="row">
<div class="col-12">
<div class="all-title">
<p>#lang('front.categoriesTitle')</p>
<h3 class="sec-title">
#lang('front.categories')
</h3>
</div>
</div>
</div>
<div id="categories" class="row justify-content-center">
#foreach ($categories as $category)
#if($category->services->count() > 0)
<div class="col-xl-2 col-lg-3 col-md-4 col-sm-6 col-12 mb-30 categories-list" data-category-id="{{ $category->id }}">
<div class="ctg-item" style="background-image:url('{{ $category->category_image_url }}')">
<a href="javascript:;">
<div class="icon-box">
<i class="flaticon-fork"></i>
</div>
<div class="content-box">
<h5 class="mb-0">
{{ ucwords($category->name) }}
</h5>
</div>
</a>
</div>
</div>
#endif
#endforeach
</div>
</div>
</section> */
<section class="listings sp-80 bg-w">
<div class="container">
<div class="row">
<div class="col-12">
<div class="all-title">
<p> #lang('front.servicesTitle') </p>
<h3 class="sec-title">
#lang('front.services')
</h3>
</div>
</div>
</div>
<div id="services" class="row">
#foreach ($services as $service)
<div class="col-lg-3 col-md-6 col-12 mb-30 services-list service-category-{{ $service->category_id }}">
<div class="listing-item">
<div class="img-holder" style="background-image: url('{{ $service->service_image_url }}')">
<div class="category-name">
<i class="flaticon-fork mr-1"></i>{{ ucwords($service->category->name) }}
</div>
</div>``
and
var categories = `
<div class="col-xl-2 col-lg-3 col-md-4 col-sm-6 col-12 mb-30 categories-list">
</div>`;
response.categories.forEach(category => {
if (category.services.length > 0) {
var url = category.category_image_url;
categories += `
<div class="col-xl-2 col-lg-3 col-md-4 col-sm-4 col-12 mb-30 categories-list" data-category-id="${category.id}">
<div class="ctg-item" style="background-image:url('${url}')">
<a href="javascript:;">
<div class="icon-box">
<i class="flaticon-fork"></i>
</div>
<div class="content-box">
<h5 class="mb-0">
${category.name}
</h5>
</div>
</a>
</div>
</div>`
}
});
$('#categories').html(categories);
var services = '';
if (response.services.length > 0) {
response.services.forEach(service => {
services += `
<div class="col-lg-3 col-md-6 col-12 mb-30 services-list service-category-${service.category_id}">
<div class="listing-item">
<div class="img-holder" style="background-image: url('${ service.service_image_url }')">
<div class="category-name">
<i class="flaticon-fork mr-1"></i>${service.category.name}
</div>
<div class="time-remaining">
<i class="fa fa-clock-o mr-2"></i>
<span>έως ${service.time} ${makeSingular(service.time, service.time_type)}</span>
</div>
</div>
You can access the webpage at https://click-away.store
I need to set the default Services view for the 1st Button Category.
Sorry if my question is silly or the solution is simple but I couldn't find out what to do!
Your error is that you never loop over the category's services.
You check if there are any services, but you never loop over them. So, within the #if you should do that.
#foreach ($categories as $category)
#if($category->services->count() > 0)
#foreach($category->services as $service)
// your code
#endforeach
#endif
#endforeach

How to make several rows with foreach?

I am using Laravel 5.8 and in my controller I am showing 4 elements with paginate():
public function index()
{
$proyects = Proyect::latest()->paginate(4);
return view('proyect.index', compact('proyects'));
}
In the view the 4 elements are in a single row but I would like to show 2 elements for each row.
<div class="row">
<div class="col-10 col-lg-12 col-md-6">
<div class="card-deck">
#forelse($proyects as $proyect)
<div class="card">
<div class="card-body">
<h5 class="card-title">{{ $proyect->title }}</h5>
<h6 class="card-subtitle mb-2 text-muted">
{{ $proyect->descripcion }}</h6>
<p class="card-text">You can use the cap image as an
overlay for the body</p>
<a href="{{ route('proyect.show', $proyect) }}"
class="card-link">Ver mas</a>
</div>
<div class="card-footer">
<small class="text-muted">
{{ $proyect->created_at->diffForHumans() }}</small>
</div>
</div>
#empty
<li> Empty </li>
#endforelse
</div>
</div>
How to show 2 items per row instead of all items in a row?
public function index()
{
$proyects = Proyect::latest()->paginate(10);
return view('proyect.index', compact('proyects'));
}
array_chunk is a php function ca splice your array.
#forelse(array_chunk($proyects->all(),2) as $rows)
<div class="row">
#foreach($rows as $proyect)
<div class="col-10 col-lg-12 col-md-6">
<div class="card-deck">
<div class="card">
<div class="card-body">
<h5 class="card-title">{{ $proyect->title }}</h5>
<h6 class="card-subtitle mb-2 text-muted">{{ $proyect->descripcion }}</h6>
<p class="card-text">You can use the cap image as overlay for the body</p>
Ver mas
</div>
<div class="card-footer">
<small class="text-muted">{{ $proyect->created_at->diffForHumans() }}</small>
</div>
</div>
</div>
</div>
#endforeach
</div>
#empty
<div>Empty ...</div>
#endforelse
May be this is your solution:
<div class="row">
#forelse($proyects as $proyect)
<div class="col-6 col-lg-6 col-md-6">
<div class="card-deck">
<div class="card">
<div class="card-body">
<h5 class="card-title">{{ $proyect->title }}</h5>
<h6 class="card-subtitle mb-2 text-muted">
{{ $proyect->descripcion }}</h6>
<p class="card-text">You can use the cap image as an
overlay for the body</p>
<a href="{{ route('proyect.show', $proyect) }}"
class="card-link">Ver mas</a>
</div>
<div class="card-footer">
<small class="text-muted">
{{ $proyect->created_at->diffForHumans() }}</small>
</div>
</div>
#empty
<li> Empty </li>
</div>
#endforelse
</div>

Laravel foreach database issue

I am making a forum and when the user clicks on a theme, a page shows up with every topic that belongs to that theme. The problem is, how do I do this?
I made a for each loop which shows ALL the topics from the database instead of the topics that belong to that theme I clicked on. How can I do this?
Web.php
Route::get('/', 'ThemesController#index')->name('home');
Route::get('/theme/{id}', 'ThemesController#show')->name('showtheme');
ThemesController.php
I only show the show method because I believe that's the only one necessary here
public function show($id)
{
$topics = Topic::all($);
return view('themes/topics')->with('topics', $topics);
}
topics.blade.php
#extends('layouts.default')
#section('content')
<div class="container main-content">
<div class="row first-row">
<div class="col s12">
<div class="card">
<div class="card-content clearfix">Nieuw topic</div>
</div>
</div>
<div class="col s12">
<div class="card">
<div class="card-content"><span class="card-title"> - Topics</span>
<div class="collection">
#foreach($topics as $topic)
<a href="" class="collection-item avatar collection-link"><img src="/uploads/avatars/{{ $topic->user->avatar }}" alt="" class="circle">
<div class="row">
<div class="col s6">
<div class="row last-row">
<div class="col s12"><span class="title">Theme - {{ $topic->topic_title }}</span>
<p>{!! str_limit($topic->topic_text, $limit = 100, $end = '...') !!}</p>
</div>
</div>
<div class="row last-row">
<div class="col s12 post-timestamp">Gepost door: {{ $topic->user->username }} op: {{ $topic->created_at }}</div>
</div>
</div>
<div class="col s2">
<h6 class="title center-align">Replies</h6>
<p class="center replies">{{ $topic->replies->count() }}</p>
</div>
<div class="col s2">
<h6 class="title center-align">Status</h6>
<div class="status-wrapper center-align"><span class="status-badge status-open">open</span></div>
</div>
<div class="col s2">
<h6 class="title center-align">Laatste reply</h6>
<p class="center-align">Naam</p>
<p class="center-align">Tijd</p>
</div>
</div>
</a>
#endforeach
</div>
</div>
</div>
</div>
<div class="col s12">
<div class="card">
<div class="card-content clearfix">Nieuw topic</div>
</div>
</div>
</div>
</div>
#endsection
I believe I'm doing something obviously wrong but I can't see what.
Help will be appreciated. Thanks in advance
If I miss some code, Please inform me.
How are you defining the relationship between a theme and it's topics? You'll have to have something like a one to many or many to many established in your database for you to be able to query correctly. Do you have anything like that yet?
****edit****
public function show($id)
{
$topics = Theme::find($id)->topics;
return view('themes/topics')->with('topics', $topics);
}

Categories