Laravel UPDATE query with multiple where conditions not working - php

I am fed up. Simple update method not working. It should update record matching the given item_id,restaurant_id and avil_date.
public function updateWeeklyItem(){
$item_id=request('item_id');
$restaurant_id=request('restaurant_id');
$avil_date=request('avil_date');
weekly_item::where(function($query) use ($item_id, $restaurant_id,$avil_date){
$query->where('item_id', $item_id)
->where('restaurant_id', $restaurant_id)
>where('avil_date', $avil_date);
})->update ([
'start_time'=>request('start_time'),
'end_time'=>request('end_time'),
'tiffin-switch'=>request('tiffin-switch'),
'lunch-switch'=>request('lunch-switch'),
'snacks-switch'=>request('snacks-switch'),
'dinner-switch'=>request('dinner-switch'),
'special-switch'=>request('special-switch'),
'extend-avil'=>request('extend-avil'),
'unit'=>request('unit'),
'quantity'=>request('quantity')
]);
}

Use This Code:
public function updateWeeklyItem(Request $request) {
$item_id = $request->item_id;
$restaurant_id = $request->restaurant_id;
$avil_date = $request->avil_date;
$item = weekly_item::where(function($query) use ($item_id, $restaurant_id,$avil_date) {
$query->where('item_id', $item_id)
->where('restaurant_id', $restaurant_id)
->where('avil_date', $avil_date);
})->first();
$item_array = (
'start_time' => $request->start_time,
'end_time' => $request->end_time,
'tiffin-switch' => $request->tiffin-switch,
'lunch-switch' => $request->lunch-switch,
'snacks-switch' => $request->snacks-switch,
'dinner-switch' => $request->dinner-switch,
'special-switch' => $request->special-switch,
'extend-avil' => $request->extend-avil,
'unit' => $request->unit,
'quantity' => $request->quantity
);
$item->update($item_array);
return $item;
}

Related

I get a 504 timeout error when importing excel to a db (Laravel)

