@ene your description of the problem is too vague to give you appropriate suggestions; please expand.
withdraw deduction
i have a withdraw model and point model both have relationship to users , i will like when a user fill the withdrawal form by inserting the amount they will like to request i want deduct the user point when the point is been submitted
how will i do this please i will love your help
class RequestWithdraw extends Component
{
public $withdraw;
public $showModal = false;
public $withdrawId;
public $image;
protected $rules = [
'withdraw.bank' => '',
'withdraw.account_name' => '',
'withdraw.account_number' => '',
'withdraw.amount' => '',
];
public function edit($withdrawId)
{
// $this->showModal = true;
$this->withdrawId = $withdrawId;
$this->withdraw = Withdraw::find($withdrawId);
}
public function create()
{
// $this->showModal = true;
$this->withdraw = null;
$this->withdrawId = null;
}
// public function save()
// {
// $this->validate([
// 'image' => 'image|max:1024', // 1MB Max
// ]);
// if (!is_null($this->withdrawId)) {
// $path = $this->image->storeAs('withdraw', str::random(30));
// $this->withdraw->image = $path;
// $this->withdraw->save();
// }
// $this->showModal = false;
// }
// public function close()
// {
// $this->showModal = false;
// }
public function render()
{
$users = Withdraw::with(['user'])->where('user_id', auth()->user()->id)->get();
return view('livewire.user.request-withdraw',[
'users' => $users,
]);
}
}
@ene I guess I don't understand why is there a Withdraw model at all?
Anyway using Model events, you can hook into the created event on a Withdraw model and update the Point model from there. There are a number of ways to hook into this event; check out the documentation for Model Events, and Observers. The simplest way is a Closure on the Withdraw model, e.g.
public static function booted()
{
static::created(function ($withdraw) {
Point::where('user_id', $withdraw->user_id)->decrement('points', $withdraw->amount);
});
}
So, this is assuming there is one Point record for each user, and you are decrementing the points property by the amount.
Please or to participate in this conversation.