I'm working with laravel 5.6 to build my app.
When I visit the / my controller gets me an array of services like
#foreach($servicies as $service)
<div class="col-sm-4 wow fadeInDown" data-wow-duration="1000ms" data-wow-delay="300ms">
<a href="{{url("servicios/{$servicie->id}/{$service->name}")}}/">
<div class="service-icon">
<i class="fa fa-{{$service->icon}}"></i>
</div>
<div class="service-info">
<h3>{{$servicio->getAttribute('title-main')}}</h3>
<p class="btn-class">{{$servicio->getAttribute('text-main')}}</p>
</div>
</a>
</div>
#endforeach
that a I have is a route, but I'm not building all the services.
So, I would like to know if with laravel is there any easy way to check if a services is available or not, without having to put an special field in the database to check it
like
if($service->available){
return view....
}else{
return 404
}
I don't understand you very well, but from your question, you can do this in your views to resolve your problem
#if($services->count())
#foreach($servicies as $service)
<div class="col-sm-4 wow fadeInDown" data-wow-duration="1000ms" data-wow-delay="300ms">
<a href="{{url("servicios/{$servicie->id}/{$service->name}")}}/">
<div class="service-icon">
<i class="fa fa-{{$service->icon}}"></i>
</div>
<div class="service-info">
<h3>{{$servicio->getAttribute('title-main')}}</h3>
<p class="btn-class">{{$servicio->getAttribute('text-main')}}</p>
</div>
</a>
</div>
#endforeach
#else
<div class="alert alert-warning">No service available at this moment</div>
#endif
#if(!empty($services)
#foreach($servicies as $service)
<div class="col-sm-4 wow fadeInDown" data-wow-duration="1000ms" data-wow-delay="300ms">
<a href="{{url("servicios/{$servicie->id}/{$service->name}")}}/">
<div class="service-icon">
<i class="fa fa-{{$service->icon}}"></i>
</div>
<div class="service-info">
<h3>{{$servicio->getAttribute('title-main')}}</h3>
<p class="btn-class">{{$servicio->getAttribute('text-main')}}</p>
</div>
</a>
</div>
#endforeach
#else
Display whatever you want here
#endif
Related
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
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
I'm getting this error message for my code:
Undefined variable: questions (View:
home/xxxx/PhpstormProjects/xxxxx/resources/views/home.blade.php)
Please, try to shed some light on what could be wrong.
I have provided my QuestionController & question template. Not sure, what my code is missing. Here is my code:
QuestionController:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Question;
class QuestionController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function show(Question $question)
{
return view('question')->with('question', $question);
}
question.blade.php
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row ">
<div class="col-md-8">
<div class="card">
<div class="card-header">Question</div>
<div class="card-body">
{{$question->body}}
</div>
<div class="card-footer">
<a class="btn btn-primary float-right"
href="#">
Edit Question
</a>
</div>
</div>
</div>
<div class="col-md-4">
<div class="card">
<div class="card-header"><a class="btn btn-primary `float-left"`
href="#">
Answer Question
</a></div>
<div class="card-body">
#forelse($question->answers as $answer)
<div class="card">
<div class="card-body">{{$answer->body}}`</div>`
<div class="card-footer">
<a class="btn btn-primary float-right"
href="#">
View
</a>
</div>
</div>
#empty
<div class="card">
<div class="card-body"> No Answers</div>
</div>
#endforelse
</div>
</div>
</div>
#endsection
home.blade.php
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-12">
<div class="card">
<div class="card-header">Questions
<div class="card-body">
<div class="card-deck">
#foreach($questions as $question)
<div class="col-sm-4 d-flex align-items-stretch">
<div class="card mb-3 ">
<div class="card-header">
<small class="text-muted">
Updated: {{ $question->created_at->diffForHumans() }}
Answers: {{ $question->answers()->count() }}
</small>
</div>
<div class="card-body">
<p class="card-text">{{$question->body}}</p>
</div>
<div class="card-footer">
<p class="card-text">
<a class="btn btn-primary float-right" href="{{ route('question.show', ['id' => $question->id]) }}">
View
</a>
</p>
</div>
</div>
</div>
#endforeach
</div>
</div>
<div class="card-footer">
<div class="float-right">
{{ $questions->links() }}
</div>
</div>
</div>
</div>
</div>
</div>
#endsection
The first think that comes to my mind is that you are giving a variable named question to your view in your controller and you are trying to use $questions in your foreach when it must be $question as $q
The other thing is that you may want to pass all Questions rows you have to you view, it can be accomplished (this may not be the best solution) by editing your AppServiceProvider.php
You have to add this.
use View;
use App\Question;
public function boot()
{
//
View::share('questions',Question::all());
}
But if you do this, it will share the rows obtained with all your views.
hope this helps.
I am making a carousel which should display the info dynamically in it. And here i am trying to apply condition through which the result can be filtered but it doesn't seems to be working. Below is the code for it.(what i really want to achieve through this is to appear the results in a carousel). Any kind of help would be immensely appreciated
<div class="container">
<div class="row" style="padding: 75px">
#if($tours->continent_id == 5)
<div class="row">
<div class="col-md-9">
<h3>Featured {{$tours->continent->name}}</h3>
</div>
<div class="col-md-3">
<!-- Controls -->
<div class="controls pull-right hidden-xs">
<a class="left fa fa-chevron-left btn btn-success"
href="#carousel-example"
data-slide="prev"></a><a class="right fa fa-
chevron-
right btn btn-success" href="#carousel-example"
data-slide="next"></a>
</div>
</div>
</div>
<div id="carousel-example" class="carousel slide hidden-xs" data-
ride="carousel">
<!-- Wrapper for slides -->
<div class="carousel-inner">
<div class="item active">
<div class="row">
#foreach($tours as $tour)
<div class="col-sm-3">
<div class="col-item">
<div class="photo">
<img src="{{$tour->photo->file ?
asset($tour->photo->file): asset('images/404.png')}}"
class="img-responsive" alt="a" />
</div>
<div class="info">
<div class="row">
<div class="price col-md-6">
<h5>{{$tour->location}}</h5>
<h5 class="price-text-
color">
{{$tour->service}}</h5>
</div>
<div class="rating hidden-sm
col-md-
6">
<i class="price-text-color
fa
fa-star"></i><i class="price-text-color fa fa-star">
</i><i class="price-text-color
fa
fa-star"></i><i class="price-text-color fa fa-star">
</i><i class="fa fa-star"></i>
</div>
</div>
<div class="separator clear-left">
<p class="btn-add"> <i class="fa fa-shopping-cart"></i>Add to cart
</p>
<p class="btn-details"> <i class="fa fa-list"></i>More details
</p>
</div>
<div class="clearfix">
</div>
</div>
</div>
</div>
#endforeach
</div>
</div>
#endif
</div>
</div>
Here is the controller part of the code. which is including all the fillable values in the form view. which later can be used to apply conditions on the form or return it's table values in the form
public function index()
{$tours = Tour::all();
return view('admin/tour/slider',compact('tours'));}
Error page error message
What you want is to filter the tours and only interact with the ones that contain continent_id equals to 5?
In that case, you should improve the query on database
$tours = Tour::where('continent_id', 5)->get();
Or filter the collection
$tours->filter(function ($tour) {
return $tour->continent_id == 5;
}
In both cases, you won't be able to do {{ $tours->continent->name }} since you're working with a Collection of Tour, not with a Tour instance.
In order to print the title of the first tour you should do
$tours->first()->continent->name
All i had to do was to put if condition after the foreach statement for its values to be available for the loop at before that foreach loop i had to check for the existence of the values in the table like #if($tours). and it all worked nicely :)
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);
}