I'm trying to import 10,000 records in excel with the library "Maatwebsite / Laravel-Excel" in Laravel, but when importing 10,000 records, the page returns a 504 timeout error (the odd thing is that it keeps inserting data in the db after that). Well then, I show you my code to see if you can help me, thanks.
This is the Import Inventory, where I programmed everything I should do with the data to be imported into the db.
class InventarioImport implements ToCollection, WithChunkReading
{
use Importable;
/**
* #param array $row
*
* #return User|null
*/
public function chunkSize(): int
{
return 250;
}
public function collection(collection $rows)
{
foreach ($rows as $row)
{
$almacenes = Almacen::where('tienda_id', \Session::get('tienda_id'))->get();
$codigos = Codigo::where('tienda_id', \Session::get('tienda_id'))->get();
$contc=-1;
$conta=0;
foreach ($codigos as $codigo) {
$contc++;
}
foreach ($almacenes as $almacen) {
$conta++;
}
if($row[$contc+1]=="DESCRIPCION") {
continue 1;
}
$idmarca = Marca::where('nombre_marca', $row[$contc+2])->where('tienda_id', \Session::get('tienda_id'))->first();
if ($idmarca==null) {
$marcaid= Marca::create([
'nombre_marca' => $row[$contc+2],
'tienda_id' => \Session::get('tienda_id'),
'estado' => "A",
])->id;
}
else {
$marcaid = $idmarca->id;
}
$buscarcategoria = Categoria::where('nombre_categoria', $row[$contc+3])->where('tienda_id', \Session::get('tienda_id'))->first();
if ($buscarcategoria==null) {
Categoria::create([
'nombre_categoria' => $row[$contc+3],
'tienda_id' => \Session::get('tienda_id'),
'estado' => "A",
]);
}
$i=0;
$buscaritem = Item::where('nombre_item', $row[$contc+1])->where('tienda_id', \Session::get('tienda_id'))->first();
$buscarunidad = Unidad::where('nombre_unidad', $row[$contc+4])->first();
$buscarmoneda = ConfiguracionMoneda::where('abreviacion_moneda', $row[$conta+3+$contc+4])->where('tienda_id', \Session::get('tienda_id'))->first();
$itemid= Item::create([
'marca_id' => $marcaid,
'nombre_item' => $row[$contc+1],
'unidad_id' => $buscarunidad->id,
'stock_minimo' => $row[$conta+1+$contc+4],
'stock_maximo' => $row[$conta+2+$contc+4],
'moneda_id' => $buscarmoneda->id,
'precio' => $row[$conta+4+$contc+4],
'impuesto' => $row[$conta+5+$contc+4],
'margen_final' => $row[$conta+6+$contc+4],
'margen_inicio' => $row[$conta+7+$contc+4],
'notas' => $row[$conta+8+$contc+4],
'estado' => "A",
'tienda_id' => \Session::get('tienda_id'),
])->id;
$a=$contc+4;
$j=$a+1;
foreach ($almacenes as $almacen) {
$buscaralmacen = Almacen::where('nombre_almacen', $almacen->nombre_almacen)->where('tienda_id', \Session::get('tienda_id'))->first();
ItemStock::create([
'item_id' => $itemid,
'almacen_id' => $buscaralmacen->id,
'detalle' => "Saldo Inicial",
'cantidad' => $row[$j],
'tipo' => "M",
'tienda_id' => \Session::get('tienda_id'),
'estado' => "A",
]);
$kardex = new Kardex();
$kardex->item_id = $itemid;
$kardex->fecha = date("Y/m/d");
$kardex->operacion = "Inicial";
$kardex->tipo = "";
$kardex->serie = "";
$kardex->numero = "";
$kardex->almacen_id = $buscaralmacen->id;
$kardex->tienda_id = \Session::get('tienda_id');
$kardex->saldocantidad = $row[$j];
$kardex->saldocosto = $row[$conta+4+$contc+4];
$kardex->saldototal = $row[$conta+4+$contc+4]*$row[$j];
$kardex->save();
$j++;
}
foreach ($codigos as $codigo) {
$buscarcodigo = Codigo::where('nombre_codigo', $codigo->nombre_codigo)->where('tienda_id', \Session::get('tienda_id'))->first();
$buscaritem = Item::where('nombre_item', $row[$contc+1])->where('tienda_id', \Session::get('tienda_id'))->first();
ItemCodigo::create([
'item_codigo' => $row[$i],
'codigo_id' => $buscarcodigo->id,
'item_id' => $buscaritem->id,
'tienda_id' => \Session::get('tienda_id'),
'estado' => "A",
]);
$i++;
}
}
}
}
This is my function in Controller, which I do the import as indicated in the previous script.
public function subirinventario(Request $request)
{
$title = 'Inventario subido';
$contarr= $request->contarr;
$arrayitems = Excel::import(new InventarioImport, $request->path);
return view('item.finalizar', compact('contarr','title'));
}
That would be all, I missed the routes and the views, but it works fine with few records, I have to emphasize that I have increased the runtime of nginx, restarted and nothing.
This is an image with the preview of the data and columns, only 10 are shown, but more than 10 thousand were detected.
You can try to put this 2 lines in the start of your method/function:
ini_set('memory_limit','1024M');
set_time_limit(3000000); //You can use 0 to remove limits
As I stated above you can use 0 inside the set_time_limit method, but i don't recommend this, because losing all the control of your execution is not worth in most cases
Hope it works!

Laravel: Add Pagination to Json Response

