how call the itemsCart->id_product? - laravel

how can i call the id_product of the $itemsCart result line to be inserted in $ encomendas_detalhes-> id_produto?
where are the '----' is where I need help more
$itemsCart = cart::where('token_user', session()->get('_token') )->where('id_encomenda', 0)->get();
foreach ( $itemsCart as $items ) {
$encomendas_detalhes = new detalhes;
$encomendas_detalhes->id_encomenda = $encomendas->id;
$encomendas_detalhes->id_produto = ---$itemsCart->id_produto--- ;
$encomendas_detalhes->quantidade = 2;//$itemsCart->quantidade;
$encomendas_detalhes->total = $total;
$encomendas_detalhes->save();
$items->id_encomenda = $id_encomenda;
$items->save();
}

Related

how to store the primary key values to the foreign key at same time of save

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++;
}

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 create a product in Prestashop 1.7

I create my first module in Prestashop 1.7.4. This is my code:
public function createProductsObject()
{
$product = new Product;
$product->name = $productName;
$product->ean13 = '';
$product->reference = '';
$product->id_category_default = $getCategoryID;
$product->category = $getCategoryID;
$product->indexed = 1;
$product->description = $description;
$product->condition = 'new';
$product->redirect_type = '404';
$product->visibility = 'both';
$product->id_supplier = 1;
$product->link_rewrite = $link_rewrite;
$product->quantity = $singleStock;
$product->price = $price;
$product->active = 1;
$product->psoft_hurtobergamo_id = $productID;
$product->add();
$product->addToCategories($getCategoryID);
This function in not complite. But this is not important right now. Variable productName hase this value:
Array ( [0] => Test [1] => Test) )
Beacause I have two language. The problem is. Why I dont have the name after the product is create ?
thanks for help.
To resolve this problem I add new index to my array.
$productName = array('0' => '');
$link_rewrite = array('0' => '');
and then add values to array start from index 1. Because the first language has identity value set to 1. The second, 2.
for ($i = 1; $i <= $getNumberOfAvailableLanguage; $i++)
{
array_push($productName, $name);
array_push($link_rewrite, $clean1);
}

how to count all the array of data in controler

I want to count the array of data got by using postgresql using codeigniter.I used foreach loop and used count($row['zero']) to count but it results only 1.please guide me.
Here is my controller
public function indicators($pcode = '', $dcode = '') {
$this->data['p_code'] = $pcode;
$this->data['d_code'] = $dcode;
$this->data['pcode'] = $this->employment_model->get_project();
$this->data['attend_household'] = $this->employment_model->household_absent_all($dcode);
var_dump($this->data['attend_household']);
die();
foreach($this->data['attend_household'] as $row){
if($row['zero']>0){
$this->data['zerroo']=count($row['zero']);
}
}
if (count($this->data['attend_household']) > 0) {
$this->data['attend_household'] = $this->data['attend_household'][0];
}
// var_dump($this->data);
$this->load->view("employment_info", $this->data);
}
Here is my model
function household_absent_all($dcode) {
$dcode_query = "";
if ($dcode != '') {
if ($dcode == 'all') {
$dcode_query = "";
} else {
$dcode_query = "where t.dcode='$dcode'";
}
}
$sql = "select (100-(sum(days_worked)/sum(days_offered))*100) as zero,regno,sum(days_offered) as days_offered,sum(days_worked) as days_worked
from(
select*
from assets.tblprojects a
join(
select pcode as project,hh_jobcard_no
from employment.work_group_member
)wg on a.pcode=wg.project
join(
select regno,at.days_offered,at.days_worked
from employment.registration_hh_member
join(
select pcode as a_pcode,hh_jobcard_no as jobcard,mem_id,
sum((shift_1*0.5)+(shift_2*0.5)) as days_worked,count(adate) as days_offered
from employment.attendence
group by a_pcode,jobcard,mem_id
)at on regno=at.jobcard and s_no=at.mem_id
)rhm on wg.hh_jobcard_no=rhm.regno
)t
group by regno
$dcode_query
";
$result = $this->db->query($sql)->result_array();
return $result;
}
try like this
$i=0;
foreach($this->data['attend_household'] as $row){
if($row['zero']>0){
$i++;
}
}
$this->data['zerroo']=$i;
how about using the num_rows() as your counter.
if($num = $result->num_rows() >=1){
$result = array('result_set'=>$result->result_array(),'result_count'=>$num);
}else{
$result = array('result_set'=>FALSE,'result_count'=>FALSE);
}
return $result;
then on your controller you can use it as $variable['result_count'] for count and $variable['result_set'] for the data results.

Resources