How to add offset after sortBy in laravel - laravel

How can I add offset after using sortBy in laravel? Please see my code below.
Controller
$order_type = ($dir == 'asc') ? 'sortBy' : 'sortByDesc';
$inventories = $inventories->get()->$order_type(function($inventory) {
$item_status = [
'0' => 'I',
'1' => 'D',
'2' => 'HI',
'3' => 'HR',
'4' => 'A',
'5' => 'DS'
];
return $item_status[$inventory->receive_item->inspection_status];
});
$inventories = $inventories->offset($start)->limit($limit);
Error I get
BadMethodCallException in Macroable.php line 74: Method offset does not exist.

Collection's do not have an offset or limit method. You can use slice and take:
$inventories->slice($start)->take($limit)
Laravel 5.2 Docs - Collections - Available Methods - slice
Laravel 5.2 Docs - Collections - Available Methods - take

Related

FirstOrCreate making duplicates

In Laravel I'm running the following
PlateOrder::firstOrCreate(
['internal_order' => $internalOrder],
['internal_order' => $internalOrder,
'order_number' => $order->order_number, 'product' => $line_item->name,
'img_url' => $plate->template, 'template' => 1,
'order_date' => $fixedDate,
'qty' => $line_item->quantity,
'custom1' => $firstLineTrimmed,
'custom2' => $secondLineTrimmed,
'created_at' => $order->created_at,
'r' => $plate->r, 'g' => $plate->g, 'b' => $plate->b,
'spacing' => $plate->spacing, 'top' => $plate->top ]
);
Using firstOrCreate the first parameter internal_order will check if it exists. If it exists it'll update it if it doesn't it should create it.
However in my database it's causing it to write duplicates.
84826 142544 1425441HIGHMARK2 08-25-2020 HIGHMARK VINYLS NULL NULL Colorado Plate Hat - Flex Fit / L/XL (7 1/4" - 7 5/8") / Black FF prt_Colorado.ai NULL 2 NULL 2020-08-25T05:47:14-07:00 1 0 77 41 38 86
84827 142544 1425441HIGHMARK2 08-25-2020 HIGHMARK VINYLS NULL NULL Colorado Plate Hat - Flex Fit / L/XL (7 1/4" - 7 5/8") / Black FF prt_Colorado.ai NULL 2 NULL 2020-08-25T05:47:14-07:00 1 0 77 41 38 86
It creates an identical record except for the id. Is this a bug or am I missing something?
Thanks to #Derek Pollard, I dug into this and it indeed is a mysql race condition.
To fix this I ended up making internal_order column a unique id. Any attempt to create a double will error out.
Here are some references to:
https://github.com/spatie/first-or-create-racing-condition-demo
https://gist.github.com/troatie/def0fba42fcfb70f873b7f033fbe255f
If you want the record to be updated if it doesn't already exist you should use updateOrCreate as firstOrCreate will not update the existing record but rather will return it. As for it inserting the duplicate records, it must not be finding the record that has the same internal_order value. Is it possible that there is trailing or leading whitespace in the existing entry that is causing it not to be found? Also, there is no reason to repeat the internal_order values in the second array as it will use the values provided in the first array automagically.
PlateOrder::updateOrCreate(
['internal_order' => $internalOrder],
[
'order_number' => $order->order_number,
'product' => $line_item->name,
'img_url' => $plate->template,
'template' => 1,
'order_date' => $fixedDate,
'qty' => $line_item->quantity,
'custom1' => $firstLineTrimmed,
'custom2' => $secondLineTrimmed,
'created_at' => $order->created_at,
'r' => $plate->r,
'g' => $plate->g,
'b' => $plate->b,
'spacing' => $plate->spacing,
'top' => $plate->top ]
);

A PHP error was encountered when conditions are applied in model

I have tables
table1(id, name)
table2(id, name, active)
The field active in table2 contains value '0' or '1'
My relation in my model is
static $has_many = array(
array('table2', 'conditions' => array('active = ?' => array(0)))
);
Later I have to find all where
table2.active = '1'
But now I am getting error as:
A PHP Error was encountered
Severity: Notice
Message: Undefined offset: 0
Filename: lib/Relationship.php
for condition value should be scalar (string, number, boolean)
$has_many = array(
array('table2', 'conditions' => array('active' => 0))
);
You have to use array only when using IN condition in query only.

A PHP Error was encountered Severity: Notice Message: Array to string conversion Filename: mysql/mysql_driver.php Line Number: 552

