Laravel. Query Builder groupBy - php

So I have table named "orders" that has receipt_id to link it with the table named "receipts"
I want to group the orders that has the same receipt_id, so in my controller, I used this code;
$orders = DB::table('orders')
->where('status', 0)
->groupBy('receipt_id')
->get();
return $orders;
But unfortunately, it only returns the first one. I have a feeling that I shouldn't be using groupBy.
This is what it currently returns;
{
id: 1,
item: "12",
qty: 123,
price: "1823.00",
subtotal: "123.00",
receipt_id: 123,
status: 0,
created_at: null,
updated_at: null
}
EDIT:
I only want to query the receipts table, here's what my blade looks like right now and commented what I want to accomplish.
#forelse($orders as $order)
<div class="col-md-4">
<div class="panel panel-default">
<div class="panel-heading text-center">
{{$order->receipt_id}}
</div>
<div class="panel-body text-center">
<table class="table table-bordered table-responsive">
<thead>
<th>Order</th>
<th>Quantity</th>
</thead>
<tbody>
//all orders that has the same receipt_id will be showed here.
<tr>
<td>{{$order->item}}</td>
<td>{{$order->qty}}</td>
</tr>
</tbody>
</table>
</div>
<div class="panel-footer clearfix">
<button type="submit" class="btn btn-success pull-right" style="margin-right: 10px;">
<i class="fa fa-check-circle" aria-hidden="true"></i> Served
</button>
</div>
</div>
</div>
#empty
#endforelse
EDIT: Database structure for receipts and orders.
receipts;
Schema::create('receipts', function (Blueprint $table) {
$table->increments('id');
$table->decimal('total');
$table->decimal('vatable');
$table->decimal('vat');
$table->decimal('vat_exempt');
$table->decimal('vat_zero');
$table->decimal('amount_due');
$table->decimal('cash');
$table->decimal('change_due');
$table->integer('receipt_id');
$table->timestamps();
});
orders;
Schema::create('orders', function (Blueprint $table) {
$table->increments('id');
$table->string('item');
$table->integer('qty');
$table->decimal('price');
$table->decimal('subtotal');
$table->integer('receipt_id');
$table->boolean('status');
$table->timestamps();
});

Somehow I managed to solve my problem and I'll share my codes of course. :)
controller;
$receipt_ids = DB::table('orders')
->where('status', 0)
->orderBy('receipt_id', 'asc')
->groupBy('receipt_id')
->get();
$orders = DB::table('orders')
->where('status', 0)
->orderBy('receipt_id', 'asc')
->get();
return view('kitchens.index', compact('receipt_ids','orders'));
blade;
#extends('layouts.app')
<div class="panel panel-default">
<div class="panel-body" style="overflow: scroll; height:85vh;">
<div class="row">
#forelse($receipt_ids as $receipt_id)
<div class="col-md-4">
<div class="panel panel-default">
<div class="panel-heading text-center">
{{$receipt_id->receipt_id}}
</div>
<div class="panel-body">
<table class="table table-bordered table-responsive">
<thead>
<th>Name</th>
<th>Quantity</th>
</thead>
<tbody>
#foreach($orders as $order)
#if($receipt_id->receipt_id == $order->receipt_id)
<tr>
<td>{{$order->item}}</td>
<td>{{$order->qty}}</td>
</tr>
#endif
#endforeach
</tbody>
</table>
</div>
<div class="panel-footer clearfix">
<button type="submit" class="btn btn-success pull-right" style="margin-right: 10px;">
<i class="fa fa-check-circle" aria-hidden="true"></i> Served
</button>
</div>
</div>
</div>
#empty
#endforelse
</div>
</div>
</div>

Related

How to show table based on the category in Laravel

