Laravel search query with different tables - php

There are three tables; venues, venues_parameters and calendars tables. On a venue table all venues added and venue details added on venues_parameters table. On a calendar table, a booked (1st half, 2nd half, full day with flag 1, 2 & 3) venue detailed store with date and other information.
What i want that if i am searching with today date, date it is check on the calendar table that the searchable date are already booked or not, if date booked with 1st half or 2nd half venue will available on search else not available.
My controller code :
public function index(Request $request, $event_type = '', $capacity = '', $venue_type = '', $catering = '')
{
//dd($request->datepicker); return date (format : 25/12/2017)
$venueQuery = Venue::whereStatus('Active');
dd($venueQuery);
if($request->occasion) {
$event_type = $request->occasion;
}
if($request->datepicker) {
$selectedDate = $request->datepicker;
} else {
$selectedDate = date('m/d/Y');
}
if($request->capacity) {
$capacity = $request->capacity;
}
if($request->venue_type_id) {
$venue_type = $request->venue_type_id;
}
if($request->catering) {
$catering = $request->catering;
}
if($event_type != '') {
$venueQuery->whereHas('occasions', function ($query) use ($event_type) {
$query->where('occasions.id', $event_type);
});
}
if($capacity != '') {
$venueQuery->whereHas('venueParameter', function ($query) use ($capacity) {
$query->where('venue_parameters.max_capacity', $capacity);
});
}
if($venue_type != '') {
$venueQuery->whereHas('venueType', function ($query) use ($venue_type) {
$query->where('venue_types.id', $venue_type);
});
}
if($catering != '') {
$venueQuery->whereHas('venueParameter', function ($query) use ($catering) {
if($catering == "indoor") {
$catering = "1";
$query->where('venue_parameters.is_indoor', $catering);
} if($catering == "outdoor") {
$catering = "1";
$query->where('venue_parameters.is_outdoor', $catering);
}
});
}
$venues = $venueQuery->get();
$venues_count = count($venues);
return view('front.venue.index', compact('venues','event_type','venues_count','selectedDate'));
}
Calendar table for the reference :

Related

Automatically filling in sql column data after referencing a substring of another column

Currently I'm using Mysql and CodeIgniters MVC framework to fill in my data. The table logs every status change that has been made and when it was made. This is what the database currently looks like:
I've added new columns in the table called status_from and status_to, where I want these columns to take the substring from action column.
But now how do I display it in my database with my following code:
Controller class:
public function status($status){
$statusar = array('D'=>'Draft','N'=>'Unpublish','Y'=>'Publish','U'=>'Action','L'=>'Unlisted','S'=>'Sold','T'=>'Let');
if($this->input->post('id')){
foreach($this->input->post('id') as $key => $id):
$check = $this->listings_model->loadlisting_check($id);
$log = "Listing website status changed from ". $statusar[$check->status]." to ".$statusar[$status].". The listing ID is #".$id.".";
$this->logs_model->insert_log(array('refno'=>$check->refno,'action'=>trim($log)));
$data=array('status'=>$status);
if($status == 'T' || $status == 'Y' || $status == 'S'){
$pub = 1;
}else{
$pub =0;
}
$this->listings_model->lpupdate(array('property_publish'=>$pub),$id);
endforeach;
}
return true;
}
listings_model:
function loadlisting_check($id)
{
$this->db->select("refno, status, archive");
$id=$this->db->escape_str($id);
$cond=array("$this->table_name.$this->primary_key"=>$id);
$this->db->where($cond);
$this->db->from($this->table_name);
$query = $this->db->get();
return $query->row();
}
logs_model:
public function insert_log($log)
{
$log['agent_id'] = $this->session->userdata('clientsessuserid');
$log['logtime'] = date('Y-m-d H:i:s');
$this->db->insert($this->table_name, $log);
return true;
}
Basically I want to fill my status_from column with $statusar[$check->status] and status_to column with $statusar[$status] when every new entry is made
You can pass the variables you want to add in the array in insert_log method parameter with proper key matched with column name in your table
public function status($status){
$statusar = array('D'=>'Draft','N'=>'Unpublish','Y'=>'Publish','U'=>'Action','L'=>'Unlisted','S'=>'Sold','T'=>'Let');
if($this->input->post('id')){
foreach($this->input->post('id') as $key => $id):
$check = $this->listings_model->loadlisting_check($id);
$log = "Listing website status changed from ". $statusar[$check->status]." to ".$statusar[$status].". The listing ID is #".$id.".";
$this->logs_model->insert_log(array('refno'=>$check->refno,'action'=>trim($log) , 'status_from'=>$statusar[$check->status] ,'status_to'=>$statusar[$status]));
$data=array('status'=>$status);
if($status == 'T' || $status == 'Y' || $status == 'S'){
$pub = 1;
}else{
$pub =0;
}
$this->listings_model->lpupdate(array('property_publish'=>$pub),$id);
endforeach;
}
return true;
}
And you will not need to add anything else to your model

