I want to update the products leased count when a product is rented to a customer.
The owner of the website can rent multiple things. I do this by cloning with Ajax.
Now, when someone has rented like 2 chairs and 1 table, i want to update the products_leased row for both products. How would i do that?
The error i get:
`SQLSTATE[01000]: Warning: 1265 Data truncated for column 'product_leased' at row 1 (SQL: update `products` set `product_leased` = 3 5, `updated_at` = 2017-06-07 19:57:03 where `id` = 1)`
My controller:
public function create_order($id) {
$customer = Customer::find($id);
$pr = Product::get();
$rent_from = Request::input('rent_from');
$rent_till = Request::input('rent_till');
$productArray = Request::get('product_name');
$productCountArray = Request::get('product_count');
$rentProduct = new cRent;
$rentProduct->customer_id = $customer->id;
$rentProduct->rent_from = $rent_from;
$rentProduct->rent_till = $rent_till;
$rentProduct->rented_products = implode(', ', $productArray);
$rentProduct->rented_products_count = implode(', ', $productCountArray);
$rentProduct->save();
$pru = Product::find($id);
$pru->product_leased = implode(' ', $productCountArray);
$pru->update();
return back();
}
The code above works only when one product has been rented. Not when i rent 2 products.
Thanks in advance.
Why are you passing in the same $id to
$customer = Customer::find($id);
and
$pru = Product::find($id);
?
Instead of imploding the product names and counts and storing them together, why not create separate rent records for each product and relate them as a one-to-many? Assuming product name and product count line in the arrays, something to the effect of:
foreach(array_combine($productArray, $productCountArray) as $product_name => $count){
$product = Product::where('name', $product_name)->first();
$rentProduct = new cRent;
$rentProduct->customer_id = $customer->id;
$rentProduct->rent_from = $rent_from;
$rentProduct->rent_till = $rent_till;
$rentProduct->product_id = $product->id
$rentProduct->count = $count;
$rentProduct->save();
}
Better yet, don't pass in the product names, instead pass the product ids in from the form.
$rentProduct = new cRent;
the code above indicates only one instance of your cRent off-course if you want have another transaction with the database with a different values or data another instance is needed
I think what you want is something like this
public function create_order($id) {
$customer = Customer::find($id);
$pr = Product::get();
$rent_from = Request::input('rent_from');
$rent_till = Request::input('rent_till');
$productArray = Request::get('product_name');
$productCountArray = Request::get('product_count');
foreache($productArray as $prod){
$rentProduct = new cRent;
$rentProduct->customer_id = $customer->id;
$rentProduct->rent_from = $rent_from;
$rentProduct->rent_till = $rent_till;
$rentProduct->rented_products = $prod
$rentProduct->rented_products_count = //not sure in this line
$rentProduct->save();
}
$pru = Product::find($id);
$pru->product_leased = implode(' ', $productCountArray);
$pru->update();
return back();
}
Related
Here is the code :
if ($param1 == 'do_update') {
$data['student_id'] = $this->input->post('student_id');
$data['title'] = html_escape($this->input->post('title'));
if ($this->input->post('description') != null) {
$data['description'] = html_escape($this->input->post('description'));
}
$data['amount'] = html_escape($this->input->post('amount'));
$data['amount_paid'] = html_escape($this->input->post('amount_paid'));
$data2['amount'] = html_escape($this->input->post('amount'));
/*$data['status'] = $this->input->post('status');*/
$data['creation_timestamp'] = strtotime($this->input->post('date'));
$this->db->where('invoice_id', $param2);
$this->db->update('invoice', $data);
$this->db->where('invoice_id', $param2);
$this->db->update('payment', $data2);
2 tables Invoice and Payment... I can update data in "invoice table"
with a specific ID, but also at the same time, it is updating the
whole column in the "Payment table" instead of that specific ID. I
can tell doing something wrong in the last 4 lines.
$data fields are of Invoice Table and
$data2 fields are of Payment table
Invoice_id is common in both tables..
You aren't actually using your where conditions. Try:
$this->db->where('invoice_id', $param2)->update('invoice', $data);
$this->db->where('invoice_id', $param2)->update('payment', $data2);
I want to get the data form database table and create a new row in another table.
Which 1 PO have many PoProducts.
$_getPO = Order::find($id);
$_getPOProducts= OrderProducts::where('order_id', $id)->get();
$order_no = $_getPO->order_no;
$eta = $_getPO->eta;
$_Order = new DeliveryOrders();
$_Order->order_no = $order_no;
$_Order->eta = $eta;
$_Order->save();
$POProduct = array();
foreach($_getPOProducts as $i => $_getPOProduct)
{
$POProduct[] = new DeliveryOrderProducts();
$POProduct[] = $_getPOProduct->order_id;
$POProduct[] = $_getPOProduct->item_id;
$POProduct[] = $_getPOProduct->qty;
$POProduct->save();
}
But, this output an error.
Call to a member function save() on array
Please help me. Thanks.
If you wish to copy records from one table to another or just duplicate a record in the same table you could simply use the repliacate() method.
$user = User::findOrFail($id);
// replicate (duplicate) the data
$staff = $user->replicate();
// make into array for mass assign.
//make sure you activate $guarded in your Staff model
$staff = $staff->toArray();
Staff::firstOrCreate($staff);
Note: in case you're only duplicating on the same table replace Staff with User on this example.
You are trying to run the save method on the array but what you want is to use it on the array index instead.
Change your foreach to this and it should work (assuming columns are the same).
foreach($_getPOProducts as $i => $_getPOProduct)
{
$POProduct[$i] = new DeliveryOrderProducts();
$POProduct[$i]->order_id = $_getPOProduct->order_id;
$POProduct[$i]->item_id = $_getPOProduct->item_id;
$POProduct[$i]->qty = $_getPOProduct->qty;
$POProduct[$i]->save();
}
You can shorten this by using forceCreate.
foreach($_getPOProducts as $i => $_getPOProduct)
{
$POProduct[$i] = (new DeliveryOrderProducts())->forceCreate($_getPOProduct->only(['order_id', 'item_id', 'qty']));
}
I know to get the last id I can use insertGetId() but I want to know how can I get the last inserted id through save() method.
order = new Store_order;
$order->invoice_id = $signed['invoice_id'];
$invoice = $order->save()['so_id'];
I used this but it returns null value
After $order->save(), $order->so_id should be the last id inserted. Your code should be as below.
$order = new Store_order;
$order->invoice_id = $signed['invoice_id'];
$order->save();
$invoice = $order->so_id;
You can get it by like below :
$order = new Store_order;
$order->invoice_id = $signed['invoice_id'];
$invoice = $order->save();
echo $invoice->so_id;
in this case you no need to store in one variable and then access it, You can get the inserted records by calling the model object itself :
$order = new Store_order;
$order->invoice_id = $signed['invoice_id'];
$order->save();
// echo $order; => will return entire stored last record.
echo $order->so_id;
Make sure so_id is autoincrement.
Try this once it worked for me where $mode->save(); was only returning true.
$mylastid = DB::getPdo()->lastInsertId();
In case of you did overwride the name of primary key in the model as in next case:
protected $primaryKey = 'your_table_id_name';
use:
$id = $your_variable_save->your_table_id_name
if not:
$id = $your_variable_save->id
You can get the last inserted id by the following codes.
Using Laravel Eloquent
$order = new Store_order();
$order->invoice_id = $signed['invoice_id'];
$order->save();
$lastInsertedId = $order->so_id; //now getting Last inserted id
echo $lastInsertedId;
Provided that $order->so_id means last inserted id of the given object where so_id is the primary key of the table.
$ObjTable->nane = $name;
$ObjTable->save();
echo $ObjTable->id;
this will be display last inserted id.
I'm in a controller for Laravel 5.2 and am trying to iterate through an eloquent collection of invoice_items, which would translate to something like order items. So, the invoice would act as the order, have it's ordered items (invoice_item), and the invoice_items would list all of the products ordered (product).
Here's what I have:
$id = $value; //from param
$invoice = Invoice::where('id', $id)->get();
$invoice_items = Invoice_item::all()->where('invoice_id', $invoice[0]->id);
$contact = Contact::where('id', $invoice[0]->contact_id)->get();
foreach($invoice_items as $item) {
$products = Product::all()->where('id', $item->product_id);
}
I'm attempting to pull all of the products from that specific invoice (via invoice items), which in this specific case should be two, different products.
What's happening, is when I iterate through using that loop, it's adding the same product twice, whereas it should be adding each product once. Is my logic just wrong here? Or do I need to look at my relationships again or something?
Change your queries to:
$invoice = Invoice::where('id', $id)->get();
$invoice_items = Invoice_item::where('invoice_id', $invoice[0]->id)->get();
$contact = Contact::where('id', $invoice[0]->contact_id)->get();
foreach($invoice_items as $item) {
$products = Product::where('id', $item->product_id)->get();
}
An easier way may be to add a items relation to the InvoiceItems model. E.g.:
public function items()
{
$this->hasOne('Items');
}
Then you can get all the Items from Invoice_item using:
return $invoice_items->items;
You can also try:
$invoice = Invoice::where('id', $id)->get();
$invoice_items = Invoice_item::where('invoice_id', $invoice[0]->id)->get()->lists('product_id');
$contact = Contact::where('id', $invoice[0]->contact_id)->get();
$products = Product::whereIn('id', $invoice_items)->get();
Hopefully, $products will then contain a collection of products for that invoice. No need for a foreach loop.
Say I have Product model which has Category property and I want to pull all such products where category is null using search() function.
$productSearch = clone Product::model();
$productSearch->Category = null;
$products = $productSearch->search()->getData();
By examining the generated SQL I see that it does not work, category is not being mentioned in the query at all. What is the best way to do it?
Also how to search records which have certain property set to NULL OR Certain value
1st variant:
// use search scenario, not clone model with metadata
$productSearch = new Product('search');
$productSearch->unsetAttributes();
// get CActiveDataProvider
$dataProvider = $productSearch->search();
// Add null condition
$dataProvider->criteria->addCondition('Category is null');
// Get data - first 10 rows
$products = $dataProvider->getData();
2nd variant (preffered):
$products = Product::model()->findAll('Category is null');
Assigning null value to a property does not work as it is not recorded by Yii: initial value of properties is null and Yii does not track modified properties (which is a bit annoying).
The work around here is to use it like this:
$productSearch = clone Product::model();
$productSearch->Category = array(null);
$products = $productSearch->search()->getData();
Note that if you want to search for more advanced Category IS NULL OR Category IN (1, 2, 3) the expected way to do it:
$productSearch->Category = array(null, 1, 2, 3);
won't work as Yii blindly puts it all into a single IN statement:
Category IN (NULL, 1, 2, 3)
Work around here is more complicated as it requires extra code in your model search method:
public function search()
{
$criteria = new CDbCriteria;
// Work around of inability of Yii to handle IS NULL OR IN () conditions
if (is_array($this->Category) && count($this->Category) > 1)
{
$hasNull = false;
$values = array();
foreach ($this->Category as $value)
{
if (is_null($value))
{
$hasNull = true;
}
else
{
array_push($values, $value);
}
}
$condition = array();
if ($hasNull) array_push($condition, 'Category IS NULL');
if (count($values)) array_push($condition, "Category IN ('" . implode("', '", $values) . "')");
$criteria->addCondition(implode(' OR ', $condition));
}
else
{
$criteria->compare('Category', $this->Category);
}
// other search criterias ...
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}