Laravel how to get count specific data by school id? - laravel-5

I have laravel queries below
$school = DB::table('sekolah')->get();
$morning_session = DB::table('sekolah')->join('pelajar', 'sekolah.sekolah_id', '=', 'pelajar.sekolah_id') ->where('pelajar.pelajar_sesi', 'like', 'Morning')->groupBy('pelajar.sekolah_id')->count();
$afternoon_session = DB::table('sekolah')->join('pelajar', 'sekolah.sekolah_id', '=', 'pelajar.sekolah_id')->where('pelajar.pelajar_sesi', 'like', 'Afternoon')->groupBy('pelajar.sekolah_id')->count();
my blade look like this
#foreach($school as $d)
<div class="col-md-6">
<i class="fa fa-mortar-board" style="font-size:80px;display:block;text-align:center"></i>
<h4>{{$d->sekolah_nama}}</h4>
<!-- morning session -->
<i class="fa fa-calendar" aria-hidden="true"></i> Morning: {{$morning_session}} people
<!-- afternoon session -->
<i class="fa fa-calendar" aria-hidden="true"></i> Afternoon: {{$afternoon_session}}
</div>
#endforeach
I want make a count for morning and afternoon session foreach school id. but the result show only pelajar.sekolah_id = 1 school website . You can refer my table below at student table

Based on what I can see, you might be able to use something like this:
$morning_session = DB::table('pelajar')
->select('sekolah_id', DB::raw('count(*) as total'))
->where('pelajar_sesi', 'like', 'Morning')
->groupBy('sekolah_id')
->pluck('total','sekolah_id')->all();
$afternoon_session = DB::table('pelajar')
->select('sekolah_id', DB::raw('count(*) as total'))
->where('pelajar_sesi', 'like', 'Afternoon')
->groupBy('sekolah_id')
->pluck('total','sekolah_id')->all();
Then in your view something like:
Morning: {{!empty($morning_session[$d->sekolah_id]) ? $morning_session[$d->sekolah_id] : '0'}} people
I hope I am understanding your question and this helps.

Related

Curly brackets are appearing along with the variable name when I try to echo on laraval

When I try to echo and display the output of a query in my dashboard it is appearing like this
[{"job_type":"Sales Manager"}]
The query is this:
->where('id',$userId)
->select('job_type')
->get();
To display it on the dashboard I am using this code
<div class="col-md-6 col-lg-2 col-xlg-3">
<div class="card card-hover">
<div class="box bg-cyan text-center">
<h1 class="font-light text-white"><i class="mdi mdi-view-dashboard"></i></h1>
<h5 class="m-b-0 m-t-5 text-white">{{ $jobType }}</h5>
<h6 class="text-white">Designation</h6>
</div>
</div>
</div>
How do I just get the result instead of the variable name along with the brackets?
You can use this query
->where('id',$userId)
->pluck('job_type')
->first();
You should print it like {{$jobType->job_type}} or {{$jobType['job_type']}} based on your return type.
->where('id',$userId)
->select('job_type')
->get();
This code will return Collection instance so you should either use first(); or you iterate over the variable. You probably try to get the user so you should use it like this:
$user = User::where('id', $userId)->first();
and use below code in your view
{{ $user->job_type }}
Or you can use
$user = User::find($userId);

Undefined index: id Laravel 5.8

