How can I insert data from get() in laravel? - php

I want to insert data from get() method in L_Examen_Categorie table but it says :
Cannot insert explicit value for identity column in table
'L_Examen_Categorie' when IDENTITY_INSERT is set to OFF. (SQL: insert
into [L_Examen_Categorie] ([id], [code], [libelle], [coefficient],
[Note], [id_examen]) values (5, 777, 777, 3, 12, 1))"
Here is it my code
$categorie = DB::table('P_Examen_Categorie as p')
->where('p.id_examen', '=', $request->input('id_examen'))
->get();
foreach($categorie as $cat_item)
{
L_Examen_Categorie::insert((array)$cat_item);;
}
Categorie reponse:
[{id: "5", code: "777", libelle: "777", coefficient: "3", Note: "12", id_examen: "1"},…]
0: {id: "5", code: "777", libelle: "777", coefficient: "3", Note: "12", id_examen: "1"}
1: {id: "7", code: "39", libelle: "39", coefficient: "3", Note: "12", id_examen: "1"}
2: {id: "9", code: "777", libelle: "39", coefficient: "3", Note: "12", id_examen: "1"}
3: {id: "10", code: "777", libelle: "777", coefficient: "1", Note: "1211", id_examen: "1"}
4: {id: "11", code: "777", libelle: "777", coefficient: "3", Note: "12", id_examen: "1"}

You need to use the "toArray()" method. This method converts your collection object into an array and then inserts data.
$categorie = DB::table('P_Examen_Categorie as p')
->where('p.id_examen', '=', $request->input('id_examen'))
->get()->toArray();
L_Examen_Categorie::insert( $categorie);
But make sure the same column is available in the L_Examen_Categorie tables

Convert objects to associative array before inserting.
$categorie = json_decode($categorie, true);
L_Examen_Categorie::insert($categorie);

Related

How Update a data from an object of json type column in Laravel?

I have an object of json type column in my table (properties) in MySQL like:
[
{
"unit": "2",
"floor": "1",
"price": "6000000",
"toilet": "2",
"balcony": "2",
"bedrooms": "2",
"customer": "3",
"bathrooms": "3",
"flat_name": "1A",
"flat_size": "1200",
"floor_plan": "217",
"price_per_sqft": "5000"
},
{
"unit": "2",
"floor": "1",
"price": "5000000",
"toilet": "2",
"balcony": "2",
"bedrooms": "2",
"customer": null,
"bathrooms": "3",
"flat_name": "1B",
"flat_size": "1200",
"floor_plan": "215",
"price_per_sqft": "5000"
},
{
"unit": "1",
"floor": "2",
"price": "6000000",
"toilet": "2",
"balcony": "2",
"bedrooms": "2",
"customer": null,
"bathrooms": "3",
"flat_name": "2A",
"flat_size": "1250",
"floor_plan": "216",
"price_per_sqft": "5300"
}
]
How can I update customer id, where flat_name = 1B from this object in Laravel?
I have made the solution with using loop. Thank you who reply and answer
$objectitem=Property::where("id", 1)->first();
$updatedFlatDetails= $objectitem->flat_details;
foreach ($objectitem->flat_details as $key => $singleflat){
if($singleflat['flat_name']==$item->flat_name){
$updatedFlatDetails[$key]['customer']=(string)$item->customer_id;
}
}
$objectitem->flat_details = $updatedFlatDetails;
$objectitem->save();
Use collection after fetch data from database :
$target= $collection->filter(function ($item) {
return $item->flat_name=="1B";
})->values();
or filter before fetch data use eloquent :
Model::where('flat_name','1B')->get();

I used laravel groupBy, but i am trying to sort results by where have many grouped items

