I beg to excuse me for my poor english.
So, I have Laravel 5 ORM, and i need to make request that should add date to some rows, like MySQL DATE_ADD. Input is single date interval and array of id's, output is rows of database, that was changed by adding date interval. Ideally, it should be one ORM request. I know that it is possible to use "bad" way and get all rows, update it in a code, and insert to database, but imho it's not good.
I hope answer will be link to some help site or some code if it's does not complicate you. Thanks for your attention!
public function update($id)
{
$user_id = Auth::user()->id;
$rep_task = RepTask::find($id);
$cl_task = \Request::has('json') ? json_decode(\Request::input('json'),true) : \Request::all();
$ids = [];
$task_id = $rep_task->task_id;
$rep_tasks = RepTask::where('task_id', '=', $task_id)
->select('id')
->get();
$new_date = date_create_from_format(self::DATE_FULLCALENDAR_FORMAT, $cl_task['new_date']);
$selected_task_date = date_create_from_format(self::DATE_MYSQL_FORMAT, $rep_task->start_date);
$diff = date_diff($selected_task_date, $new_date);
if(\Request::has('json'))
{
$ids = [1,2,3]; //this for easy understanding
DB::table('rep_task')
->whereIn('id', $ids)
->update(['start_date' => DB::raw('DATE_ADD(start_date, INTERVAL ' . $diff->d . ' DAY)')]);
$out_json = ['updated' => $ids];
return json_encode($out_json, JSON_UNESCAPED_UNICODE);
}
else
{
$start_date = 0;
$end_date = 0;
if (!isset($cl_task['name']) || !isset($cl_task['text']))
return "{'error':'columns are not defined'}";
if (isset($cl_task['start_date']) && isset($cl_task['end_date']))
{
$dt = date_create_from_format(self::DATE_FULLCALENDAR_FORMAT, $cl_task['start_date']);
$start_date = $dt->format(self::DATE_MYSQL_FORMAT);
$dt = date_create_from_format(self::DATE_FULLCALENDAR_FORMAT,$cl_task['end_date']);
$end_date = $dt->format(self::DATE_MYSQL_FORMAT);
}
$rep_task->name = $cl_task['name'];
$rep_task->text = $cl_task['text'];
$rep_task->start_date = $start_date;
$rep_task->end_date = $end_date;
$rep_task->users_id = $user_id;
$rep_task->save();
}
$user_id = Auth::user()->id;
$tasks = Task::getAllTasksByUserFullcalendar($user_id);
return view(
'task.index',
[
'tasks' => $tasks
]
);
}
Related
I'm trying to create a pagination in my search result. I have one queries in my repository like:
public function getFilteredVehicles($id,$pax,$categories,$search_type,$from_date,$to_date)
{
$vehicles = Vehicle::whereHas('destinations', function($q) use($id){
$q->where('destination_id',$id)
->where('active',1);
})
->paginate(1);
if($search_type == 'disposal' || $search_type == 'pnd')
{
$vehicles = $vehicles->where('registered_location',$id);
}
if($categories)
{
$vehicles = $vehicles->where('vehicle_categoryid',$categories);
}
return $vehicles;
}
The returned result again needs to be processed via loop like:
public function calculateLocationAmount($vehicles,$fdate,$tdate,$destination_id)
{
$datetime1 = new DateTime($fdate);
$datetime2 = new DateTime($tdate);
$interval = $datetime1->diff($datetime2);
$days = $interval->format('%a');
$days = $days+1;
$nights = $days-1;
foreach ($vehicles as $key => $vehicle) {
# code...
$perday_rate = $vehicle->destinations->where('id',$destination_id)->first()->pivot->day_rate;
$pernight_rate = $vehicle->destinations->where('id',$destination_id)->first()->pivot->night_rate;
$day_rate = $perday_rate * $days;
$night_rate = $pernight_rate * $nights;
$total_amount = $day_rate + $night_rate;
$vehicle['total_amount'] = $total_amount;
$vehicle['availability'] = 'true';
if($vehicle->whereHas('unavailability', function($q) use($fdate,$tdate){
$q->whereRaw("? BETWEEN `from_date` AND `to_date`", [$fdate])
->orwhereRaw("? BETWEEN `from_date` AND `to_date`", [$tdate]);
})->count()>0){
$vehicle['availability'] = 'false';
}
}
return $vehicles;
}
This final result needs to be paginated. How can i do it?
Using foreach is changing the value to collection due to which links is not working. If i don't do paginate() or get(), for loop is not executed.
Kindly help.
You can paginate your initial query just as you have, then loop over the pagination object as if it was your regular collection or use $pagination->items()
You should also use nested with('relation') in your initial query to stop N+1 queries https://laravel.com/docs/8.x/eloquent-relationships#constraining-eager-loads
This question is related to my previews one: How to change time zome in different tables in Laravel
After I mark the answer, discover a problem:
When user load the page it need to gives him records for the current day.
For example from 00:00:00 to now.
The problem is that the controller is asking the DB to give records from 00:00:00 in UTC, after this my model(in the link above) parsing it to LOCAL time.
This local time is Europe/Sofia (+3 hours), causing to miss 3 records from today starting with 03:00:00.
Any idea how to fix it?
The controller:
function getdata_chart(Request $request)
{
$start_date = date('d-m-Y 00:00:00');
$end_date = date('d-m-Y 23:59:59');
if($request->start_date != '' && $request->end_date != '')
{
// if user fill dates
$dateScope = array($request->start_date ." 00:00:00", $request->end_date ." 23:59:59");
} else {
// default load page - today
$dateScope = array($start_date, $end_date);
};
$students = MeasCanal::whereBetween('recordtime', $dateScope)
->selectRaw('recordtime')
->selectRaw('max(formattedvalue) filter (where fullname = \'Данни.Кота\') as iazovir')
->selectRaw('max(formattedvalue) filter (where fullname = \'Данни.Температура\') as temperatura350')
->where(function ($query) {
$query->where('fullname', 'like', "Язовир.Данни.Кота")
->orWhere('fullname', 'like', "ГСК_11_350.Данни.Температура");
})
->groupBy('recordtime')
->orderBy('recordtime')
->get();
return response()->json($students);
}
return response()->json($students);
}
The model:
class MeasCanal extends Model
{
protected $connection = 'MeasCanal';
protected $table = 'meas_kanal';
protected $fillable = ['fullname','formattedvalue','recordtime','qualitydesc','statedesc','id'];
/**
* Get the user's recordtime.
*
* #param string $value
* #return string
*/
public function getRecordtimeAttribute($value)
{
return Carbon::parse($value)->timezone('Europe/Sofia')->toDateTimeString();
}
}
You have to map the user input date from the DB timezone to the user timezone by calling the carbon method and array map function. and call the query as it is.
function getdata_chart(Request $request) {
$start_date = date('d-m-Y 00:00:00');
$end_date = date('d-m-Y 23:59:59');
if($request->start_date != '' && $request->end_date != '')
{
// if user fill dates
$dateScope = array($request->start_date ." 00:00:00", $request->end_date ." 23:59:59");
} else {
// default load page - today
$dateScope = array($start_date, $end_date);
};
$dbTimeZone = 'Asia/Kolkata'; // update with your db timezone
$userTimeZone = 'Europe/Sofia';
$dateScope = array_map(function($date) use ($dbTimeZone, $userTimeZone) {
return Carbon::createFromFormat('d-m-Y H:i:s', $date, $dbTimeZone)->setTimezone($userTimeZone);
}, $dateScope);
$students = MeasCanal::whereBetween('recordtime', $dateScope)
->selectRaw('recordtime')
->selectRaw('max(formattedvalue) filter (where fullname = \'Данни.Кота\') as iazovir')
->selectRaw('max(formattedvalue) filter (where fullname = \'Данни.Температура\') as temperatura350')
->where(function ($query) {
$query->where('fullname', 'like', "Язовир.Данни.Кота")
->orWhere('fullname', 'like', "ГСК_11_350.Данни.Температура");
})
->groupBy('recordtime')
->orderBy('recordtime')
->get();
return response()->json($students);
}
return response()->json($students);
}
Note that:- add the following line to the controller if you are not using carbon til yet.
use Carbon\Carbon;
The problem was that date separator was different on each fuction causing Unexpected data found.. After edit him the answer of Sachin Kumar did the job. Thanks for that.
Just discover the problem with similar long way solution:
$start_date0 = date('d-m-Y 00:00:00');
$end_date0 = date('d-m-Y 23:59:59');
$start_date = Carbon::createFromFormat('d-m-Y H:i:s', $start_date0, 'Europe/Sofia')->setTimezone('UTC');
$end_date = Carbon::createFromFormat('d-m-Y H:i:s', $end_date0, 'Europe/Sofia')->setTimezone('UTC');
if($request->start_date != '' && $request->end_date != '')
{
// if user fill dates
$start_date0 = $request->start_date;
$end_date0 = $request->end_date;
$start_date = Carbon::createFromFormat('d/m/Y H:i:s', $start_date0, 'Europe/Sofia')->setTimezone('UTC');
$end_date = Carbon::createFromFormat('d/m/Y H:i:s', $end_date0, 'Europe/Sofia')->setTimezone('UTC');
$dateScope = array($start_date, $end_date);
} else {
// default load page - today
$dateScope = array($start_date, $end_date);
};
I am trying to create an invoice number like - HCL/LF/02/2018 in where the number will be incremented by one for the new invoice. Would someone help me please to get expected invoice_no? I tried something like this in my controller -
public function create()
{
$check = OrderProformaInvoice::orderBy('created_at', 'DESC')->get();
$number = 01;
$year = date('Y');
if (count($check) > 0) {
$invoiceNo = OrderProformaInvoice::latest()->first(['invoice_no']);
$arr = array('HCL','LF', $invoiceNo + 1, $year);
$newInvoiceNo = implode("/",$arr);
}else{
$arr = array('HCL','LF', $number, $year);
$newInvoiceNo = implode("/",$arr);
}
return view('admin.marchendaising.order-proforma-invoices.create', compact('newInvoiceNo'));
}
And in my view form input field is-
<input type="text" name="invoice_no" class="form-control" value="{{ $newInvoiceNo }}">
You have mistake in this line:
$invoiceNo = OrderProformaInvoice::latest()->first(['invoice_no']);
Correct:
$invoiceNo = OrderProformaInvoice::latest()->get()->invoice_no;
You made a mistake here:
$invoiceNo = OrderProformaInvoice::latest()->first(['invoice_no']);
There was no need for this line since you already have a count of all of the OrderProformaInvoice so we may aswell set this to a variable and just increase it for a new one rather than making another call to the database.
If you ever were to delete a database record, you could use
$invoice = OrderProfromaInvoice::orderBy('created_at', 'desc')->first();
Then instead of $numOfInvoices++ you could do $invoice->id++ which will take the ID of the latest invoice and increment it by one.
public function create()
{
$check = OrderProformaInvoice::orderBy('created_at', 'DESC')->get();
$year = date('Y');
$numOfInvoices = count($check)
$arr = array('HCL','LF', $numOfInvoices++, $year);
$newInvoiceNo = implode("/",$arr);
// You could also just do this
$newInvoiceNo = 'HCL/LF/' . $numOfInvoices++ . '/' . $year;
// $newInvoiceNo will equal example: 'HCL/LF/12/2018';
return view('admin.marchendaising.order-proforma-invoices.create', compact('newInvoiceNo'));
}
I have a method that can receive a parameter of "month" and another of "year", you can receive both or only one of them.
In the case of only receiving the month I want to make a query that I look for in a field that is called "created_at" just looking for the month of that field date
At the moment I use this code but when doing a like it does not do it correctly
else if ($request->has('month')) {
$month = $request->input('month');
$currentTimestamp = date_create_from_format('n', $month)->getTimestamp();
$currentDate = date('m', $currentTimestamp);
$filters['updated_at'] = $currentDate;
unset($filters['month']);
}
$cars = Car::where(function($query) use($filters) {
foreach ($filters as $column => $value) {
if ($column === 'updated_at') {
$query->where($column, 'LIKE', '%'.$value.'%');
continue;
}
$query->where($column, 'LIKE', '%'.$value.'%');
}
})->orderBy('updated_at', 'desc')->get();
You should try this:
if ($request->has('month')) {
$month = $request->input('month');
Car::whereRaw('MONTH(created_at) = '.$month)->get();
}
You can use whereMonth method, like this:
$cars = Car::whereMonth('created_at', '12')->get();
Example with determining if a month value is exist:
if ($request->has('month')) {
$cars = Car::whereMonth('created_at', $request->input('month'))->get();
}
You can use laravel queryBuilder .
The idea is pretty simple. Build your query and execute it only when needed
carQuery = Car::newQuery();
if ($request->has('month') {
carQuery->where('created_at' .. do camparison you want);
}
if ( $request->has('year') {
// add extra query filter
}
cars = carQuery->get();
In my database I have many products that have a datecreated attribute.
I want to delete any product after an amount of time, like 10 or 20 days, when I refresh my home page.
This is my controller:
public function indexAction()
{
$datenow = new \DateTimeImmutable();
$datenowStringType = $datenow->format('Y-m-d');
$em = $this->getDoctrine()->getManager();
$products = $em->getRepository('ProductBundle:Product')->findAll();
foreach ($products as $produit) {
$CreatedDate = $produit->getCreatedDate();
$CreatedDate_StringType = $CreatedDate->format('Y-m-d');
$duree = '+'.$produit->getDuree();
$date_fin_duree = date('Y-m-d', strtotime($CreatedDate_StringType.$duree));
if ( strtotime($datenowStringType)<strtotime($date_fin_duree) ) {
$em->remove($produit);
}
}
array_values($products);
return $this->render('product/index.html.twig', array(
'products' => $products,
));
}
There are no errors returned, but the product isn't deleted. It could be a simple error but I searched a lot and I think it's a lack of concentration.
Please help?
$date = ''; // calculate your date here, it should be DateTime object
$qb = $em->createQueryBuilder();
$qb->delete('Product', 'p');
$qb->where('p.duree < :date');
$qb->setParameter('date', $date);