Have you tried putting ->get(); at the end?
Can't get DB::raw query to work within the select method of another query
Background
I have a project that manages the finances of certain clients, in the app, I have a Loan, Account, and LoanStatus models in relationship such that Account hasMany loans and a LoanStatus belongsTo a Loan
Account Schema
Schema::create('accounts', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('user_id'); # as registrant
$table->string('account_number', 9)->unique(); #AC2347521
$table->string('firstname', 50);
$table->string('lastname', 50);
...
$table->softDeletes();
$table->timestamps();
$table->engine = 'InnoDB';
Loan Schema
Schema::create('loans', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('loan_account_id', 9)->unique(); #LN2347521
$table->unsignedBigInteger('user_id');
$table->bigInteger('account_id')->unsigned();
$table->bigInteger('amount')->unsigned();
$table->bigInteger('amount_with_interest')->unsigned();
$table->smallInteger('interest');
$table->smallInteger('loan_tenure'); #weeks
$table->dateTime('expiry_date');
$table->enum('repayment_frequency', ['daily', 'weekly', 'monthly']);
$table->bigInteger('amount_per_repayment');
$table->enum('ledger_side', ['credit', 'debit'])->default('debit');
$table->string('loan_record_token', 50)->unique();
$table->enum('times', ['first', 'subsequent']);
$table->boolean('disbursed')->default(false);
$table->boolean('completed')->default(false);
$table->softDeletes();
$table->timestamps();
$table->engine = 'InnoDB';
Loan Status Schema
Schema::create('loan_statuses', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('account_id');
$table->unsignedBigInteger('loan_id');
$table->unsignedBigInteger('loan_amount');
$table->unsignedBigInteger('total_paid')->default(0);
$table->softDeletes();
$table->timestamps();
Can anyone tell me why the query below returns an empty result set when the last line is present DB::raw("(loan_statuses.loan_amount - loan_statuses.total_paid) AS amount_remaining")
Loan::join('accounts', 'loans.account_id', '=', 'accounts.id')
->join('loan_statuses', 'loans.id', '=', 'loan_statuses.loan_id')
->whereDate('loans.created_at', '>=', Carbon::parse($fromDate))
->whereDate('loans.created_at', '<=', Carbon::parse($toDate))
->select([
'loans.id',
'loans.loan_account_id',
'loans.amount',
'loans.interest',
'loans.completed',
'loans.expiry_date',
'loans.amount_with_interest',
DB::raw('loans.created_at as loan_created_at'),
'accounts.account_number',
'accounts.lastname',
'accounts.firstname',
DB::raw("CONCAT(accounts.lastname,' ',accounts.firstname) AS fullname"),
'loan_statuses.total_paid',
DB::raw("(loan_statuses.loan_amount - loan_statuses.total_paid) AS amount_remaining")
]);
please note that the database already has data and only the last column amount_remaining makes the query empty. I also have that exact column query on a different query with no problem
i think that if no error usually meant the syntax is work, if result is empty then the condition input may the issue, especially the date format...
try print out the rendered sql statement, check and try it out at db side...
Please or to participate in this conversation.