I want to save data for all user that have certain group_id in new table.
For example, i want to save new data to all user who have group_id = 1, but based on my code it only save user_id = 2. It should save user_id = 1 too.
$group = Group::find($req->groupID);
foreach($group->creators as $creator)
$attend = new Attendance;
$attend->groupID = $req->groupID;
$attend->meetingID = $data->id;
$attend->userFeedback = NULL;
$attend->userAttendance = 'Not Attend';
$attend->userID = $creator->id;
$attend->save();
You should run your loop properly. Follow the below code
$group = Group::find($req->groupID);
foreach($group->creators as $creator)
{
$attend = new Attendance();
$attend->groupID = $req->groupID;
$attend->meetingID = $data->id;
$attend->userFeedback = NULL;
$attend->userAttendance = 'Not Attend';
$attend->userID = $creator->id;
$attend->save();
}
Related
Morning all,
I am trying to create a button that will copy the organisation address and populate the organisation billing address fields. I have no idea where to start.
The fields in question are:
// I want to copy these values
$organisation->email = Input::get('email');
$organisation->line_1 = Input::get('line_1');
$organisation->line_2 = Input::get('line_2');
$organisation->line_3 = Input::get('line_3');
$organisation->city = Input::get('city');
$organisation->state = Input::get('state');
$organisation->postcode = Input::get('postcode');
$organisation->country = Input::get('country');
// To these fields
$organisation->billing_line_1 = Input::get('billing_line_1');
$organisation->billing_line_2 = Input::get('billing_line_2');
$organisation->billing_line_3 = Input::get('billing_line_3');
$organisation->billing_city = Input::get('billing_city');
$organisation->billing_state = Input::get('billing_state');
$organisation->billing_postcode = Input::get('billing_postcode');
$organisation->billing_country = Input::get('billing_country');
Here is the start of my function
// Copy Address Button
public function onCopyAddress()
{
$organisation = Organisation::find($this->param('id'));
// Copy address logic here
$organisation->save();
Flash::success($organisation->name." Address has been successfully copied.");
return Redirect::to('/organisations/'.$organisation->id);
}
This is the solution I came up with.
public function onCopyAddress()
{
$organisation = Organisation::find($this->param('id'));
$organisation->billing_line_1 = Input::get('line_1');
$organisation->billing_line_2 = Input::get('line_2');
$organisation->billing_line_3 = Input::get('line_3');
$organisation->billing_city = Input::get('city');
$organisation->billing_state = Input::get('state');
$organisation->billing_postcode = Input::get('postcode');
$organisation->billing_country = Input::get('country');
$organisation->save();
Flash::success($organisation->name." Address has been successfully copied.");
return Redirect::to('/organisations/'.$organisation->id);
}
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']);
}
I want to save the ID of a new Sale(); in $sale->voucher_series, how can I achieve this?
$sale = new Sale();
$sale->customerid = $request->customerid;
$sale->userid = \Auth::user()->id;
$sale->proof_type = $request->proof_type;
$sale->voucher_series = $request->saleId;
$sale->date_time = $mytime->toDateString();
$sale->total = $request->total;
$sale->status = 'Registered';
$sale->save();
If your voucher_series column is nullable field then it will work.
$sale = new Sale ();
$sale->customerid = $request->customerid;
$sale->userid = \Auth :: user()->id;
$sale->proof_type = $request->proof_type;
$sale->date_time = $mytime->toDateString ();
$sale->total = $request->total;
$sale->status = 'Registered';
$sale->save();
$sale->voucher_series = $sale->id;
I am new to the laravel and trying to store the primary key values of patientid to the foreign key of patient_id in address table
$patdet = $request->patdet;
foreach ($patdet as $patdets) {
$pdet = new Patient();
$pdet->fname = $patdets['fname'];
$pdet->mname = $patdets['mname'];
$pdet->lname = $patdets['lname'];
$pdet->age = $patdets['age'];
$pdet->blood_group = $patdets['bloodgroup'];
$pdet->gender = $patdets['gender'];
$ptid = $pdet->save();
}
$addr = $request->address[0];
$address = new Address;
$address->gps_lat = $addr['gps_lat'];
$address->gps_log = $addr['gps_long'];
$address->house_no = $addr['houseno'];
$address->zipcode = $addr['zip_code'];
$address->street = $addr['street'];
$address->chowk = $addr['chowk'];
$address->city = $addr['city'];
$address->patient_id = $patid->id;
$address->save();
I know that $ptid is a local variable and cannot be used in address table, so how can I store it in address table?
Try the following and ask me if it doesn't work
foreach($request->patdet as $key => $patdets)
{
$pdet = new Patient();
$pdet->fname = $patdets['fname'];
$pdet->mname = $patdets['mname'];
$pdet->lname = $patdets['lname'];
$pdet->age = $patdets['age'];
$pdet->blood_group = $patdets['bloodgroup'];
$pdet->gender = $patdets['gender'];
$pdet->save();
$addr = $request->address[$key];
if($pdet->id > 0 && !empty($addr)){
$addrss = new Address;
$addrss->gps_lat = $addr['gps_lat'];
$addrss->gps_log = $addr['gps_long'];
$addrss->house_no = $addr['houseno'];
$addrss->zipcode = $addr['zip_code'];
$addrss->street = $addr['street'];
$addrss->chowk = $addr['chowk'];
$addrss->city = $addr['city'];
$addrss->patient_id = $pdet->id;
$addrss->save();
}
}
Try the following
$patArray = $request->patdet;
$addressArray = $request->address;
$pId = array();
foreach($patArray as $key => $patdets){
$pdet = new Patient();
$pdet->fname = $patdets['fname'];
$pdet->mname = $patdets['mname'];
$pdet->lname = $patdets['lname'];
$pdet->age = $patdets['age'];
$pdet->blood_group = $patdets['bloodgroup'];
$pdet->gender = $patdets['gender'];
$pdet->save();
$pId[$key] = ($pdet->id > 0) ? $pdet->id : 0;
}
foreach($addressArray as $key => $addr){
$addrss = new Address;
$addrss->gps_lat = $addr['gps_lat'];
$addrss->gps_log = $addr['gps_long'];
$addrss->house_no = $addr['houseno'];
$addrss->zipcode = $addr['zip_code'];
$addrss->street = $addr['street'];
$addrss->chowk = $addr['chowk'];
$addrss->city = $addr['city'];
$addrss->patient_id = array_key_exists($key ,$pId) ? $pId[$key] : 0;
$addrss->save();
$i++;
}
I need to pull any amount of records that correspond to a specific value (CourseCode), and insert these records into another table. This code works fine as long as the Linq code returns only one record, however if there is any more than that I get the following message:
An object with the same key already exists in the ObjectStateManager.
The existing object is in the Modified state. An object can only be
added to the ObjectStateManager again if it is in the added.
Below is my code:
if (_db == null) _db = new AgentResourcesEntities();
var prodCodes = from records in _db.CourseToProduct
where records.CourseCode == course.CourseCode
select records;
foreach (var pt in prodCodes.ToList())
{
agentProdTraining.SymNumber = symNumber;
agentProdTraining.CourseCode = course.CourseCode;
agentProdTraining.ProductCode = pt.ProductCode;
agentProdTraining.DateTaken = course.DateTaken;
agentProdTraining.Method = course.Method;
agentProdTraining.LastChangeOperator = requestor;
agentProdTraining.LastChangeDate = DateTime.Now;
agentProdTraining.DateExpired = course.ExpirationDate;
agentProdTraining.ProductCode = pt.ProductCode;
agentProdTraining.NoteId = pt.NoteId;
_db.AgentProductTraining.AddObject(agentProdTraining);
_db.SaveChanges();
PtAdded++;
EventLog.WriteEntry(sSource, "Product Training added", EventLogEntryType.Warning);
}
The loop is re-adding the same agentProdTraining object even though property values are changed. Create a new instance for each loop execution.
foreach (var pt in prodCodes.ToList())
{
var agentProdTraining = new AgentProductTraining();
agentProdTraining.SymNumber = symNumber;
agentProdTraining.CourseCode = course.CourseCode;
agentProdTraining.ProductCode = pt.ProductCode;
agentProdTraining.DateTaken = course.DateTaken;
agentProdTraining.Method = course.Method;
agentProdTraining.LastChangeOperator = requestor;
agentProdTraining.LastChangeDate = DateTime.Now;
agentProdTraining.DateExpired = course.ExpirationDate;
agentProdTraining.ProductCode = pt.ProductCode;
agentProdTraining.NoteId = pt.NoteId;
_db.AgentProductTraining.AddObject(agentProdTraining);
_db.SaveChanges();
PtAdded++;
EventLog.WriteEntry(sSource, "Product Training added", EventLogEntryType.Warning);
}