I am trying to sort where grouped product_id which has many orders comes first:
Here are my codes:
$orderProducts = OrderProduct::where("seller_id", auth()->user()->id)->latest()->get()
->groupBy(function ($val){
return $val->product_id;
});
Result is like:
` {
"1": [
{
"id": 8,
"order_id": "5",
"product_id": "1",
}],
"2": [
{
"id": 9,
"order_id": "5",
"product_id": "2",
},
{
"id": 10,
"order_id": "5",
"product_id": "2",
}
]}
`
And i want it sorted like: `
{
"2": [
{
"id": 9,
"order_id": "5",
"product_id": "2",
},
{
"id": 10,
"order_id": "5",
"product_id": "2",
}
],
"1": [
{
"id": 8,
"order_id": "5",
"product_id": "1",
}],
}
`
where product_id of 2 with many orders comes first and others are ordered according to orders they have. Thanks!
Since you already have a collection, you can simply call the sortByDesc() method on the collection. Ref: https://laravel.com/docs/7.x/collections#method-sortbydesc
$orderProducts = OrderProduct::where("seller_id", auth()->user()->id)
->latest()
->get()
->groupBy(function ($val){
return $val->product_id;
})->sortByDesc(function($object) {
return $object->count();
});
As you can see, I'm returning the count of the object's items array using the Laravel count() method.

Want to get a result like below. how to write a query for that

{"result": [
{
"room_id": "1",
"floor_id": "1",
"flat_id": "1",
"room_name": "Room1",
"no_of_cots": "4",
"cot_info":[{"cot_id": "1",
"cot_name": "COT1",
"cot_status": "1"},
{"cot_id": "2",
"cot_name": "COT2",
"cot_status": "1"}],
"flat_name": "Sumangali PG",
"floor_name": "GroundFloor"
},
{
"room_id": "2",
"floor_id": "1",
"flat_id": "1",
"room_name": "Room2",
"no_of_cots": "2",
"cot_info":[{"cot_id": "1",
"cot_name": "COT1",
"cot_status": "1"},
{"cot_id": "2",
"cot_name": "COT2",
"cot_status": "1"}],
"flat_name": "Sumangali PG",
"floor_name": "GroundFloor"
} ]
}
OK you can use two page one for proccess the query example should be like that then you can addit to an array then decode the array and make echo like in the example bellew:
//page source
$query ='select room_id,floor_id,flat_id,room_name,no_of_cots,cot_info,cot_id,cot_name,cot_status,flat_name,floor_name
from table1 ,table2,table3,table4 where table1.id=table2.id and table2.id=table3.id and table4.id=table3.id';
$result = mysqli_query($conn,$query);
$array_list=array();
while($row = mysqli_fetch_array($result))
{
$array_list['room_id']=$row;
$array_list['floor_id']=$row;
$array_list['flat_id']=$row;
$array_list['room_name']=$row;
$array_list['cot_info']=$row;
$array_list['cot_name']=$row;
$array_list['cot_id']=$row;
$array_list['cot_status']=$row;
$array_list['flat_name']=$row;
$array_list['floor_name']=$row;
}
$json_data = array(
"result" => $array_list,
);
echo json_encode($json_data);
// page 2
$content='{"result": [ { "room_id": "1", "floor_id": "1", "flat_id": "1", "room_name": "Room1", "no_of_cots": "4", "cot_info":[{"cot_id": "1", "cot_name": "COT1", "cot_status": "1"}, {"cot_id": "2", "cot_name": "COT2", "cot_status": "1"}], "flat_name": "Sumangali PG", "floor_name": "GroundFloor" }, { "room_id": "2", "floor_id": "1", "flat_id": "1", "room_name": "Room2", "no_of_cots": "2", "cot_info":[{"cot_id": "1", "cot_name": "COT1", "cot_status": "1"}, {"cot_id": "2", "cot_name": "COT2", "cot_status": "1"}], "flat_name": "Sumangali PG", "floor_name": "GroundFloor" } ] }';
$json=json_decode($content);
foreach($json->result as $row)
{
print_r($row);
}

Insert nested json array into database

