App For Rental Car :
I need check car if available between 2 date and check quantity ("column name : qty"),
There is no need for a car more than the available number of cars (number cars booking < QTY car )
When change number 3 in the name of the column ('qty') does not work.
Query From Url : $PickupStartDate,$DropEndDate;
Car::withCount('reservation')->having('reservation_count', '<', 3)->get();
Tables :
Cars
ID | name | price | qty|
Reservations
ID | start_date | end_date | days | start_place | return_place | car_id |
check if car available for rental check we have a car between date
$start_date = $PickupStartDate;
$end_date = $DropEndDate;
$cars = Car::with(['reservations' => function($query) use ($start_date, $end_date) {
$query->where('start_date', '<=', $end_date)
->where('end_date', '>=', $start_date);
}])->having(3, '<', 'reservations_count')->get();
Try this
Solution :
$start_date= $filters['StartDate'];
$end_date= $filters['EndDate'];
$query->withCount(['reservation' => function($query) use ($start_date, $end_date) {
$query->where('start_date', '<=', $start_date)
->where('end_date', '>=', $end_date)
->orWhere('end_date', '>', $start_date);
}])
in code Check
Car.qty< car.reservation_count
Related
0, I have this code that works perfectly
$data = DB::table('incidencias')
->join('clientes_incid', 'clientes_incid.rif', '=', 'incidencias.rif')
->select(DB::raw('count(*) as gold, cat_cliente'))->where('cat_cliente', '=','Gold')
->groupBy('cat_cliente')
->get();
I want to make another select but with another condition, I tried with this but doesn't work
$data = DB::table('incidencias')
->join('clientes_incid', 'clientes_incid.rif', '=', 'incidencias.rif')
->select(DB::raw('count(*) as gold, cat_cliente'))->where('cat_cliente', '=','Gold')
->select(DB::raw('count(*) as silver, cat_cliente'))->where('cat_cliente', '=','Silver')
->groupBy('cat_cliente')
->get();
Can somebody help me?
Group By cat_cliente, this will count gold, count silver..., in each records.
DB::table('incidencias')
->join('clientes_incid', 'clientes_incid.rif', '=', 'incidencias.rif')
->select(DB::raw('count(incidencias.id) as count'), cat_cliente)
->whereIn('cat_cliente', ['Gold', 'Silver'])
->groupBy('cat_cliente')
->get();
output =>
-----------------
count | cat_cliente
------------------
10 | Gold
------------------
3 | Siliver
-----------------
You want to put them in one record, I think you can get it out and display it by loop.
However, if you really want to get it in query, try this:
$gold_query = DB::table('incidencias')
->join('clientes_incid', 'clientes_incid.rif', '=', 'incidencias.rif')
->where('cat_cliente', 'Gold')
->selectRaw('COUNT(incidencias.id) AS gold', '0 AS silver');
$silver_query = DB::table('incidencias')
->join('clientes_incid', 'clientes_incid.rif', '=', 'incidencias.rif')
->where('cat_cliente', 'Silver')
->selectRaw('0 AS gold, count(incidencias.id) AS silver');
$gold_silver = $gold_query->unionAll($silver_query);
DB::table(DB::raw("({$gold_silver->toSql()}) AS gs"))
->mergeBindings($gold_silver)
->selectRaw('SUM(gold) AS gold, SUM(silver) AS silver')
->get()
Output =>
----------------
gold | silver |
-----------------
10 | 3 |
----------------
I'm trying to select from my SQLite table all the rows in selected months and sum the value in "total" column.
Here is my example table:
| ID | DATE | TOTAL |
| 1 | 22-11-2017 | 700 |
| 2 | 26-11-2017 | 100 |
| 3 | 28-11-2017 | 150 |
| 4 | 30-11-2017 | 50 |
Here is the dql function
public function getDataByMonth($repo, $year, $month)
{
date = new \DateTime("{$year}-{$month}-01 00:00");
$dateEnd = new \DateTime("{$year}-{$month}-31 23:59");
$query = $qb->where('b.date BETWEEN :start AND :end')
->setParameter('start', $date->format('d-m-Y H:i'))
->setParameter('end', $dateEnd->format('d-m-Y H:i'))
->select('SUM(b.total) as totals');
}
Even if the query goes, when I set $monthvariable to October (so 10) the query return 1000instead of NULL.
I'm calling the function using
$earnings = $this->getDoctrine()->getManager()->getRepository(Invoices::class);
$this->getDataByMonth($earnings,date("Y"), strftime("%m", strtotime("-1 months")));
What I'm doing wrong?
I need to select the right rows relating to the selected month.
maybe you should try sth like this, had similar problem and it worked:
->add('where', $qb->expr()->between(
'e.datetimefield',
':from',
':to'
))
it's database software independent, should work with most databases, more info in doctrine documentation:
http://docs.doctrine-project.org/en/latest/reference/query-builder.html#the-expr-class
and also don't format datetime object, ex:
$from = new \DateTime();
->setParameter('from', $from);
SQLite does not have a storage class set aside for storing dates
and/or times. Instead, the built-in Date And Time Functions of SQLite
are capable of storing dates and times as TEXT, REAL, or INTEGER
values
--https://sqlite.org/datatype3.html
So I don't think you can use BETWEEN.
I had similar issue with SQLite I ended up using timestamp
Symfony 3 ?
$date = new \DateTime("{$year}-{$month}-01 00:00");
$dateEnd = new \DateTime("{$year}-{$month}-31 23:59");
$em = $this->getDoctrine()->getManager();
$query = $em->createQuery("SELECT SUM(p.total) FROM AppBundle:Invoices p WHERE p.date >= :dateFirstDay and p.date <= :dateLastDay")
->setParameter('dateFirstDay',$date->format('d-m-Y H:i'))
->setParameter('dateLastDay',$dateEnd->format('d-m-Y H:i'));
I have a table
+---------------------+-----------
| id | start_date | End_date |
+---------------------+-----------+
| 1 | 2017-07-07 | 2017-07-31|
| 2 | 2017-08-01 | 2017-08-31|
| 3 | 2017-09-01 | 2017-09-10|
|
+------+--------------+-----------+
And I want to select dates between two dates.I have a query
SELECT * FROM Financial_Year WHERE CURDATE() between `start_date` and `End_date`
I want to convert this query to laravel so i tried this
$dt = Carbon::now();
$getmonths= DB::table('Financial_Year')
->whereBetween($dt, ['start_date', 'End_date'])->get();
But i didn't get output.Any help would be appreciated.
Here you can use laravel whereRaw() to achieve this.
Just like this
$dt = Carbon::now();
$getmonths= DB::table('Financial_Year')
->whereRaw('"'.$dt.'" between `start_date` and `End_date`')
->get();
You can use Carbon native function for this:
http://carbon.nesbot.com/docs/
$first = Carbon::create(2012, 9, 5, 1);
$second = Carbon::create(2012, 9, 5, 5);
var_dump(Carbon::create(2012, 9, 5, 3)->between($first, $second)); // bool(true)
var_dump(Carbon::create(2012, 9, 5, 5)->between($first, $second)); // bool(true)
If you want to use only Laravel Query Builder maybe you could try:
$date_start = Carbon::now()->startOfYear();
$date_end = Carbon::now()->endOfYear();
$query = Model::where('created_at', '<=', $date_end)
->where('created_at', '>=', $date_start)
->get();
In the variables of date_start and date_end you will put the range you want to use. Maybe is during the same month, during the same year or you could use:
$date = Carbon::create(2012, 9, 5, 5);
to determine the date manually.
Here is my table structure. and I want to get records between provided date range.
|--------------|---------------------|
| id | year | month |
|--------------|---------------------|
| 1 | 2015 | 01 |
| 2 | 2015 | 02 |
| 3 | 2016 | 02 |
|--------------|---------------------|
I tried with following query, but didn't work.
return $this->db->select('*')
->from('tourist T')
->where('T.year >=', $where['s_year'])
->where('T.month >=', $where['s_month'])
->where('T.year <=', $where['e_year'])
->where('T.month <=', $where['e_month'])
->get()->result();
Try This:
$this->db->select('*')
->from('test T')
->where('T.year >='.$where['s_year'].' and T.month >='.$where['s_month'])
->where('T.year <='.$where['s_year'].' and T.month <='.$where['s_month'])
->get()->result();
So I have table prices with structure:
+----+--------+
| id | reward |
+----+--------+
| 1 | 721 |
+----+--------+
| 2 | 54 |
+----+--------+
| 3 | 99 |
+----+--------+
and I'm using this method to sum all rewards:
'withdrawals' => \App\Tradeoffer::where('type', 'withdraw')
->where('completed', 1)
->where('declined', 0)
->where('timeout', 0)
->where('created_at', '>', (time() - $hours))
->sum('reward')
and the response is: 7215499 instead of sum of all entries. Why is that? How to deal with it?
I think you can do it like,
$totalReward = \App\Tradeoffer::selectRaw('SUM(reward) as total')
->where('type', 'withdraw')
->where('completed', 1)
->where('declined', 0)
->where('timeout', 0)
->where('created_at', '>', (time() - $hours))
->first();
$totalReward->total;
Instead of use the model I use a Query Builder and it works (with my database)
DB::table('Tradeoffer') // Here is the name of the table
->where('type', 'withdraw')
->where('completed', 1)
->where('declined', 0)
->where('timeout', 0)
->where('created_at', '>', (time() - $hours))
->sum('reward');
Try it!
What you should do is making sure your reward column in prices table in database is number (integer/float) and not varchar