ErrorException : Undefined offset: 0 - laravel

on my array push i want to concat the 2 data if they have the same date
$goal = Goal::where('employee_id',Auth::user()->employees->first()->id)
->with('accomplishments')->orderBy('date','asc')->get();
$next_week = $goal->whereBetween('date',[$add_start_date,$add_end_date]);
$last_week = $goal->whereBetween('date',[$sub_start_date,$sub_end_date]);
$goals = [];
$date = "";
for ($i=0; $i < count($next_week); $i++) {
if($next_week[$i]['date']==$date){
$goals[$i-1]['activity'] = $goals[$i-1]['activity'] .', '. $next_week[$i]['activity'];
continue;
}
array_push($goals,$next_week[$i]);
$date = $next_week[$i]['date'];
}

when using filtering on laravel collections, indexes are lost,
to re_index the result array, use 'values':
$next_week = $goal->whereBetween('date',[$add_start_date,$add_end_date])->values();
$last_week = $goal->whereBetween('date',[$sub_start_date,$sub_end_date])->values();

Related

I want to store 1 month order data in one the problem is try to change date in day after day but it is storing 1970 date

This is my controller. I want to store 1 month order data in one. The problem is try to change date in day after day but it is storing 1970 date.
public function orderdatastore(Request $request){
$userid = 'GWU458910';
$orderid = "ORD".rand(10,99).rand(25,35).rand(155,255);
$pid = $request->product_id;
// Fetch Product name ny Product id
$product_data = DB::table('products')->where('product_id',$pid)->first();
$product_name = $product_data->product_name;
$selling_price = $product_data->selling_price;
// Fetch user name by user id
$customer_data = DB::table('customer_registrations')->where('customer_id',$userid)->first();
$customer = $product_data->product_name;
$fdate=$request->start_date;
$tdate=$request->end_date;
$datetime1 = strtotime($fdate); // convert to timestamps
$datetime2 = strtotime($tdate); // convert to timestamps
$days = (int)(($datetime2 - $datetime1)/86400);
for ($i=0; $i <= $days ; $i++) {
$order = new Orders;
$order->order_id = $orderid;
$order->product_id = $request->product_id;
$order->mobile = $request->mobile;
$order->user_id = $request->userid;
$order->user_name = $customer;
$order->product_name = $product_name;
$order->product_quantity = $request->quantity;
if($i==0) {
$order->order_date = $fdate;
}else{
$mydate = $fdate;
$daystosum = '1';
$dd = (int)(date('d-m-Y', strtotime($mydate.' + '.$daystosum.' days')));
$d2 = date('Y-m-d', $dd);
$d3 = $d2;
$order->order_date = $d3;
}
$order->product_selling_price = $selling_price;
// $order->volume = $request->volume;
$order->order_start_date = $fdate;
$order->order_end_date = $tdate;
$order->payment_mode = $request->payment_mode;
$order->mobile = $request->mobile;
$order->save();
echo "All data Saved";
}
The problem in your existing code is this line:
$dd = (int)(date('d-m-Y', strtotime($mydate.' + '.$daystosum.' days')));
strtotime() will give you a Unix timestamp;
date('d-m-Y', $timestamp) will give you a string like 2022-07-13;
Casting that string to an int will give you 2022 (and this part is the problem, this makes no sense);
A timestamp represents "seconds since Jan 1, 1970". 2022 seconds after midnight on Jan 1, 1970 is ... Jan 1, 1970, so date('Y-m-d', $dd); will give you 1970-01-01;
Probably the simplest fix is to just change that line to:
$dd = strtotime($mydate.' + '.$daystosum.' days');
But you are using Laravel, and Laravel comes with Carbon, which is extremely useful when working with dates. Here's a version of your code using Carbon instead:
public function orderdatastore(Request $request) {
$userid = 'GWU458910';
$orderid = "ORD" . rand(10, 99) . rand(25, 35) . rand(155, 255);
$pid = $request->product_id;
// Fetch Product name ny Product id
$product_data = DB::table('products')->where('product_id', $pid)->first();
$product_name = $product_data->product_name;
$selling_price = $product_data->selling_price;
// Fetch user name by user id
$customer_data = DB::table('customer_registrations')->where('customer_id', $userid)->first();
$customer = $product_data->product_name;
// Use Carbon to parse your input dates
$fdate = Carbon\Carbon::parse($request->start_date);
$tdate = Carbon\Carbon::parse($request->end_date);
// Use Carbon to find the difference between start/end dates
$days = $tdate->diffInDays($fdate);
// Create a copy of the start date to use for order dates
$mydate = $fdate->copy();
for ($i = 0; $i <= $days; $i++) {
$order = new Orders;
$order->order_id = $orderid;
$order->product_id = $request->product_id;
$order->mobile = $request->mobile;
$order->user_id = $request->userid;
$order->user_name = $customer;
$order->product_name = $product_name;
$order->product_quantity = $request->quantity;
$order->order_date = $mydate;
// Increment order date
$mydate->addDay();
$order->product_selling_price = $selling_price;
// $order->volume = $request->volume;
$order->order_start_date = $fdate;
$order->order_end_date = $tdate;
$order->payment_mode = $request->payment_mode;
$order->mobile = $request->mobile;
$order->save();
echo "All data Saved";
}
}

Save multidimensional array using Laravel and ajax

I am building a pharmacy management system. I have a condition where I need to build a section
As shown in the picture, I need to store the data where the upper three should be same for all of the rows inputted below.
The form data is submitted as in the below picture.
But the data when looped and saved is not being saved as desired. Only the last row of the data is being inserted and I am also confused to store supplier, purchase date and note since these data should be repeated as many as the number of rows are added.
PurchaseController.php
public function storePurchase(PurchaseStoreRequest $request)
{
$purchase = new Purchase();
$count = count($request->name);
// return response()->json($request->all());
for ($i = 0; $i < $count; $i++) {
$purchase->supplier = $request->supplier;
$purchase->purchase_date = $request->purchase_date;
$purchase->description = $request->description;
$purchase->category = $request->category[$i];
$purchase->name = $request->name[$i];
$purchase->generic_name = $request->generic_name[$i];
$purchase->batch_number = $request->batch_number[$i];
$purchase->company = $request->company[$i];
$purchase->strength = $request->strength[$i];
$purchase->expiry_date = $request->expiry_date[$i];
$purchase->quantity = $request->quantity[$i];
$purchase->selling_price = $request->selling_price[$i];
$purchase->purchase_price = $request->purchase_price[$i];
$purchase->save();
}
return response()->json(['message' => 'Purchase Saved Successfully']);
}
Can someone help me to store these three fields in the database repeating the number of rows submitted ?
Currently only the last row is being inserted into the database.
This might be an another way to accomplish what you're looking for.
$sharedKeys = ['supplier', 'purchase_date', 'description'];
$sharedData = $request->only($sharedKeys);
$multiKeys = ['category', 'name', 'generic_name', 'batch_number', 'company', 'strength', 'expiry_date', 'quantity', 'selling_price', 'purchase_price'];
$multiData = $request->only($multiKeys);
for ($i = 0; $i < count($request->name); $i++) {
$individualData = array_combine($multiKeys, array_column($multiData, $i));
Purchase::create($sharedData + $individualData);
}
For every loop, you need to create a new instance, then It will create a new record on each loop :
public function storePurchase(PurchaseStoreRequest $request)
{
// $purchase = new Purchase();
$count = count($request->name);
// return response()->json($request->all());
for ($i = 0; $i < $count; $i++) {
$purchase = new Purchase(); // create new instance end with (); on each loop
$purchase->supplier = $request->supplier;
$purchase->purchase_date = $request->purchase_date;
$purchase->description = $request->description;
$purchase->category = $request->category[$i];
$purchase->name = $request->name[$i];
$purchase->generic_name = $request->generic_name[$i];
$purchase->batch_number = $request->batch_number[$i];
$purchase->company = $request->company[$i];
$purchase->strength = $request->strength[$i];
$purchase->expiry_date = $request->expiry_date[$i];
$purchase->quantity = $request->quantity[$i];
$purchase->selling_price = $request->selling_price[$i];
$purchase->purchase_price = $request->purchase_price[$i];
$purchase->save();
}
return response()->json(['message' => 'Purchase Saved Successfully']);
}

How to pluck a value from foreach statement in Laravel

I am getting an array from database table and through a for each statement. Then check the difference between two values if there is any difference that is less than zero sets a value to 0 for that loop. Then do the same with the next loop.
$bom_id = App\Models\Bom::where('item_id', $item->order_item->id)->pluck('id')->first();
if ($bom_id > 0) {
$bomList = App\Models\Bom_list::where('bom_id', $bom_id)->get();
echo '<div class="row">';
foreach ($bomList as $bomlist) {
$availableQty = $item->order_item->qty;
$requiredQty = $bomlist->qty * $item->qty;
$dif = $availableQty - $requiredQty;
}
$check = '';
$arr = array($bomlist->$dif);
$light = in_array($check, $arr, true) ? 0 : 1;
} else {
$light = 0;
}
enter image description here

doctrine query with parameters having multiple values

I want to make a doctrine query in which each parameters can have multiple values (coming from a select multiple).
I have a table with a 'type' parameter that can have value of 1, 2, 3 or 4 and an 'online' parameter that can be 0 or 1.
My query so far is the following :
$query = $this->createQueryBuilder('properties');
if (array_key_exists('type', $searchValues)) {
$types = $searchValues['type'];
$iterator = 0;
foreach ($types as $type) {
if ($iterator == 0) {
$query->andWhere('properties.idPropertyType = ' . $type);
} else {
$query->orWhere('properties.onlineProperties = ' . $type);
}
$iterator++;
}
}
if (array_key_exists('status', $searchValues)) {
$status = $searchValues['status'];
$iterator = 0;
foreach ($status as $statu) {
if ($iterator == 0) {
$query->andwhere('properties.onlineProperties = ' . $statu);
} else {
$query->andWhere('properties.onlineProperties = ' . $statu);
}
$iterator++;
}
}
$properties = $query->getQuery()->getResult();
In the case of a search with parameter type = 1 and online = 0 and 1, I have results where type is another value than 1. I understand the reason why but I cannot figure out a proper way to make my query.
You don't need to build your query by hand manually, just use the (in) function from the QueryBuilder class. Try this:
$query = $this->createQueryBuilder('properties');
if(array_key_exists('type', $searchValues)){
$types = $searchValues['type'];
$query->andWhere($query->expr()->in('properties.idPropertyType', $types));
}
if(array_key_exists('status', $searchValues)){
$status = $searchValues['status'];
$query->andwhere($query->expr()->in('properties.onlineProperties', $status));
}
$properties = $query->getQuery()->getResult();

How to add laravel pagination with custom array?

I want to add pagination in my custom laravel query.
Here is my code.
$jobseekers = Jobseeker::where('employer_id', Auth::employer()->get()->id)->get()->toArray();
$jobs = Job::select('id')->where('employer_id', (Auth::employer()->get()->id))->get()->toArray();
$apps = DB::table('applications')
->leftJoin('jobseekers', 'applications.jobseeker_id', '=', 'jobseekers.id')
->groupby('jobseekers.id')
->whereIn('applications.job_id', $jobs)
->get();
$apps_array = array();
for ($i = 0; $i < count($apps); $i++) {
$apps_array[$i] = (array) $apps[$i];
}
$jobseeker = array_merge($jobseekers, $apps_array);
return view('frontend.manageCV', compact('jobseeker', 'assignJS'));
Try this and let see how it goes
// I have changed the get to paginate
$jobseekers = Jobseeker::where('employer_id', Auth::employer()->get()->id)->paginate();
$jobs = Job::select('id')->where('employer_id', (Auth::employer()->get()->id))->get()->toArray();
$apps = DB::table('applications')
->leftJoin('jobseekers', 'applications.jobseeker_id', '=', 'jobseekers.id')
->groupby('jobseekers.id')
->whereIn('applications.job_id', $jobs)
->get();
$apps_array = array();
for ($i = 0; $i < count($apps); $i++) {
$apps_array[$i] = (array) $apps[$i];
}
// I am merging to the $jobseekers->data in $jobseekers array
$jobseekers->data = (object)array_merge((array)$jobseekers->data, $apps_array);
return view('frontend.manageCV', compact('jobseekers', 'assignJS'));

Resources