My Tables:
kategoris table
id | kode_kategori | kategori_name |
items table
id | kategori_id | item_name
In items table the kategori_id column has foreignkey.
My Controller:
public function edit($id)
{
// $item = Item::findOrFail($id);
$item = DB::table('items')
->join('kategoris', 'items.kategori_id', '=', 'kategoris.id')
->where('items.id', '=', $id)
->select('items.*', 'kategoris.*', 'items.id', 'items.kategori_id')
->get();
// dd($item);
return view('master-dev/item/edit', compact('item'));
}
My View:
<div class="card card-default">
{{ Form::model($item,['route'=>['item.update',$item['id']], 'files'=>true,'method'=>'PUT']) }}
<div class="card-header">
<h3 class="card-title"><b>Edit Data Item</b></h3>
<div class="card-tools">
<button type="button" class="btn btn-tool" data-card-widget="collapse"><i class="fas fa-minus"></i></button>
</div>
</div>
<!-- /.card-header -->
<div class="card-body">
#if(!empty($errors->all()))
<div class="alert alert-danger">
{{ Html::ul($errors->all())}}
</div>
#endif
<div class="row">
<div class="col-md-6">
<div class="form-group">
{{ Form::label('kode_kategori', 'Kode Kategori') }}
<select name="kode_kategori" id="kode_kategori" class="form-control">
#foreach ($item as $i)
<option valu="{{ $i['kode_kategori'] }}">{{ $i['kode_kategori'] }}</option>
#endforeach
</select>
</div>
</div>
..........
..........
{{ Form::close() }}
I've tried any solutions in stackoverflow such as adding (ifempty...) and other solution but still the result Undefined index: id in my edit blade. When I was trying using dd and vardump the results was shown. I need to loop the foreach in my dropdown menu to show the list of data from my categories table. And I need to join my items table and my categories table to get the name of the categories.
you are calling same id from items and kategoris try this
public function edit($id)
{
// $item = Item::findOrFail($id);
$item = DB::table('items')
->join('kategoris', 'items.kategori_id', '=', 'kategoris.id')
->where('items.id', '=', $id)
->select('items.*', 'kategoris.id as kategory_id', 'kategoris.kode_kategori', 'kategoris.kategori_name')
->get();
// dd($item);
return view('master-dev/item/edit', compact('item'));
}
if this answer doesnot work show your database relation i will give you solution
$item = ....->get() will return a Collection to only have one item you need to use $item = ....->first() instead
But since you have #foreach ($item as $i) I believe, you still want to have a collection, but in that case, your issue is here
{{ Form::model($item,['route'=>['item.update',$item['id']], 'files'=>true,'method'=>'PUT']) }}
Since you have a collection, we don't know what $item['id'] it's referring to. Perhaps $item->first()['id'] ?
I solved the problem, there's a conflict fetching data from items and kategoris tables. There are differences calling a value with array and object, mostly if its data looped. So in the controller I must declared one by one, the selected value id from kategoris table, and I have to join both tables to get the name of the kategoris, and then I have to declare once more to get the list of the kategoris data. So there are three (3) variables to declare each one of them. For this long time I was looking for the short code in my Controller but I cannot find it.
Thank you for all of you guys helping me this problem. Cheers.

How to display data value according to dropdown in laravel?

I have a table in my database which has a field lis_type and the field has 2 values: sale and rent, and I am calling data on my view page. Let's suppose I click on the Sale page, there only sale similar listing should be displayed. If I click on Rent then similar listing should be shown for rent. However, right now both listings are coming together, Please let me guide where I am making a mistake.
Controller
public function listingshownow(Request $r, $slug)
{
$listview = Listing::where('slug', $slug)->first();
$products = Listing::where('propId', $listview->propId)
->where(function ($query) {
$query->where('lis_type', '=', 'sale')->orWhere('lis_type', '=', 'rent');
})->get();
return view('listings-view', compact('products'));
}
View for Similar Listings...
#foreach($products as $prod)
#if($prod->lis_type === 'sale')
<div class="row featured portfolio-items">
<div class="col-lg-7 col-md-12 homes-content pb-0 mb-44">
<ul class="homes-list clearfix">
<li>
<i class="fa fa-bed" aria-hidden="true"></i>
<span>{{$prod->ls_fs}}</span>
</li>
<li>
<i class="fa fa-bath" aria-hidden="true"></i>
<span>{{$prod->bathroom}} Bathroom</span>
</li>
</ul>
</div>
</div>
#endif
#endforeach
You need to pass a variable to your controller to indicate what kind of page you are viewing, so you can use that in your query/filter.
For example, let's say your sales page URL looks like http://example.com/listings/sales. Then you could set up a route like:
Route::get('listings/{type}', 'ListingsController#show');
Now your ListingsController could have a method like:
// The $type parameter will be what was matched in the route
public function show($type) {
$listings = Listings::where('lis_type', $type)->get();
return view('listings-view', ['listings' => $listings]);
}
You might want to check that $type only matches the values you expect. You could either do that in the controller, eg:
// At the top of show()
if ($type !== 'sales' && $type !== 'rent') {
abort(404);
}
Or you could restrict it in your route, by specifying that the {type} there has to match some pattern:
Route::get('listings/{type}', 'ListingsController#show')->where('type', 'sales|rent');
All of this is covered in the Laravel routing docs, I suggest reading them, and better yet, take some minutes and browse all the docs! 30min skimming will give you a broad idea of how things work, and the knowledge of where to look and read more when you need a question answered.

HOw to make simple search with laravel

