I want to know that how can I get that how many days are left in course duration.
course-duration is just a column and hold an integer in database like 30.
I want to compare course-duration with created_at and return me that left days in laravel.
id username course course-duration(days) created_at
---------------------------------------------------------------------
1 krish SSB 14 2018-11-19
---------------------------------------------------------------------
2 Brij SSB 30 2018-11-18
---------------------------------------------------------------------
3 Sagar SSB 90 2018-11-15
I want to get remaining days of the course after comparing course-duration with created_at.
You can get the remain days like this:
\DB::statement("SELECT (course-duration - DATEDIFF(NOW(), created_at)) remained FROM your_table");
You can use Carbon to get the value of days between created_at and now
$diff_from_now = Carbon::parse($course->created_at)->diffInDays();
$course_duration = $course->duration;
$remaining_days = $course_duration- $diff_from_now;
Try this one
Select sub(course-duration - DATE(created_at)) as left_days from table_name;
Just define a method in your Course model to calculate remaining days of each course:
class Course extends Model
{
public function remainingDays() {
return $this->course-duration - $this->created_at->diffInDays(Carbon::now());
}
}
And to get remaining days of every course:
$course->remainingDays();
Related
Hi Guys I have a question. I am still learning and am trying to get some date out. Beneath is the table. It has hundreds of lines, but for example:
FormNR
Datum
XX1
XX2
XX3
0001
2022-09-08
4
23
7
0002
2022-09-10
8
5
0
The table name is 'forms'. Now what I need to do is to count XX1+XX2+XX3 (for a year rapport). Then I have a 'date from and to' selection box on my page. So the question would be:
What instanties have been used between a certain date in total but so that you can see a a total per Instantie (each number is a different instantie).
So for example...Between the 1st of January and the 1st of June a list of all XX numbers ( there are 36 ) with there total behind it
What I have is the following. Is works great and shows all XX's in a nice table but for the entire table, not per date. As soon as i want to add the 'between $date_from AND $date_to' it fails.
<?php
$sql_rg_total="SELECT forms.Datum, x.f1,Count(x.f1)
FROM
(SELECT XX1 As F1 FROM forms
UNION ALL
SELECT XX2 As F1 FROM forms
UNION ALL
SELECT XX3 As F1 FROM forms) x
WHERE x.f1 = '$subcat_id'
GROUP BY x.f1";
$resultvv=mysqli_query($conn, $sql_rg_total);
if (mysqli_num_rows($resultvv) > 0) {
while ($rowvv = mysqli_fetch_assoc($resultvv)) {
$subnr = $rowvv['Count(x.f1)'];
echo $subnr;
}
}
?>
By the way $subcat_id is from another table which connects the number to a name.
I have tried to write it as clear as I could. I know it's a bit thought haha. Thanks anyway for any input. Really stuck.
This query should do it:
SELECT SUM(x.c) AS c
FROM (
SELECT ((XX1 = '$subcat_id') + (XX2 = '$subcat_id') + (XX3 = '$subcat_id')) AS c
FROM forms
WHERE Datum BETWEEN '$date_from' AND '$date_to'
) x
The value of a boolean condition is 1 when it's true, 0 when it's false. So XX1 = '$subcat_id' + XX2 = '$subcat_id' + XX3 = '$subcat_id' adds up the number of columns that match in a row, then SUM(c) totals them in the entire table.
You don't need GROUP BY, since it's the same column that you're filtering in the WHERE condition (and now in the SELECT expression). And this moves the date condition into the subquery.
Could Some one help me to get figure this out.To start of this is not a duplicate of CRUD and relation between three tables/Models in Laravel.
There are 2 tables Medication_Patient Pivot Table and Med_Time
Medication_Patient Pivot Table
id medication_id patient_id
1 1 (MED X) 1 (Patient X)
2 2 (MED y) 1 (Patient X)
3 2 (MED y) 2 (Patient Y)
And MEdTime which store time and where the medication was given or not
id med_patient_id(foreign key) Day time given
1 1 Yesterday 0900 1
2 1 Today 0900 0
3 1 Today 2000 0
4 2 Today 0600 1
On the Models I have
class Medication extends Model {
protected $guarded = [];
public function patient()
{
return $this->belongsToMany('App\Patient');
} }
class Patient extends Model
{
public function medication()
{
return $this->belongsToMany('App\Medication');
}
}
To get the Medication's assign to a Patient
$assignedMeds = $patient->medication()->get();
but it doesn't give me the ID of Pivot table which I need to find the Time for medication, So I used (PLEASE LET ME KNOW IF THERE IS A BETTER WAY TO DO THIS)
//get the id from medication_patient pivot Table
$medPatient = DB::table('medication_patient')->select('id')
->Where([
['patient_id','=', $patient->id],
['medication_id' ,'=', $medication->id]
])->get()->first;
$medPatientId = $medPatient->id->id;
//Using Medication_patient_id to find MedTime
$assignedMedTimes = MedTime::where('med_patient_id' ,'=' , $medPatientId)->get();
//Filtering the Med Time according to the day
$yesterdayMedTimes = $assignedMedTimes->where('day', '=', 'yesterday')->all();
$todayMedTimes = $assignedMedTimes->where('day', '=', 'today')->all();
$tomorrowMedTimes = $assignedMedTimes->where('day', '=', 'tomorrow')->all();
return view('medicationPatient.medTime', compact('patient','medication'),[
'assignedMedTimes' => $assignedMedTimes,
'yesterdayMedTimes' => $yesterdayMedTimes,
'todayMedTimes' => $todayMedTimes,
'tomorrowMedTimes' => $tomorrowMedTimes,
]);
}
But this only works when I am getting the Time for 1 Medication (Med X time assigned to Patient X), how do I setup a loop or relation in query or eloquent to get me all the medication time (MED X,Y time for Patient X) and pass it into blade.
Sorry of the long post. Would Appreciate if you could show me the Code.
Thank you
You may be making it a little harder on yourself having to go directly to the table in that query. If it were me, I might consider refactoring the database a little bit to make this easier, and to easily take advantage of the Laravel relationships & pivots.
I'm not sure you need to store the data in two separate tables. I would look to normalize as much as possible and collapse this down to a single table. You don't seem to need to reiterate yourself in the Med_Time table -- the med_patient table adds multiple pill's given, so it serves the same purpose as the med_time table (I think). I suggest just a medication_patient table with the pivots:
id medication_id patient_id Day time given
1 1 (MED X) 1 (Patient X) Yesterday 0900 1
2 2 (MED y) 1 (Patient X) Today 0900 0
Your relations will be much the same as you have them, but you can draw the pivots right from the model. This is from the Patient model, but your relations are good for both in your question
public function medication()
{
return $this->belongsToMany('App\Medication')->withPivot('Day', 'time', 'given');
}
Then, when you need to access the data, just pull the pivot. Example:
$patient->pivot->Day... or $patient->pivot->time
Have you tried something like?
public function patient()
{
return $this->belongsToMany('App\Patient')->withPivot(['id']);
}
I am generating a sales report and I am having a hard time with the query. I am not really good in query. I hope you can help me with this. Ok here it is.
I have a sales table. And the structure is like this
EX:
product_id name date_purchased
1 apple 2014-01-03 12:00:59
2 orange 2014-01-05 10:12:20
3 banana 2014-02-01 09:25:01
4 mango 2014-02-20 18:13:25
5 stawberry 2014-02-28 13:14:30
6 jackfruit 2014-05-26 08:16:31
7 grapes 2014-11-03 09:25:21
8 guava 2014-12-25 10:15:45
Now I want to count the item purchased every month
My output result should be like this
2,3,0,0,1,0,0,0,0,0,1,1
2 represents Jan,
3 represents Feb,
etc...
My query looks like this but I know it is wrong.. :(
SELECT COUNT(product_id) FROM sales_table GROUP BY date_purchased
How can I do that using a query? Or should I make a PHP function for this?
try like this :
SELECT year(date_purchased), month(date_purchased), COUNT(product_id)
FROM sales_table
GROUP BY concat(year(date_purchased), month(date_purchased))
I think you can retrieve the dates and use PHP as it is pretty straight forward.
Once you have a date lets say for example,
$date_str = "2014-01-03 12:00:59"; //let's say you fetch this from DB
$month = date('m',strtotime($date_str)); //this will automatically output 1
You mentioned, 2 represents Jan, 3 represents Feb etc, so you can just add 1 to $month and you have what you need.
Hope it should work
SELECT COUNT(product_id) FROM sales_table GROUP BY DATENAME(month, date_purchased)
The SQL query is enough.
You have to use group by.
But first make a month column for your table.
create function byMonth(#myDate datetime)
returns datetime
as
begin
declare #newDate
set #newDate = year(#myDate) + '/' + month(#myDate) + '/1 00:00:00'
return #newDate
end
go
and use this in your query:
Select Count(*),byMonth(date_puchased) as x
from myTable
where ...
group by x
having ...
this should work for you.
More elaborated solutions
To get Month - wise
SELECT COUNT(id) FROM testrec GROUP BY DATE_FORMAT(pur,'%M');
Get all details
SELECT COUNT(product_id),`name` , date_purchased, DATE_FORMAT(date_purchased,'%M') FROM sales_table GROUP BY DATE_FORMAT(pur,'%M');
Here's my database (free rooms in a hotel, simplified)
rooms_available:
id date_available room_id
================================
1 2013-12-19 2
2 2013-12-20 2
3 2013-12-21 2
4 2013-12-22 2
5 2013-12-23 2
6 2013-12-25 3
rooms:
id name minimal_range
================================
2 Apartment A 5
3 Apartment B 1
I want to query all rooms which are available between 2013-12-20 and 2013-12-22
My query is like:
select *
from rooms_available
where (date='2013-12-20' OR date='2013-12-21' OR date='2013-12-22')
My questions:
is there a more comfortable way? when the date range will be like 2 weeks, the query will also be very long (which will take much longer for querying)
would it be possible to consider minimum ranges - for example: room_id 2 is only available for at least 5 nights (see table "rooms") -> so above query should return no records
Thanks
date >= '2013-12-20' and date <= '2013-12-22'
SELECT * FROM rooms_available WHERE `date_available` BETWEEN "2013-12-20 " AND "2012-03-31"
I didn't test this but it should point you in the right direction especially for the second part of your question about minimal range.
SELECT t1.id as id, t1.date_available as date_available, t1.room_id
FROM rooms_availble as t1 JOIN rooms as t2 on t1.room_id = t2.id
WHERE t1.date_available BETWEEN DATE('2013-12-20') AND DATE('2012-03-31') AND
t2.minimal_range <= datediff('2013-12-22', '2012-12-20');
The mysql datediff function will return the number of days between two dates then you can just compare it with the minimal_range from the rooms join table. Also you might consider binding the start and end dates to variables so that you only have to write each date once.
My Pastebin : http://pastebin.com/PmfDEdEw
Hi,
I am doing a bus search script and here is a table called "bus_notavailable" and it has fields as
nodate_id(PK)
bus_id(FK)
DD(INT 2)
MM(INT 2)
and YYYY(INT 4)
My Question is : Suppose Bus ID is 1 which is not available for these two dates are 15-08-2013 and 26-01-2013 so the record will filtered for this day...
The Nodates Table
Nodate_id bus_id DD MM YYYY
1 1 15 08 2013
2 1 26 01 2013
• so bus_notavailable.DD != '26' its showing me one record
• bus_notavailable.DD != '11' then its showing me 2 records
I know I did mistakes or don't know the solutions therefore want to find out the way to get rid of, and knew this is a issue of one to many relationship where the 'bus_notavailable' has two id of the same bus its showing me two records from 'bus' table.
So Do I need a sub-query to achieve this or is there any other methods?
Your condition here
LEFT JOIN bus_notavailable ON bus.bus_id = bus_notavailable.bus_id
.....
WHERE ......
bus_notavailable.bus_id IS NULL
AND bus_notavailable.DD != '$DD'
AND bus_notavailable.MM != '$MM'
AND bus_notavailable.YYYY != '$YYYY'
Is nonsensical because once bus_id IS NULL, there is no bus_notavailable record from which to compare the DD, MM or YYYY columns.
The condition you want is this (move the DD/MM/YYYY into the LEFT JOIN ON cluse):
LEFT JOIN bus_notavailable ON bus.bus_id = bus_notavailable.bus_id
AND bus_notavailable.DD = '$DD'
AND bus_notavailable.MM = '$MM'
AND bus_notavailable.YYYY = '$YYYY'
.....
WHERE ......
bus_notavailable.bus_id IS NULL
Man)))))) First of all you didn't say what you want to achieve. If you want to select buses that are available on day DD, then you are doing right.
The reason why second row returns 2 rows because as you say you select records where DD not equal to 11. And in this table, both records are suitable. That is why you get 2 rows.
If you want to select only one buss that is available for certain date in case there are many dates in this table you should use distinct keyword.
select distinct bus where dd!=11. You will get one row))
I mean only one row for each bus. So if you have 10 buses available on that date you will have 10 records. One record for each bus.