I have an array for showing users' contacts list to each other.
I want to add ->paginate(10) features for json response but I could not find where I must put it.
My index function:
public function index()
{
$contacts = [];
$user = request()->user();
// Loop through the contacts and format each one
Contact::for($user->id)->get()->each(function ($contact) use ($user, &$contacts) {
$friend = $contact->user1_id === $user->id ? $contact->user2 : $contact->user1;
$contacts[] = $friend->toArray() + ['room' => $contact->room->toArray()];
});
return response()->json($contacts);
}
You can create a collection for the contact and use LenfthAwarePaginator
class ContactResourceCollection extends ResourceCollection
{
/**
* Transform the resource into an array.
*
* #param \Illuminate\Http\Request
* #return array
*/
public function toArray($request)
{
$response = [
'data' => $this->collection,
];
if($this->resource instanceof LengthAwarePaginator)
{
$response['pagination'] = [
'total' => $this->resource->total(),
'lastPage' => $this->resource->lastPage(),
'perPage' => $this->resource->perPage(),
'currentPage' => $this->resource->currentPage(),
'nextPageUrl' => $this->resource->nextPageUrl(),
'previousPageUrl' => $this->resource->previousPageUrl(),
];
}
return $response;
}
}
In the controller method add this line:
return new UserResourceCollection($users);
Here is the total code
$contacts = Contact::where('user_id', $user->id)->paginate(12);
if($contacts->count()){
$pageIndex = array();
$lastPage = $contacts->lastPage();
$user = request()->user();
for($i= 2; $i<=$lastPage; $i++){
array_push($pageIndex, $i);
}
return response()->json([
'contacts' => $contacts->map(function ($contact) use ($user) {
if($contact->user1_id === $user->id){
return [
'friend' => $contact->user2,
'room' => $contact->room,
];
} else {
return [
'friend' => $contact->user1,
'room' => $contact->room,
];
}
})->toArray(),
'per_page' => $contacts->perPage(),
'on_first_page' => $contacts->onFirstPage(),
'last_page' => $contacts->lastPage(),
'first_page_url' => $contacts->url(1),
'next_page_url' => $contacts->nextPageUrl(),
'prev_page_url' => $contacts->previousPageUrl(),
'last_page_url' => $contacts->url($contacts->lastPage()),
'total' => $contacts->total(),
'pageIndexArray' => $pageIndex,
'errors' => false,
]);
} else {
// Do Nothing
}
Call
GET 'URL?page='+Page_index to get the response in JS (AJAX)
I am not sure but try : replace get() to paginate(10)
Contact::for($user->id)->paginate(10)->each(function ($contact) use ($user, &$contacts) {
$friend = $contact->user1_id === $user->id ? $contact->user2 : $contact->user1;
$contacts[] = $friend->toArray() + ['room' => $contact->room->toArray()];
});
Can you change the query into:
$contacts = Contact::for($user->id)->paginate(10);
Then after this query you can use for loop for $contact;
foreach ($contacts as $key => $contact)
{
$friend = $contact->user1_id === $user->id ? $contact->user2 : $contact->user1;
$contacts[] = $friend->toArray() + ['room' => $contact->room->toArray()];
}
Paginate first before get into loop/each.

Laravel: Count number of uploads and return those

