I am making an online shopping cart, and I having huge issues pushing add to cart to items
$collection->update(
array('session' => $_SESSION["redi-Shop"],
array('$push'=>
array('items'=> $_POST["item"])
)));
When the customer selects their first item to add to the cart it works fine
$collection->insert(
array('session' => $_SESSION["redi-Shop"],
'status' => "cart",
'items' =>$_POST['item']));
but after the first item is added it does not allow me to add any more.
Please any advice would be helpful.
When you insert it the first time, the items field is not array (probably a string).
According to the mongodb $push doc :
The operation will fail if the field specified in the $push statement
is not an array.
Change your insert operation to :
$collection->insert(
array(
'session' => $_SESSION["redi-Shop"],
'status' => "cart",
'items' => array($_POST['item'])
));
Then run your update query.
Related
I have a table that has a column called "items", but not all rows have it, so I want to scan all rows that have "items".
Something like:
$resposta = $clientDB->scan(array(
'TableName' => 'tableName',
'Key' => [
'items' => ['S' => 'exists']
]
));
But I can't figure out how to do it...
The table has 10000 rows, but only 10 of them have "items", and I want to get only these 10 rows.
Edit:
As Seth Geoghegan pointed below, it was necessary create a global secondary indexes on the attribute i wanted to filter.
I ended up here:
$params = [
'TableName' => 'tableName',
'FilterExpression' => "attribute_exists(items)"
];
OR
$params = [
'TableName' => 'tableName',
'FilterExpression' => 'items != :null',
'ExpressionAttributeValues' => [
':null' => null,
],
];
But both didnt worked... First one seens necessary ExpressionAttributeValues to be setup and the second the php stop working with no error logs.
This is a perfect use case for global secondary indexes (GSI)!
You can create a GSI on the items attribute. Items with the items attribute defined will get projected into the GSI. Importantly, items that do not contain this attribute will not be in the index. You could then query the GSI and retrieve the items you're after.
Well, after some effort, i found a way though:
$resposta = $clientDB->scan(array(
'TableName' => 'tableName',
'FilterExpression' => "attribute_exists(items)"
));
After i created another global secondary index (GSI) for "items" (pointed by Seth Geoghegan), i just needed to add in the scan function the FilterExpression the "attribute_exists(items") and it worked.
I have this code in my Shopping Cart. I got this from a site. I am using this and code is working fine but i can not understand the difference between the first code and the second code. It seems both are same to me. But if i use only the first code in the place of second code then my cart doesn't work right. So can anyone tell me what is the difference between them??
First code:
$cart = [
$id => [
'name' => $product->name,
'quantity' => 1,
'price' => $product->price,
'photo' => $product->image
]
];
Second code:
$cart[$id] = [
'name' => $product->name,
'quantity' => 1,
'price' => $product->price,
'photo' => $product->image
];
The first code replaces, whatever was in $cart before, with a new array. This means that any previous value of $cart is forgotten.
The second code only adds a new key-value pair into the array, or replaces that key-value pair if it already existed. This means that other key-value pairs will not be forgotten.
I assume the $id is a product identifier. The first code will only allow one product in your cart, whereas the second code allows many different products.
As stated in the issues on Laravel Cashier. It does not state how to add meta data to invoices/charges.
Auth::user()->invoiceFor('Stickers', 500, ['metadata' => ['VAT' => 'TEST']]);
After following how to do this based off the issue, as seen above, it successfully creates the invoice however, the meta data is not added.
How can I add meta data?
You may pass meta data to the fourth parameter of invoiceFor().
The third parameter is an array of options for the invoice item whereas the other one is also an array of options but for the invoice itself.
$user->invoiceFor('Stickers', 500, [], [
'tax_percent' => 20,
'metadata' => [
'custom-option' => 'value',
]
]);
This function is not in the Laravel documentation, I have found it in the source code however I am not completely sure how it should be used. For example, if I am working with products, I want to either insert or update a product in the database based on its UPC. If a product with the same UPC exists, update it with the new values. If not, insert the new values as a new product. How should this be done using this function?
Thank you!
Insert or update a record matching the attributes, and fill it with values.
updateOrInsert(array $attributes, array $values = [])
https://laravel.com/api/master/Illuminate/Database/Query/Builder.html#method_updateOrInsert
DB::table('products')->updateOrInsert(
[
'upc' => $request->get('upc'),
],
[
'upc' => $request->get('upc'),
'name' => $request->get('name'),
'vendor' => $request->get('vendor'),
'description' => $request->get('description')
]
);
i want to add an extra column in AdminCustomersControllerCore so that it will show a new column in my back office manage customer page.
Use the hook actionControllerNameListingFieldsModifier to add fields.
Every admin controller calls this hook before generating the list/table.
Hook::exec('action'.$this->controller_name.'ListingFieldsModifier', array(
'select' => &$this->_select,
'join' => &$this->_join,
'where' => &$this->_where,
'group_by' => &$this->_group,
'order_by' => &$this->_orderBy,
'order_way' => &$this->_orderWay,
'fields' => &$this->fields_list,
));
You will need to modify select and fields variable to include your own. You might need to modify join variable as well if you have data in a custom table.