I am working on a website and i needed to record the contributions of members of that organisation in the database. And usually the logged in user can be different from the donor. However, each time I add a new contribution, it records the logged user id in the DB instead of the donor’s id. I have used dd($request->donor_user_id)
in the controller to see the id being sent to the controller and it matches that of the donor. However, when I save it to the DB, I see the logged in use id instead.
Everything looks fine for me,yet it does not work. Please what could be causing this issue? See codes below.
The migration/table
public function up(): void
{
Schema::create('church_parishioner_support_incomes', function (Blueprint $table) {
$table->id();
$table->integer('cause_id');
$table->integer('donor_id');
$table->integer('is_org_member')->default(0);
$table->integer('organisation_id')->default(1);
$table->integer('committee_id')->nullable();
$table->integer('subactivity_id')->nullable();
$table->float('amount');
$table->float('paid');
$table->float('balance');
$table->string('comment')->nullable();
$table->integer('created_by');
$table->timestamps();
});
The controller
public function store(Request $request, String $slug){
$request->validate([
'cause_id'=>['required', 'integer'],
'donor_user_id'=>['required', 'integer'],
'amount'=>['required', 'numeric'],
'organisation_id'=>['required', 'integer'],
'paid'=>['required', 'numeric'],
'balance'=>['required', 'numeric'],
'comment'=>['nullable'],
]);
// dd($request->donor_user_id);
$user=User::findOrFail($request->user);
$income = new ChurchParishionerSupportIncome();
$income->donor_id=$request->donor_user_id;
$income->cause_id=$request->cause_id;
$income->is_org_member=1;
$income->amount=$request->amount;
$income->paid=$request->paid;
$income->balance=$request->balance;
$income->comment=$request->comment;
$income->organisation_id=$request->organisation_id;
$income->created_by=$user;
$income->save();
toastr('Income saved successfully', 'success');
return redirect()->route('org.finance.orgmembers-supports.income',['slug'=>$slug, 'user'=>$user]);
}
The route
Route::post('/portal/dashboard/organisation/{slug}/finances/members/members-support-incomes/store', [OrgMembersSupportIncomeController::class, 'store'])->name('org-finance.members-supports.income.store');
The View
<section class="section">
<div class="mb-3">
<a href="{{ route('org.finance.orgmembers-supports.income', [$organisation->slug, 'user'=>$user->id]) }}" class="btn bg-primary custom-txt-bcolor"><i class="fa fa-arrow-left"></i>
Go Back</a>
</div>
<div class="row">
<h5>{{ $income?"Edit Members Support Income Record":"Add Members Support Incomes" }}</h5>
<div class="col-12 col-md-12 col-lg-12">
<div class="card card-primary">
<div class="card-body">
@if($income !==null)
<form action="{{ route('org-finance.members-supports.income.update', [$organisation->slug, $income->id, 'user'=>$user->id] ) }}" method="POST" >
@method('PUT')
@else
<form action="{{ route('org-finance.members-supports.income.store', [$organisation->slug, 'user'=>$user->id]) }}" method="POST" >
@endif
@csrf
<div class="form-group">
<label>Project/Cause</label>
<select class="form-control select2" name="cause_id">
<option value="">Select project/cause</option>
@foreach ($causes as $cause )
<option {{ $income?$income->cause_id==$cause->id?"selected":"":"" }} value="{{ $cause->id }}">{{ $cause->name }}</option>
@endforeach
</select>
</div>
<div class="form-group">
<label>Member</label>
<select class="form-control select2" name="donor_user_id">
<option value="">Select member</option>
@foreach ($registeredMembers as $member )
<option {{ $income?$income->user_id==$member->user_id?"selected":"":"" }} value="{{ $member->user_id }}">{{ $member->user->first_name }} {{ $member->user->middle_name?$member->user->middle_name:"" }} {{ $member->user->last_name }}</option>
@endforeach
</select>
</div>
<div class="form-group">
<label>Amount donated (N)</label>
<input name="amount" type="number" class="form-control donated_amt" value="{{ $income?$income->amount:"" }}">
</div>
<div class="form-group">
<label>Paid (N)</label>
<input name="paid" type="number" class="form-control paid_amt" value="{{ $income?$income->paid:"" }}">
</div>
<div class="form-group">
<label>Balance (N)</label>
<input name="balance" type="number" class="form-control balance_amt" value="{{ $income?$income->balance:"" }}" readonly>
</div>
<div class="form-group">
<label>Comment</label>
<div class="form-floating">
<textarea name="comment" class="form-control" placeholder="Leave a comment here" id="floatingTextarea2" style="height: 100px">{!! $income?$income->comment:"" !!}</textarea>
</div>
</div>
<input name="organisation_id" type="hidden" class="form-control" value="{{$organisation->id}}" readonly>
<button type="submit" class="btn btn-primary btn_submit">{{ $income?"Update":"Save" }}</button>
</form>
</div>
</div>
</div>
</div>
</div>