i try to create forms search in laravel..
when the title of the title article is searched .. then the title will appear.. i am search title article form Article Table
this is my HomeController
...
public function search(Request $request){
$cari = $request->get('search');
$Title = Article::where('title', 'LIKE', '%' .$cari . '%')->paginate(10);
return view('/article/show', $cari);
}
this is my header.blade.php
**...
<div class = "col-md-4">
{!! Form::open(['method'=>'GET', 'url'=>'/article/show', 'role'=>'search']) !!}
<div class= "input-group custom-search-form">
<input type="text" class="form-control" name="search" placeholder="Judul..">
<span class="input-group-btn">
<span class="input-group-btn">
<button class="btn-btn-default" type="submit"><i class="fa fa-search"></i>Cari</button>
</span>
</span>
{!! Form::close()!! }
</div>
</div>
thi is my route..
..
Route::get('/article/show', 'HomeController#search');
.
But when i am typing on search form.. i am getting error like this
(2/2) QueryException
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'show AND is_show = TRUE and `articles`.`deleted_at` is null' at line 1 (SQL: select count(*) as aggregate from `articles` where category_id=show AND is_show = TRUE and `articles`.`deleted_at` is null)
.
please tell me which part is wrong
Thanks...
You're sending a POST request as GET. Switch the GET to POST, or include the searched term in the URL and remove the Request $request part of your search(Request $request) function since GET will not provide an instance of Request. Just make it search($terms) instead if you go that route. I would personally just switch to POST though.
IE:
Route::post('/article/show', 'HomeController#search');
{!! Form::open(['method'=>'POST', 'url'=>'/arti...
In your form add
{{ csrf_token() }}
Also you will want to check if 'search' has a value, so wrap it around a if ($request->search)
Also, it looks like you may have a SQL error somewhere else on the page which is cause this.
check your table name, or try this
public function search(Request $request)
{
$cari = $request->get('search');
$data['result']= DB::table('articles')->WHERE('title', 'LIKE', '%' .$cari . '%')->paginate(10);
return view('/article/show', $data);
}
your form
<form class="navbar-form navbar-left" method="GET" action="{{url('search')}}">
<div class="input-group">
<input type="text" class="form-control" placeholder="Search" name="search">
<button class="btn btn-default" type="submit">
<i class="fa fa-search"></i>
</button>
</div>
</form>

Previous / next page links using Laravel

I am developing a small portfolio site and i am stuck for a second.
So users are allowed to upload their sort them ( with jQuery sortable )
So when other users view his / her portfolio it is displayed by sort order, and when you click on the image it shows a big image.
Here i would like a Previous / next navigation so people can navigate.
It works but i have a problem, so currently in my database the image with the 8 is the last one, but because the images are displayed by sort order, and the image with id 8 which is the last is in the fifth place it stops returning the next id, and it should be there.
Here is what i tried.
Controller
public function show($id)
{
$photo = $this->photo->find($id);
if(is_null($photo)) return App::abort('404');
$previous = $this->photo->where('id', '<', $photo->id)->where('user_id', '=', $photo->user_id)->orderBy('id', 'DESC')->first();
$next = $this->photo->where('id', '>', $photo->id)->where('user_id', '=', $photo->user_id)->orderBy('sort', 'ASC')->first();
$this->layout->title = "Saját képek";
$this->layout->content = View::make('photo::show')
->with('photo', $photo)
->with('previous', $previous)
->with('next', $next);
}
View
<div class="ui page grid">
<div class="ui grid">
<div class="wide column">
<div class="ui segment">
<div class="photo-viev-nav">
#if(!empty($previous->id))
<i class="ui left icon"></i> Vissza
#endif
<strong>{{ $photo->user->name() }} potfóliója</strong>
#if(!empty($next))
Következő <i class="ui right icon"></i>
#endif
</div>
<div class="photo-view">
{{ HTML::image($photo->photoOriginal(), '', array('class' => 'ui huge image')) }}
</div>
</div>
</div>
</div>
The second i tried is this query
$previous = $this->photo->where('id', '<', $photo->id)->where('user_id', '=', $photo->user_id)->orderBy('sort', 'ASC')->max('id');
$next = $this->photo->where('id', '>', $photo->id)->where('user_id', '=', $photo->user_id)->orderBy('sort', 'ASC')->min('id');
But the problem with this it ignores the sort order
Could please someone give me a hint?
Experimented more, and i was a bit silly, instead of focusing on the id i needed to focus on the sort order and get the id's based on that way
Correct query
$previous = $this->photo->where('sort', '<', $photo->sort)->where('user_id', '=', $photo->user_id)->orderBy('', 'DESC')->first();
$next = $this->photo->where('sort', '>', $photo->sort)->where('user_id', '=', $photo->user_id)->orderBy('sort', 'ASC')->first();

Resources