How to filter products in e-commerce shop by many inputs when some values can be empty

enter image description here
I have several inputs in order to filter products in the online shop. My question is, how can I filter products if some inputs are left without being filled/chosen. How should I query?
public function find()
{
$categories = Category::all();
if (isset($_GET['submit'])) {
if (!empty($_GET['brand'])) {
$selectedBrand = $_GET['brand'];
echo 'You have chosen: ' . $selectedBrand;
} else {
echo 'Please select the value.';
}
$date = Request::get('date');
$name = Request::get('name');
$selected = $_GET['type'];
$data = DB::table('product')->where('product.type', $_GET['type'])
->where('product.name', $name)
->join('shop', 'product.id', '=', 'shop.product_id')
->where('shop.releasedate', $date)
->get();
return view('pages/catalog')->with(['product' => $data, 'categories' => $categories]);
}
}
You can first check if your fields are filled and continue to query your model with when method
Logic
$date = null;
if($request->filled('date)){
$date = $request->date;
}
// your other values can go here like above
$data = DB::table('product')->where('product.type', $_GET['type'])
->where('product.name', $name)
->join('shop', 'product.id', '=', 'shop.product_id')
->when($date, function ($query, $transmission) {
// this query runs only if $date is `true` (has a value and not empty)
return return $query->where('shop.releasedate','=', $date);
->orderBy('shop.created_at','desc);
}, function ($query) {
// something you want to return if the $date is `false` (empty)
})
->get();

Codeigniter MYSQL data minus date1 data from date2

This is my Codeigniter project, I've made a bookshop function that counts the amount of brought and sold for the book in each entry and minus the sold price and gives me the amount that I currently have for each book.
Type "0" = The amount that I received
Else it's the amount that I spent
What I want to do now is I want to fetch the total amount that I received between date1 and date2 which should look something like "get_book_amount" for date1 -(minus) "get_book_amount" from date2.
The equation is like this:
On Date1 total amount that I received is 100, on Date2 it's 300. So the total amount that increased during that time is "200" which is the number I want to get.
How can I achieve this?
public function bookshop($date1,$date2)
{
$total_books = 0;
$books = '';
$this->db->select("*");
$this->db->from('books_s');
$this->db->where(['books_s.type' => 'Fantasy']);
$this->db->where(['books_s.stock' => 'Yes']);
$query = $this->db->get();
if($query->num_rows() > 0)
{
$count_books = $query->result();
if($count_books != NULL)
{
foreach ($count_books as $single_book)
{
$getamount = $this->get_book_amount($single_book->id,$date1,$date2);
if($getamount > 0)
{
$amt = $getamount;
}
else
{
$amt = -($getamount);
}
$total_books = $total_books+$amt;
$books .= '<tr><td><h4>'.$single_book->name.'</h4></td>
<td style="text-align:right" ><h4>'.$amt.'</h4></td></tr>';
}
$books .= '<tr"><td ><h4><i>Total Current Assets</i></h4></td><td style="text-align:right;" ><h4><i>'.$total_books.'</i></h4></td></tr>';
}
}
}
//USED TO COUNT SINGLE BOOK AMOUNT
public function get_book_amount($warehouse_id,$date1,$date2)
{
$count_total_amount = 0;
$this->db->select("bookentry.id as transaction_id,bookentry.date,bookentry.naration,book_stock_entry.*");
$this->db->from('book_stock_entry');
$this->db->join('bookentry', 'bookentry.id = book_stock_entry.parent_id');
$this->db->where('book_stock_entry.warehouse', $warehouse_id);
$this->db->where('bookentry.date >=', $date1);
$this->db->where('bookentry.date <=', $date2);
$query = $this->db->get();
if ($query->num_rows() > 0)
{
$count_books = $query->result();
$count_total_amount = 0;
if($count_books != NULL)
{
foreach ($count_books as $single_book)
{
if($single_book->type == 0)
{
$count_total_amount = $count_total_amount + $single_book->getamount;
}
else
{
$count_total_amount = $count_total_amount - $single_book->getamount;
}
}
}
}
if($count_total_amount == 0)
{
$count_total_amount = NULL;
}
else
{
$count_total_amount = number_format($count_total_amount,'3','.','');
}
return $count_total_amount;
}
I think it's best if you do two separete querys and then calculate.
Create a Model Method call "get_book_amount_until_date"
First call the method with your date 1 and save it in a var.
Second call the method with your date 2 and save it in another var.
Now do:
Total = Results_date_2 - Results_date_1

Codeigniter on duplicate key insert

i want to create a condition which a user would choose whether he wants to use an input which means a database would create an auto_incremented id or he wants to use an older data which will not use new id but only use it. i have used dropdown database populate for my old input.
My Dropdown list
//get contractor list
$Contractor_List = $this->foo_pro->get_list_contractors();
$opt = array('' => '');
foreach ($Contractor_List as $Contractor_No) {
$opt[$Contractor_No] = $Contractor_No;
}
$data['con_list'] = form_dropdown('',$opt,'','ProjectID = "Contractor_No" name="contractorNo" id="" class="w3-select w3-border w3-hover-light-grey"');
My Controller
public function save_createdProject()
{
$data_project = array();
$data_contractor = array();
//project data
if ($this->input->post('year') === '') {
$data_project['P_Year'] = 15;
}else{
$data_project['P_Year'] = $this->input->post('year');
}
if ($this->input->post('code') === '') {
$data_project['Code'] = 'KO';
}else{
$data_project['Code'] = $this->input->post('code');
}
$data_project['ProjectID'] = $this->input->post('project');
$data_project['Contract_Amount'] = $this->input->post('camount');
//contractor data
if ($this->input->post('cname') === '' && $this->input->post('caddress') === '') {
$data_project['Contractor_No'] = $this->input->post('contractorNo');
}else{
$data_contractor['Contractor_Name'] = $this->input->post('cname');
$data_contractor['Contractor_Address'] = $this->input->post('caddress');
}
$this->foo_pro->add_project($data_project, $data_contractor);
redirect('Main/project','refresh');
//var_dump($data);exit;
}
My Model
public function add_project($data_project, $data_contractor)
{
//contractor
$this->db->insert('contractor', $data_contractor);
$Contractor_No = $this->db->insert_id();
//project
$data_project['Contractor_No'] = $Contractor_No;
$this->db->insert('project', $data_project);
$ProjectID = $this->db->insert_id();
}
this here is my problem except inserting the older data which is the contractor_no 1 it creates another data that would insert as contractor_no 4.. but also i need to insert new data for new contractor_name..

codeigniter mysql query output data display with specific condition

here are the following codes:
controller.php
function getchart() {
$prac = $this->input->post('prac_name');
$datee = $this->input->post('datee');
$this->load->model('appoint');
$results['appoint'] = $this->appoint->getappoint($prac , $datee);
$this->load->view('ajax/getappchart' , $results);
}
model.php
function getappoint($prac , $datee) {
$this->db->select('rdv.id as rdvid, startTime, endTime, day, firstname, lastname');
$this->db->from('rdv');
$this->db->join('contact', 'contact.id = rdv.contact_id');
$this->db->where('people_id',$practicien);
$this->db->where('DATE(day)', $datee);
$this->db->order_by('TIME(startTime)', 'ASC');
$query = $this->db->get();
//print_r($this->db->last_query());
return $query;
}
view.php
if ($appoint->num_rows() > 0) {
foreach($appoint->result() as $sub_row)
{
// display output.
}
} else {
echo 'No Appointments on Above Date.';
}
?>
what i need is, there are appointments with sametime and day(mostly 2 same).
if there is morethan 2, i need to set two different class style for both the appointment.
how can i achieve this ?
Thanks.
Final answer: done it with the help of #minhaz-ahmed
if ($appoint->num_rows() > 0) {
$appoint_counter = array();
foreach ($appoint->result() as $sub_row) {
//i am assuming your startTime is H:M:S, and day Y-M-D format
$key = strtotime($sub_row['day'] . ' ' . $sub_row['startTime']);
if (!isset($appoint_counter[$key])) {
$appoint_counter[$key] = 0;
}
$appoint_counter[$key] ++;
$style_class = 'YOUR_1ST_CLASS';
if ($appoint_counter[$key] > 2) {
$style_class = 'YOUR_2ND_CLASS';
}
//REST VIEW CODE
}
}
else {
echo 'No Appointments on Above Date.';
}
You can do like this
if ($appoint->num_rows() > 0) {
$appoint_counter = array();
foreach ($appoint->result() as $sub_row) {
//i am assuming your startTime is H:M:S, and day Y-M-D format
$key = strtotime($sub_row['day'] . ' ' . $sub_row['startTime']);
if (!isset($appoint_counter[$key])) {
$appoint_counter[$key] = 0;
}
$appoint_counter[$key] ++;
}
foreach ($appoint->result() as $sub_row) {
//i am assuming your startTime is H:M:S, and day Y-M-D format
$key = strtotime($sub_row['day'] . ' ' . $sub_row['startTime']);
$style_class = 'YOUR_1ST_CLASS';
if ($appoint_counter[$key] > 2) {
$style_class = 'YOUR_2ND_CLASS';
}
//YOUR REST VIEW
}
} else {
echo 'No Appointments on Above Date.';
}
What do you mean by class style? Anyway, the code below assumes you will put the results in a datagrid.
$prev_date = "";
$prev_time = "";
$results = $query->result();
foreach($results as $appointment) {
if ($prev_date != $appointment->day) // Assuming "day" is tables date
# Display output here
if ($prev_time != $appointment->startTime) // Logically, display should sort appointments by their starting time
# Display output here
# Display appointment rows here
$prev_date = $appointment->day;
$prev_time = $appointment->startTime;
}
if (empty($results)) {
# Display "no appointments found" output here
}

Categories