Using this lib uploading works great. I have number of objects in an excel and I go through them and do whatever I desire.
The question is while uploading the excel I am ought to check whether a particular object already exists, if so increment the $rejected variable otherwise create and increment the $uploaded variable. As a result I would like to return the results: how many uploaded and how many rejected? Whats the best way to do as such? It is obvious I can't access those variables inside the function. What's the best practice here?
public function uploadUsingFile($file)
{
$rejected = 0;
$uploaded = 0;
Excel::load($file, function ($reader) {
foreach ($reader->toArray() as $row)
{
$plateAlreadyExist = Plate::where('serial_number', $row['plate_serial_number'])->exists();
if ($plateAlreadyExist) {
$rejected += 1;continue;
}
$supplier = Supplier::firstOrCreate(['name' => $row['supplier_name']]);
$statusName = EquipmentStatusCode::firstOrCreate(['name' => $row['status_name']]);
$plateType = PlateType::firstOrCreate(['name' => $row['plate_type_name']]);
$process = Process::firstOrCreate(['name' => $row['process_name']]);
$project = Project::firstOrCreate(['name' => $row['project_name']]);
$plateQuality = PlateQuality::firstOrCreate(['name' => $row['plate_quality']]);
$wafer = Wafer::firstOrCreate(['serial_number' => $row['wafer_serial_number']]);
$data = [
'serial_number' => $row['plate_serial_number'],
'crc_code' => $row['crc_code'],
'supplier_id' => $supplier['id'],
'equipment_status_code_id' => $statusName['id'],
'plate_type_id' => $plateType['id'],
'process_id' => $process['id'],
'project_id' => $project['id'],
'plate_quality_id' => $plateQuality['id'],
'wafer_id' => $wafer['id'],
'created_by' => Auth::user()->id,
];
if($data)
{
Plate::create($data);
$uploaded += 1;
}
}
});
return [ 'uploaded' => $uploaded, 'rejected' => $rejected ];
}
You can pass a reference to the variables into the closure by using the use keyword:
...
Excel::load($file, function ($reader) use(&$rejected, &$uploaded){
...
}
Anonymous Functions

How to format array data in more cleaner way?

I have this method:
private function formatCliendCardData($data) {
$formatedData = array();
$formatedData['first_name'] = trim($data['name']);
$formatedData['last_name'] = trim($data['surname']);
$formatedData['date_of_birth'] = $data['birth_year'] . '-' . sprintf("%02d", $data['birth_month_id']) . '-' . sprintf("%02d", $data['birth_day']); //[yyyy-mm-dd]
$formatedData['sex'] = ($data['sex_id'] == 's1'? 'm' : 'f');
$formatedData['county'] = 'Latvija'; //#TODO get real data
$formatedData['post_index'] = (int) preg_replace( '/[^0-9]/', '', $data['post_index']);
$formatedData['city'] = trim($data['city']);
$formatedData['street'] = trim($data['street']);
$formatedData['house_nr'] = trim($data['house_nr']);
$formatedData['phone'] = trim($data['phone']);
$formatedData['email'] = trim($data['email']);
return $formatedData;
}
I run in this problem from time to time. I have big method that just reformats data and returns it. It looks ugly. There is few other aproaches Im aware -- just make foreach loop, but there are exeptions, so i need make 'ifs' anyway. What is better way to reformat such data in your opinon?
I aggree with helloei,
create a class to handle all the formating and validation in the setter.
class Person
{
public function setName($name)
{
$this->name = trim($name);
}
...
}
and then create the object in your function:
private function formatCliendCardData($data) {
$person = new Person();
$person->setName($data['name`])
...
if you have highly custom condition to reformat some datas i think you don't have any other solution that apply this condition manually with some if/else or other custom loop.
if else, you have some unified condition to reformat you can aggregate this and apply in batch at your datas. In other word for my opinion the only other solution are: generalize conditions of your reformatting operations and apply this at your data.
this is Object Oriented approach
IMHO In those cases you have to reduce and clean your code. Often I use an helper function that allows me to split data and formatting rules.
// helper function (OUTSITE YOUR CLASS)
function waterfall($input=NULL,$fns=array()){
$input = array_reduce($fns, function($result, $fn) {
return $fn($result);
}, $input);
return $input;
}
// your formatting rules
$rules = array(
'first_name' => array('trim'),
'last_name' => array('trim'),
'date_of_birth' => array(
'f0' => function($x){
return sprintf("%s-%02d-%02d",
$x['birth_year'],
$x['birth_month_id'],
$x['birth_day']
);
},
'trim'
),
'sex' => array(
'f0' => function($x){
if($x['sex_id'] == 's1'){
return 'm';
}else{
return 'f';
}
}
),
'post_index' => array(
'f0' => function($x){
return (int) preg_replace('/[^0-9]/', NULL, $x);
},
'trim'
),
'city' => array('trim'),
'email' => array('strtolower','trim')
);
// your data
$data = array(
'first_name' => ' Andrea',
'last_name' => 'Ganduglia ',
'date_of_birth' => array(
'birth_year' => '1899',
'birth_month_id' => 5,
'birth_day' => 7
),
'post_index' => 'IT12100',
'city' => 'Rome',
'email' => 'USER#DOMAIN.tld '
);
// put all together
$formatedData = array();
foreach($data as $k => $v){
$formatedData[$k] = waterfall($v,$rules[$k]);
}
// results
print_r($formatedData);
/*
Array
(
[first_name] => Andrea
[last_name] => Ganduglia
[date_of_birth] => 1899-05-07
[post_index] => 12100
[city] => Rome
[email] => user#domain.tld
)
*/

Update Query in codeigniter model is not working

In the function update_status the 2nd update query is not working. The values in array are properly getting and correct. Can somebody please help me with that?
Here my code:
function update_status($appno,$value,$user_note,$date)
{
$status = array(
'app_status' => $value, );
$this->db->where('app_no',$appno);
$this->db->update('tbl_application', $status);
$today = date('Y-m-d');
$emp = $this->session->userdata('username');
$this->db->select('*');
$this->db->from('tbl_employee');
$this->db->where('Name', $emp);
$query = $this->db->get();
foreach($query->result() as $row)
{ $emp_code = $row->EmployeeCd; }
$data = array(
'app_no' => $appno,
'office_code' => 'KL08',
'verifi_status' => $value,
'view_status' => 1,
'view_by' => $emp_code,
'first_view' => $date,
'last_view' => $today,
'modification' => $user_note,
'modifi_status' => '1',
'notes' => 'NA'
);
$this->db->update('tbl_appverification', $data);
$this->db->where('app_no',$appno);
}
Where clause must be written before udpate.
$this->db->where('app_no',$appno);
$this->db->update('tbl_appverification', $data);
Please write the Where clause before the Update query, as you have written in your first update.

Categories