Sync command wont update values - laravel

Hello i have this code in Laravel that updates existing many-to-many relationship tables so when i use the sync command the values update wrong, this is the code:
public function update(Request $request, $id)
{
$order = Order::where('id', $id)->first();
$request->validate([
'order_number' => 'required',
'client_id' => 'required',
'description' => 'required',
'productOrder' => 'required',
'productOrder.*.product_id' => 'required|distinct|exists:products,id',
'productOrder.*.amount' => 'required|numeric|min:1',
]);
$order->update($request->all());
foreach ($request->productOrder as $product) {
$values[] = [
'order_id' => $order->id,
'product_id' => $product['product_id'],
'amount' => $product['amount'],
];
$amount = Product::find($product['product_id']);
$totalValue = $product['amount'] + $amount->amount;
$amount->update(['amount' => $totalValue]);
}
$order->products()->sync($values); //the problem is here
$orders = Order::all();
$orders->load('client', 'products');
return view('orders/index', compact('orders'));
}
If i have 2 values like:
Product1 -> amount: 250
Product2 -> 100
And i updatet these existing values to
Product1 -> amount: 350
Product2 -> 200
The result will be
Product2 -> 200
Product2 -> 200
If i make this line of code dd($order->products()->sync($values)); i get this result
If i add a new product with the existing ones i get this result
From what im understanding the first result is replaced with the second one or removed, why does this happen?

So, you have this code:
public function update(Request $request, $id)
{
$order = Order::where('id', $id)->first();
$request->validate([
'order_number' => 'required',
'client_id' => 'required',
'description' => 'required',
'productOrder' => 'required',
'productOrder.*.product_id' => 'required|distinct|exists:products,id',
'productOrder.*.amount' => 'required|numeric|min:1',
]);
$order->update($request->all());
foreach ($request->productOrder as $product) {
$values[] = [
'order_id' => $order->id,
'product_id' => $product['product_id'],
'amount' => $product['amount'],
];
$amount = Product::find($product['product_id']);
$totalValue = $product['amount'] + $amount->amount;
$amount->update(['amount' => $totalValue]);
}
$order->products()->sync($values); //the problem is here
$orders = Order::all();
$orders->load('client', 'products');
return view('orders/index', compact('orders'));
}
The issue with it, is that sync is getting wrong IDs, because $values has numeric autoincrementals IDs: 0, 1, 2, etc.
What you need to pass to the sync is either an array of IDs [1, 2, 3], or an array of arrays (when you want to update columns related to that ID, for example: [1 => ['amount' => 100], 2, 3].
Let me try to modify your code to help you.
Let's assume you orders table and products table. You also have a pivot table called order_product table, so you store the relation in there. This order_product table has:
id
order_id
product_id
amount
So, your code should be like this:
public function update(Request $request, $id)
{
$validated = $request->validate([
'order_number' => 'required',
'client_id' => 'required',
'description' => 'required',
'productOrder' => 'required',
'productOrder.*.product_id' => 'required|distinct|exists:products,id',
'productOrder.*.amount' => 'required|numeric|min:1',
]);
$order = Order::find($id); // I would change this to use implicit binding
$order->update($validated); // Don't use $request->all(), you are using unvalidated values
foreach ($request->productOrder as $product) {
$values[$product['product_id']] = [
'amount' => $product['amount']
];
$amount = Product::find($product['product_id']);
$totalValue = $product['amount'] + $amount->amount;
$amount->update(['amount' => $totalValue]);
}
$order->products()->sync($values); // Now it will work
$orders = Order::all();
$orders->load('client', 'products');
return view('orders/index', compact('orders'));
}

Related

How to get from request->value one value at a time inside foreach loop

Hello i have this code in laravel controller and i get an error for a single value:
public function store(Request $request)
{
$values = [];
$request->validate([
'order_number' => 'required',
'client_id' => 'required|exists:clients,id',
'description' => 'required',
'products' => 'required|exists:products,id',
'amount' => 'required',
]);
$order = Order::create($request->all());
foreach ($request->products as $product) {
$values[] = [
'order_id' => $order->id,
'product_id' => $product,
'amount' => $request->amount,
];
$amount = Product::find($product);
$total_value = $request->amount + $amount->amount; //the error happens here
$amount->update(['amount' => $total_value]);
}
$order->products()->attach($values);
return redirect('/')->with('msg', 'Order Saved successfully!');
}
All the values come except the $request->amount that comes as array and not as a single value in a row. This is the error i get:
Unsupported operand types: array + string
This is the product model:
protected $fillable = [
'name',
'price',
'amount',
];
public function orders()
{
return $this->belongsToMany(Order::class);
}
And this is dd($request->amount);
Assuming that $request->amount is directly related to $request->products with the index then you would either need to combine products and amount before you send the request or iterate over products with the index.
foreach ($request->products as $index => $product) {
$values[] = [
'order_id' => $order->id,
'product_id' => $product,
'amount' => $request->amount[$index], //Reference via index
];
$amount = Product::find($product);
$total_value = $request->amount[$index] + $amount->amount; //Also here
}
}

How to use sync eloquent method