Model :
function trans_gdc_add()
{
$trans_gdc_gc_package[0] = $this->input->post('trans_gdc_gc_package');
$trans_gdc_gc_package1 = implode(",", $trans_gdc_gc_package[0]);
$add=array(
'trans_gdc_no' => $this->input->post('trans_gdc_no'),
'trans_gdc_to' => $this->input->post('trans_gdc_to'),
'trans_gdc_vehicle_no' => $this->input->post('trans_gdc_vehicle_no'),
'trans_gdc_vehicle_type' => $this->input->post('trans_gdc_vehicle_type'),
'trans_gdc_vehicle_chas' => $this->input->post('trans_gdc_vehicle_chas'),
'trans_gdc_vehicle_make' => $this->input->post('trans_gdc_vehicle_make'),
'trans_gdc_vehicle_eng' => $this->input->post('trans_gdc_vehicle_eng'),
'trans_gdc_vehicle_permit' => $this->input->post('trans_gdc_vehicle_permit'),
'trans_gdc_vehicle_pol' => $this->input->post('trans_gdc_vehicle_pol'),
'trans_gdc_vehicle_isby' => $this->input->post('trans_gdc_vehicle_isby'),
' trans_gdc_date' => $this->input->post(' trans_gdc_date'),
'trans_gdc_from' => $this->input->post('trans_gdc_from'),
'trans_gdc_driver_add' => $this->input->post('trans_gdc_driver_add'),
'trans_gdc_lic_no' => $this->input->post('trans_gdc_lic_no'),
'trans_gdc_vehilce_owner_name' => $this->input->post('trans_gdc_vehilce_owner_name'),
'trans_gdc_vehicle_owner_add' => $this->input->post('trans_gdc_vehicle_owner_add'),
'trans_gdc_vehicle_owner_mob' => $this->input->post('trans_gdc_vehicle_owner_mob'),
'trans_gdc_broker_name' => $this->input->post('trans_gdc_broker_name'),
'trans_gdc_broker_mob' => $this->input->post('trans_gdc_broker_mob'),
'trans_gdc_gc_no' => $this->input->post('trans_gdc_gc_no'),
'trans_gdc_gc_package' => $trans_gdc_gc_package1,
'trans_gdc_gc_cont' => $this->input->post('trans_gdc_gc_cont'),
'trans_gdc_gc_weight' => $this->input->post('trans_gdc_gc_weight'),
'trans_gdc_gc_freight' => $this->input->post('trans_gdc_gc_freight'),
'trans_gdc_gc_consignor' => $this->input->post('trans_gdc_gc_consignor'),
'trans_gdc_gc_consignee' => $this->input->post('trans_gdc_gc_consignee'),
'status' => '1'
);
return $this->db->insert('trans_gc_add',$add);
}
A PHP Error was encountered
Severity: Notice
Message: Array to string conversion
Filename: mysql/mysql_driver.php
Line Number: 552
Please help me to resolve this issue .
check this
' trans_gdc_date' => $this->input->post(' trans_gdc_date'),
space might be giving the problem, repla
'trans_gdc_date' => $this->input->post(' trans_gdc_date'),
You can't directly merge strings in arrays!
your trying to merge concatinate strings with arrays or lets say arrays with string thats not possible
General Example
<?php
$stack = array("Orange", "Banane");
array_push($stack, "Apfel", "Himbeere");
print_r($stack);
?>
for your code Example
// array push tiggers a php array as stack!
$add=array()
array_push($add, 'trans_gdc_no', $this->input->post('trans_gdc_no'));
array_push($add, 'trans_gdc_to', $this->input->post('trans_gdc_to'));
print_r($add);

CodeIgniter ActiveRecord adding a random id field

$updatedOrder = array(
'ship_status' => 'shipped',
'shipped_carrier' => (string)$selectedShipper->shipper->name,
'base_rate' => (float)$selectedShipper->rate,
'discount_rate' => (float)$selectedShipper->rate,
'tracking_number' => '123',
);
$this->orders_m->where('id', $tmpOrder->id)
->update('orders', $updatedOrder);
This yields the following SQL query: UPDATE default_orders SET ship_status = 'shipped', shipped_carrier = 'UPS Next Day Air', base_rate = 22.85, discount_rate = 22.85, tracking_number = '123' WHERE id = '1' AND id = 'orders'
Where did that last bit come from? id='orders'?
Just make sure that $tmpOrder->id is a variable and not an array.
var_dump($tmpOrder->id);
Maybe there is an error somewhere where you are getting the $tmpOrder and it returns an array for that.

Query error in code igniter ( wrong escape )

I try to insert a row in the table on code igniter from a Array, but something is going wrong.
That's the array:
Array
(
[Date] => 2001-08-15
[Number] => 962883
[Time] => 17:40
[Etc1] => 0
[Etc2] => 0
)
And this the insert:
$this->db->insert('mytable', $myarray);
A new line is inserted, but all columns are empty!
Trying to find de error, I printed the last query by
echo $this->db->last_query() ." <br>";
And I got:
INSERT INTO `mytable` (`Date`, `Number`, `Time`, `Etc1`, `Etc2`)
VALUES
('\02\00\00\01\0-\00\08\0-\01\05\0', '\09\06\02\08\08\03\0', '\01\07\0:\04\00\0', '\00\0', '\00\0')
For some reason I can not get, the codeigniter ( or PHP ) is wrongly escaping the values.
Any Idea?
Firstly your change your array like this:
$data_array = Array
(
'Date'=> 2001-08-15
'Number' => 962883
'Time' => 17:40
'Etc1' => 0
'Etc2' => 0
);
$this->your_model->insert_data($data_array);
and inside your model write function like this
function insert_data($data=array())
{
$this->db->trans_start();
$this->db->insert('your_table_name',$data);
$this->db->trans_complete();
return TRUE;
}
i hope this will solve your problem
try this , this may help.
$this->db->set('Date', '2001-08-15', FALSE);
$this->db->set('Number', '962883', FALSE);
$this->db->set('Time', '17:40', FALSE);
$this->db->set('Etc1', '0', FALSE);
$this->db->set('Etc2', '0', FALSE);
this->db->insert('mytable');
I tried emulating your problem and was able to get the correct SQL statement genrated.
But I am pasting here the code that worked for me:
$data = array
(
'Date' => '2001-08-15',
'Number' => '962883',
'Time' => '17:40',
'Etc1' => '0',
'Etc2' => '0'
);
$this->db->insert('mytable', $data);
Let me know if this works - and if not, what the error message is.

Resources