I have created a table called jeniskerja which contains the category (id's range from 1-5) of the pekerjaan's table. I already made the eloquent model for each of the said table;
jeniskerja (model)
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model as Eloquent;
class Jeniskerja extends Eloquent
{
use HasFactory;
protected $guarded = [];
public $timestamps = false;
public function pekerjaans(){
return $this->hasMany(Pekerjaan::class);
}
}
pekerjaan (model)
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model as Eloquent;
class Pekerjaan extends Eloquent
{
use HasFactory;
protected $guarded = [];
protected $dates = ['tanggal'];
public function penyedia(){
return $this->belongsTo(Penyedia::class, 'penyedia_id');
}
public function jeniskerja(){
return $this->belongsTo(Jeniskerja::class, 'jeniskerja_id');
}
public function user(){
return $this->belongsTo(User::class, 'user_id');
}
}
At the moment, when the user click on the vendor's name, it will direct them to show a detail of the vendor and the table list of work... What I need is to show the table based on the category id when the user click on the said category tab... Here is the picture to show the page
And here is my code to view the page:
<section class="content">
<div class="container-fluid">
<div class="card card-default color-palette-box">
<div class="card-header">
<h3 class="card-title">
<b>{{$penyedia->nama}}</b>
</h3>
</div>
<div class="card-body">
<div class="col-12">
<h5><i class="fas fa-id-card"></i>&nbsp NPWP: {{$penyedia->npwp}}</h5><br>
<h5><i class="fas fa-map-pin"></i>&nbsp Alamat: {{$penyedia->alamat}}</h5>
</div>
</div>
<hr class="solid" style="border-top: 1px solid;">
<div class="card-body">
<div class="col-12">
<h5><i class="fas fa-building"></i>&nbsp Bentuk Usaha: {{$penyedia->bentuk_usaha}}</h5><br>
<h5><i class="fas fa-envelope"></i>&nbsp Email: {{$penyedia->email}}</h5>
</div>
</div>
</div>
</div>
</section>
<section class="content">
<div class="container-fluid">
<!-- Small boxes (Stat box) -->
#foreach ($penyedia as $penyedias)
#endforeach
<div class="row">
{{-- <div class="col-lg-4 col-6">
<!-- small card -->
<div class="small-box bg-info">
<div class="inner">
<h3>{{$penyedia->count('nama')}}</h3>
<p>Penyedia</p>
</div>
<div class="icon">
<i class="fas fa-store"></i>
</div>
<a href="/datapenyedia" class="small-box-footer">
More info <i class="fas fa-arrow-circle-right"></i>
</a>
</div>
</div> --}}
<!-- ./col -->
<div class="col-lg-6 col-8">
<!-- small box -->
<div class="small-box bg-lightblue">
<div class="inner">
<h3>{{$pekerjaan->count('pekerjaan')}}</h3>
<p>Pekerjaan</p>
</div>
<div class="icon">
<i class="fas fa-map"></i>
</div>
More info <i class="fas fa-arrow-circle-right"></i>
</div>
</div>
<!-- ./col -->
<div class="col-lg-6 col-8">
<!-- small box -->
<div class="small-box bg-lightblue">
<div class="inner">
<h3>Rp. {{number_format($pekerjaan->sum('nilai_kontrak'))}}</h3>
<p>Nilai Kontrak</p>
</div>
<div class="icon">
<i class="fas fa-money-bill-wave"></i>
</div>
More info <i class="fas fa-arrow-circle-right"></i>
</div>
</div>
</div>
</section>
#foreach ($jeniskerja as $jeniskerjas)
<section class="content">
<div class="container-fluid">
<div class="card card-default collapsed-card">
<div class="card-header collapsed-card">
<h3 class="card-title">
<b>{{$jeniskerjas->nama_jenis}}</b>
</h3>
<div class="card-tools">
<button type="button" class="btn btn-tool" data-card-widget="collapse">
<i class="fas fa-plus"></i>
</button>
</div>
</div>
<div class="card-body table-responsive">
<table id="tabelpekerjaan" class="table table-bordered">
<thead>
<tr>
<th style="width: 10px" rowspan="2">Tahun Anggaran</th>
<th rowspan="2">Tanggal Kontrak</th>
<th rowspan="2">Paket Pekerjaan</th>
<th rowspan="2">Nama Perusahaan</th>
<th rowspan="2">Lokasi Pekerjaan</th>
<th rowspan="2">Jenis Pekerjaan</th>
<th rowspan="2">HPS</th>
<th rowspan="2">Nilai</th>
<th rowspan="2">Dokumen</th>
<th colspan="2">Tanggal</th>
</tr>
<tr>
<td>Tgl Buat</td>
<td>Tgl Update</td>
</tr>
</thead>
<tbody>
#php $no = 1; /*$totalNilai = 0.0;*/ #endphp
#foreach ($pekerjaan as $pekerjaans)
<tr>
<td>{{$pekerjaans->tanggal->format('Y')}}</td>
<td>{{$pekerjaans->tanggal->format('d/m/Y')}}</td>
<td>{{$pekerjaans->pekerjaan}}</td>
<td>{{$pekerjaans->penyedia->nama}}</td>
<td>{{$pekerjaans->lokasi}}</td>
<td>{{$pekerjaans->jeniskerja->nama_jenis}}</td>
<td>Rp. {{number_format($pekerjaans->hps,0,',',',')}}</td>
#php
$pekerjaans->nilai_total = $pekerjaans->nilai_1 + $pekerjaans->nilai_2 + $pekerjaans->nilai_3 + $pekerjaans->nilai_4;
#endphp
<td>{{$pekerjaans->nilai_total}}</td>
<td><u>{{$pekerjaans->status}}</u></td>
<td>
</td>
<td>
</td>
</tr>
#endforeach
</tbody>
</table>
</div>
</div>
</div>
</section>
#endforeach
And here is the AdminController file
public function showpenyedia(Penyedia $penyedia){
$pekerjaan = $penyedia->pekerjaans; //show pekerjaan(work) based on penyedia(vendor)
$jeniskerja = Jeniskerja::all();
return view('admin.showpenyedia', compact('penyedia','pekerjaan','jeniskerja'));
}

Display foreign key

I have 2 tables, the first is clubs with 3 fields (id_club, name_club, type_sport_club), the second table is membres with 3 fields (id_membre, name_membre, fk_club).
In my folder membres index.blade.php I get a error message
SQLSTATE[42S22]: Column not found: 1054 Champ 'clubs.id' unknown in where clause (SQL: select * from clubs where clubs.id = 1 limit 1) "
Is it I have forgot anything?
<div class="px-content">
<div class="page-header">
<div class="row">
<div class="col-md-4 text-xs-center text-md-left text-nowrap">
<h1><i class="px-nav-icon ion-android-apps"></i>Liste des enregistrements</h1>
</div>
<hr class="page-wide-block visible-xs visible-sm">
<!-- Spacer -->
<div class="m-b-2 visible-xs visible-sm clearfix"></div>
</div>
</div>
<div class="row">
<div class="panel">
<div class="panel-body">
<div class="table-responsive">
<table class="table">
<a class="btn btn-sm btn-success" href="{{ route('membres.create') }}">Ajouter</a>
<thead>
<tr>
<th>Name</th>
<th>Club</th>
</tr>
</thead>
#foreach($membres as $membre)
<tr>
<td> {{$membre->name_membre}}</td>
<td> {{$membre->club->name_club}}</td>
<td>
<form method="POST" action="{{ route('membres.destroy', $membre) }} ">
<a class="btn btn-sm btn-warning" href="{{route('membres.edit',$membre->id_membre)}}">Editer</a>
#csrf
#method('DELETE')
<button type="submit" class="btn btn-sm btn-danger">Deleter</button>
</form>
</td>
</tr>
#endforeach
</table>
</div>
</div>
</div>
</div>
{!! $membres->links() !!}
</div>
#endsection
For information
public function up()
{
Schema::create('membres', function (Blueprint $table) {
$table->increments('id_membre');
$table->string('name_membre');
$table->integer('fk_club')->unsigned();
$table->foreign('fk_club')->references('id_club')->on('clubs');
$table->timestamps();
});
}
Thank you
I'm pretty sure that you didn't pass the right key when you added the relation on the Membre model. It should be something similar to:
public function club()
{
return $this->belongsTo('App\Club', 'fk_club', 'id_club');
}
Also, you must update the relation to the Club model to:
public function membres()
{
return $this->hasMany('App\Membres', 'fk_club', 'id_club');
}

CRUD - Delete on Laravel

I am stuck on my button "delete" when I try to delete a recording nothing happens.
In my StudentController I have that:
public function destroy($id)
{
$student = Student::find($id);
$student->delete();
return redirect()->route('student.index')
->with('success', 'Deleted successfully');
}
And in my index.blade.php I have that:
#section('content')
<div class="px-content">
<div class="page-header">
<div class="row">
<div class="col-md-4 text-xs-center text-md-left text-nowrap">
<h1><i class="px-nav-icon ion-android-apps"></i>List </h1>
</div>
<hr class="page-wide-block visible-xs visible-sm">
<!-- Spacer -->
<div class="m-b-2 visible-xs visible-sm clearfix"></div>
</div>
</div>
<div class="row">
<div class="panel">
<div class="panel-body">
<div class="table-responsive">
<table class="table">
<a class="btn btn-sm btn-success" href="{{ route('student.create') }}">Create</a>
<thead>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>
</thead>
#foreach($students as $student)
<tr>
<td> {{$student->firstname}}</td>
<td> {{$student->lastname}} </td>
<td>
<a class="btn btn-sm btn-warning" href="{{route('student.edit',$student->id)}}">Edit</a>
#csrf
#method('DELETE')
<button type="submit" class="btn btn-sm btn-danger">Delete</button>
</td>
</tr>
#endforeach
</table>
</div>
</div>
</div>
</div>
{!! $students->links() !!}
</div>
#endsection
For the folder route
Auth::routes();
route::resource('student','AdminController');
Route::PATCH('/update/{id}','AdminController#update');
I don't understand where is the problem ? I would like to know my error because I don't have of error message in fact.
Thank you in advance.
You are missing the form so surround your button with a form tag like this:
<form method="POST" action="{{ route('student.destroy', $student) }} ">
#csrf
#method('DELETE')
<button type="submit" class="btn btn-sm btn-danger">Delete</button>
</form>

Search form/function no results, Laravel

I have this search form which is search.blade.php for my building.blade.php the problem is it doesn't give me the the office that I searched in the search field
This is what it looks like when I tried to use {{dd($offices)}}; when I click search button in my search.blade.php and then I removed it and it shows nothing, how do I make it work?
[
search.blade.php
#extends('layouts.main')
#section('title', $search)
#section('content')
<div class="search">
{!! Form::open(['method'=> 'GET','url'=>'offices','role'=>'search']) !!}
<div class="input-group col-xs-4 col-md-6" >
<input type="text" name="search" class="form-control" placeholder="Search...">
<span class="input-group-btn">
<button type="submit" class="btn btn-info btn-md">Search
</button>
</span>
</div>
{!! Form::close()!!}
</div>
<hr>
<table class="table">
<thead>
<th>Office Name</th>
<th>Belongs to</th>
<th>Office Floor</th>
</thead>
<tbody>
#foreach($offices as $office)
<tr>
<td>{{($office)->name}}</td>
<td>{{$office->building->name}}</td>
<td>{{$office->floor}}</td>
<td class="a">
#if(!Auth::guest())
Edit
Delete
#endif
</td>
</tr>
#endforeach
</tbody>
</table>
</div>
</div>
</div>
#endsection
Building.blade.php
#extends('layouts.main')
#section('title',$building->name)
#section('css')
#stop
#section('content')
<div class="container">
<div class="row">
<div class="col-lg-12">
<img src="{{URL::to('/assets')}}/{{$building->picture}}" alt="" style="height:300px; width:500px;">
</div>
</div>
<div class="row">
<div class="col-lg-12">
{{$building->name}}
</div>
</div>
</div>
</div>
<div class="rows">
<div class="col-md-6 col-md-offset-3">
<div class="col-xs-4 col-md-6">
#if(!Auth::guest())
Create an Office
#endif
</div>
{!! Form::open(['method'=> 'GET','url'=>'offices','role'=>'search']) !!}
<div class="input-group col-xs-4 col-md-6" >
<input type="text" name="search" class="form-control" placeholder="Search...">
<span class="input-group-btn">
<button type="submit" class="btn btn-info btn-md">Search</i>
</button>
</span>
</div>
{!! Form::close()!!}
<table class="table">
<div class="ttitle">
<thead>
<th>Office Name</th>
<th>Office Floor</th>
</thead>
<tbody>
#foreach($offices as $office)
<tr>
<td>{{optional($office)->name}}</td>
<td>{{$office->floor}}</td>
<td class="a">
#if(!Auth::guest())
Edit
Delete
#endif
</td>
</tr>
#endforeach
</tbody>
</table>
</div>
</div>
#endsection
OfficeController.php
public function index()
{
$search = \Request::get('search');
$offices = Office::where('name','like','%'.$search.'%')->get();
return view('search',compact('offices','search'));
}
Office.php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Office extends Model
{
public function building(){
return $this->belongsTo('App\Building');
}
}
Building.php
use Illuminate\Database\Eloquent\Model;
class Building extends Model
{
public $table = 'buildings';
public function offices(){
return $this->hasMany('App\Office');
}
}
Routes
Auth::routes();
Route::get('/', 'BuildingController#index')->name('index');
Route::get('building/{id}', 'PageController#show')->name('building');
Route::get('office/{id}', 'OfficeController#show')->name('officeMenu');
Route::get('offices', 'OfficeController#index');
Route::group(['middleware' => ['auth']], function () {
Route::get('buildings/create', 'BuildingController#create')->name('createbform');
Route::post('building/create/store', 'BuildingController#saveBuilding')->name('createbuilding');
Route::get('building/{id}/edit', 'BuildingController#edit');
Route::post('building/{id}/edit', 'BuildingController#update')->name('editbuilding');
Route::get('building/{id}/delete', 'BuildingController#destroy');
Route::get('building/{id}/offices/create', 'OfficeController#create')->name('createofficeform');
Route::post('building/{id}/offices/create/store', 'OfficeController#store')->name('createoffice');
Route::get('building/{id}/offices/{office_id}/edit', 'OfficeController#edit')->name('editofficeform');
Route::post('building/{id}/offices/{office_id}/edit', 'OfficeController#update')->name('editoffice');
Route::get('offices/{id}/delete', 'OfficeController#destroy')->name('deleteoffice');
});
You should use ILIKE instead of LIKE for this, because, the name of the Office is Case Room and you're looking for an office name that has the words "case". Given that LIKE in SQL is case sensitive, it will not find what are you looking for.
Instead of using this one Edit I used this as what #oscar.rpr said
Edit

Displaying data from Blade model using the click function

I am using the button to display the details of the particular person using the data-toggle="modal" but the button is not performing any action. When I click the button I am not able to display any details of the user.
Route:
Route::get('document', 'PatientController#show');
Controller:
public function show()
{
$patient_user=patient_user::all();
return view('document')->with('patient_user',$patient_user);
}
View:
<form id="myForm" method="get">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="panel-body">
<table class="table table-striped table-bordered table-hover table-full-width" id="sample_1">
<thead>
<tr>
<th>Patient ID</th>
<th>Patient Name</th>
<th>Full Name</th>
<th>DOB</th>
<th>Ref. Name</th>
<th>Payment</th>
<th>Doc Status</th>
<th>Appointment</th>
<th>Action</th>
</tr>
</thead>
#foreach($patient_user as $key=>$row)
<tbody>
<tr>
<td>{{$row->patient_id}}</td>
<td>{{$row->patient_firstname}}</td>
<td>{{$row->patient_firstname}} {{$row->patient_lastname}}</td>
<td>{{$row->dob}}</td>
<td>{{$row->refering_physician_name}}</td>
<td>{{$row->dob}} </td>
<td>{{$row->dob}}</td>
<td>{{$row->app_date}}</td>
<td class="center">
<div class="visible-md visible-lg hidden-sm hidden-xs">
<i class="fa fa-edit"></i>
<i class="fa fa-eye"></i>
<i class="fa fa-times fa fa-white"></i>
</div>
</td>
</tr>
</tbody>
#endforeach
</table>
</div>
<div id="static" class="modal fade" tabindex="-1" data-backdrop="static" data-keyboard="false" style="display: none;">
<div class="modal-body">
<h4><i>Patient Details</i></h4>
</div>
<div class="modal-body">
<div class="col-md-12">
<div class="col-md-6">First Name</div>
<div class="col-md-5" id="fname"> {{ $row->patient_firstname}} </div>
</div>
<br>
<div class="col-md-12">
<div class="col-md-6">Middle Name</div>
<div class="col-md-5" id="mname"> {{ $row->middle_name}} </div>
</div>
<br>
<div class="col-md-12">
<div class="col-md-6">Last Name</div>
<div class="col-md-5" id="lname"> {{ $row->patient_lastname}} </div>
</div>
</div>
</div>
</form>
jquery:
<script type="text/javascript">
$('#static').on('show', function(e)
{
e.preventDefault();
var link = e.relatedTarget(),
modal = $(this),
patient_firstname = link.data("patient_firstname"),
middle_name = link.data(middle_name),
patient_lastname = link.data("patient_lastname"),
modal.find("#fname").val(patient_firstname);
modal.find("#mname").val(middle_name);
modal.find("#lname").val(patient_lastname);
});
</script>
where #static is my modal Id and the value within find are the id's of the particular fields and val are the database values.
The problem is that you're requesting data from attributes that are not set.
patient_firstname = link.data("patient_firstname"),
The above part is trying to retrieve the value of the attribute data-patient_firstname. Since the original link does not have this attribute there won't be any data.
Change
<i class="fa fa-eye"></i>
Into
<a href='#' data-toggle="modal"
data-patient_firstname="{{$row->patient_firstname}}" <-- do this for all requested vars
class="btn btn-xs btn-green tooltips" data-id="{{ $row->patient_id}}" data-target="static" onclick="pat_det{{ $row->patient_id }}" data-placement="top" data-original-title="View" id="pat_det"><i class="fa fa-eye"></i></a>
That way the data-* attributes will be availble.

Categories