Hello i am trying to make a code that updates existing values by removing existing ones/add new ones or make changes in the existing values. This is the code i have done so far:
public function update(Request $request, $id)
{
$order = Order::find($id);
$request->validate([
'order_number' => 'required',
'client_id' => 'required',
'description' => 'required',
'productOrder' => 'required',
'productOrder.*.product_id' => 'required|distinct|exists:products,id',
'productOrder.*.amount' => 'required|numeric|min:1',
]);
$order->update($request->all());
foreach ($request->productOrder as $product) {
$values[] = [
'order_id' => $order->id,
'product_id' => $product['product_id'],
'amount' => $product['amount'],
];
$amount = Product::find($product['product_id']);
$totalValue = $product['amount'] + $amount->amount;
$amount->update(['amount' => $totalValue]);
// $order->products()->sync([$product['product_id'] => array(
// 'product_id' => $product['product_id'],
// 'amount' => $product['amount'], THIS CODE MAKES ERROR BY DELETING ALL THE VALUES EXCEPT ONE
// )]);
}
$order->products()->detach();
$order->products()->attach($values); //I WANT THE CODE TO DO THIS FUNCTIONS BASICALLY
$orders = Order::all();
$orders->load('client', 'products');
return view('orders/index', compact('orders'));
}
I think you should use this
$values = [];
foreach ($request->productOrder as $product) {
/* This is the sync id && This is the pivot column */
$values[$product['product_id']] = ['amount' => $product['amount']];
$amount = Product::find($product['product_id']);
$totalValue = $product['amount'] + $amount->amount;
$amount->update(['amount' => $totalValue]);
}
// $values keys must be a product_id and its value must be pivot values
$order->products()->sync($values);

Laravel 8 factories

Can someone tell me how can I make a factory with relationships etc...
I have a post table with 2 foreign keys: user_id and category_id
I need to generate dummy data but I don't know how to do it.
I have tried to make categories first then to do something with posts and users but did not work.
PostFactory:
public function definition()
{
$title = $this->faker->sentence;
$slug = Str::slug($title);
return [
'title' => $title,
'slug' => $slug,
'image' => $this->faker->imageUrl(900, 300),
'content' => $this->faker->text(300),
];
}
CategoryFactory:
public function definition()
{
$category = $this->faker->words(2, true);
$slug = Str::slug($category);
return [
'category' => $category,
'slug' => $slug
];
}
And user factory is just default one :)
You can check if you have enough records, and query the DB to find a random User and Category to use on each Post. But if there not enough records (20 Users and 7 Categories), create a new one.
PostFactory:
public function definition()
{
$title = $this->faker->sentence;
$slug = Str::slug($title);
$user = User::count() >= 20 ? User::inRandomOrder()->first()->id: User::factory();
$category = Category::count() >= 7 ? Category::inRandomOrder()->first()->id: Category::factory();
return [
'title' => $title,
'slug' => $slug,
'image' => $this->faker->imageUrl(900, 300),
'content' => $this->faker->text(300),
'user_id' => $user,
'category_id' => $category,
];
}

adding products in different column in laravel

Hi guys I want to add products in differents column in database, however all I have done it was inserting all products in same column. thanks
my code:
$products = $request->all();
foreach( $products as $product) {
$invoice->products([
'name' => $product['name'],
'price' => $product['price'],
'qty' => $product['qty'],
'total' => $product['total']
]);
}
results
database shot
sorry new here
$products = $request->all();
$name = $products['name'];
$price = $products['price'];
$qty = $products['qty'];
$total = $products['total'];
foreach( $name as $key => $n) {
$invoice->products()->create([
'invoice_id' => $invoice->id,
'name' => $name[$key],
'price' => $price[$key],
'qty' => $qty[$key],
'total' => $total[$key]
]);
}

Decrease product quantity in database after checkout

I have cart on my web store and so far everything works perfectly but I said when I try decrement the product quantity to product, the all product in my database will decrement Not just products in cart.
Here is my ordercontroller :
public function postOrder(Request $request) {
$validator = Validator::make($request->all(), [
'first_name' => 'required|max:30|min:2',
'last_name' => 'required|max:30|min:2',
'address' => 'required|max:50|min:4',
'address_2' => 'max:50|min:4',
'city' => 'required|max:50|min:3',
'state' => 'required|',
'zip' => 'required|max:11|min:4',
'full_name' => 'required|max:30|min:2',
]);
if ($validator->fails()) {
return redirect('/checkout')
->withErrors($validator)
->withInput();
}
$first_name = Input::get('first_name');
$last_name = Input::get('last_name');
$address = Input::get('address');
$address_2 = Input::get('address_2');
$city = Input::get('city');
$state = Input::get('state');
$zip = Input::get('zip');
$full_name = Input::get('full_name');
$user_id = Auth::user()->id;
$cart_products = Cart::with('products')->where('user_id', '=', $user_id)->get();
$cart_total = Cart::with('products')->where('user_id', '=', $user_id)->sum('total')
$charge_amount = number_format($cart_total, 2) * 100;
$order = Order::create (
array(
'user_id' => $user_id,
'first_name' => $first_name,
'last_name' => $last_name,
'address' => $address,
'address_2' => $address_2,
'city' => $city,
'state' => $state,
'zip' => $zip,
'total' => $cart_total,
'full_name' => $full_name,
));
foreach ($cart_products as $order_products) {
$order->orderItems()->attach($order_products->product_id, array(
'qty' => $order_products->qty,
'price' => $order_products->products->price,
'reduced_price' => $order_products->products->reduced_price,
'total' => $order_products->products->price * $order_products->qty,
'total_reduced' => $order_products->products->reduced_price * $order_products->qty,
));
}
// in the fact all product will decrement Not just products in cart
\DB::table('products')->decrement('product_qty', $order_products->qty);
Cart::where('user_id', '=', $user_id)->delete();
flash()->success('Success', 'Your order was processed successfully.');
return redirect()->route('cart');
}
I use program \DB::table('products')->decrement('product_qty', $order_products->qty);; for the decrement but in the fact all product will decrement Not just products in cart
The decrement you are using updates all records, as you have observed because you didn't constrain it to a specific record. You want to do this in your loop:
DB::table('products')
->where('id', '=', $order_products->product_id)
->decrement('product_qty', $order_products->qty);

Resources