I am trying ti insert JSON array into my mysql database.Here I give format for that
array ${
"customer_id": "1",
"products":[ {
"product_id": "1",
"product_qty": "2"
}, {
"product_id": "2",
"product_qty": "4"
}, {
"product_id": "3",
"product_qty": "12"
}, {
"product_id": "4",
"product_qty": "22"
}],
"order_totalamount": "100"
}
I tried insert query as below:
foreach($data as $item) {
//insert into mysql table
$sql = "insert into `order`(cm_id,product_id,product_quantity,order_totalamount,order_id,order_date) values ($cus_id,$item[product_id],$item[product_qty],$order_totalamount,$cus_id,CURDATE())";
also decode JSON data, and foreach loop
json_decode the string and iterate through the array of products and insert them one by one.
$data = json_decode($json_string);
$customer_id = $data->customer_id;
foreach($data->products as $product) // its a array of products you showed in json
{
// insert code here for mysql insert
}
try below code :
$res = '{
"customer_id": "1",
"products":[ {
"product_id": "1",
"product_qty": "2"
}, {
"product_id": "2",
"product_qty": "4"
}, {
"product_id": "3",
"product_qty": "12"
}, {
"product_id": "4",
"product_qty": "22"
}],
"order_totalamount": "100"
}';
$data=json_decode($res);
foreach ($data->products as $item)
$sql = "insert into `order`(cm_id,product_id,product_quantity,order_totalamount,order_id,order_date ) values ($data->customer_id,$item->product_id,$item->product_qty,$data->order_totalamount,$data->customer_id,CURDATE())";

json_encode replaces spaces with carriage return

i have a php script that execute a mysql query and return the result in json_encode format but there is a problem. It seems that json replaces space with carriage return.
This is my json encode response
[
{
"codice": "1",
"messaggio": "Leghe utente",
"output": [
{
"ID_LEGA": "28",
"PORTIERI": "3",
"DIFENSORI": "8",
"CENTROCAMPISTI": "8",
"ATTACCANTI": "6",
"SCAMBI": "5",
"OBBLIGO_ROSA": "1",
"CALC_DUPLICATI": "0",
"MERCATO_LIBERO": "0",
"PARTECIPANTI": "10",
"NOME": "Fantantonio Club 2013/2014",
"CREDITI_INIZIALI": "500",
"SOSTITUZIONI": "3",
"POR_PANCHINA": "1",
"DIF_PANCHINA": "2",
"CEN_PANCHINA": "2",
"ATT_PANCHINA": "2",
"CAMBIO_MODULO": "1",
"PORTIERE_IMBATTUTO": "0.0",
"POR_MODIFICATORE": "0",
"DIF_MODIFICATORE": "1",
"CEN_MODIFICATORE": "0",
"ATT_MODIFICATORE": "0",
"SOGLIA_GOL": "66",
"MINUTI_FORMAZIONE": "15",
"FORMAZIONE_UNICA": "0"
},
{
"ID_LEGA": "29",
"PORTIERI": "3",
"DIFENSORI": "8",
"CENTROCAMPISTI": "8",
"ATTACCANTI": "6",
"SCAMBI": "5",
"OBBLIGO_ROSA": "1",
"CALC_DUPLICATI": "0",
"MERCATO_LIBERO": "0",
"PARTECIPANTI": "2",
"NOME": "Lega di Esempio",
"CREDITI_INIZIALI": "500",
"SOSTITUZIONI": "3",
"POR_PANCHINA": "1",
"DIF_PANCHINA": "2",
"CEN_PANCHINA": "2",
"ATT_PANCHINA": "2",
"CAMBIO_MODULO": "0",
"PORTIERE_IMBATTUTO": "0.0",
"POR_MODIFICATORE": "0",
"DIF_MODIFICATORE": "0",
"CEN_MODIFICATORE": "0",
"ATT_MODIFICATORE": "0",
"SOGLIA_GOL": "66",
"MINUTI_FORMAZIONE": "15",
"FORMAZIONE_UNICA": "0"
}
]
}
]
Why, for example, json insert a carriage return between "Lega di" and "Esempio". In my db table there is not a carriage return.
Why?

Categories