Answer the question
In order to leave comments, you need to log in
Am I using the controller correctly or should this code be in the models?
This code is in the controller, is this correct, or do I need to create a method in the models and pass values there?
public function buyProduct($id){
$product = Product::where('id',$id)->get();
$userId = Auth::user()->id;
$sellerUserId = $product->first()->seller_user_id;
$sellerUserIdBalance = User::where('id',$sellerUserId)->get()->first()->balance;
$productOwnerId = $product->first()->owner_user_id;
$productPrice = $product->first()->price;
$userBalance = User::where('id',$userId)->get()->first()->balance;
if($userId == $productOwnerId){
$response = [
'response' => 'You cant buy your product'
];
return response($response,201);
}
elseif ($product->first()->status_id == SOLD){
$response = [
'response' => 'This product is sold'
];
return response($response,201);
}
elseif ($productPrice >= $userBalance ){
$response = [
'response' => 'Not enough money'
];
return response($response,201);
}
else{
Product::where('id', $id)->update(array('owner_user_id' => $userId));
Product::where('id', $id)->update(array('buyer_user_id' => $userId));
Product::where('id', $id)->update(array('status_id' => SOLD));
User::where('id', $userId)->update(array('balance' => $userBalance-$productPrice));
User::where('id', $sellerUserId)->update(array('balance' => $sellerUserIdBalance+$productPrice));
}
return Product::where('id',$id)->get();
}
Answer the question
In order to leave comments, you need to log in
public function buyProduct($id){
$product = Product::where('id',$id)->get();
$userId = Auth::user()->id;
$sellerUserId = $product->first()->seller_user_id;
$sellerUserIdBalance = User::where('id',$sellerUserId)->get()->first()->balance;
User::where('id', $sellerUserId)->update(array('balance' => $sellerUserIdBalance+$productPrice));
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question