0

I am using Laravel Framework 6.16.0.

I want to select data via sql functions, such as CONCAT('Q', QUARTER(transaction_date)).

However, I get an error:

Column not found: 1054 Unknown column 'CONCAT('Q', QUARTER(transaction_date))' in 'field list'

The query looks like the following:

$days = 365 * 2; // two years

$trx = DB::connection('mysql_prod')->table('product')->select(array("transaction_date", "CONCAT('Q', QUARTER(transaction_date))", "YEAR(transaction_date)", "trx_type", "amount_range", "transaction_value", "symbol"))
    ->leftJoin('companies', 'product.companies_id', '=', 'companies.id')
    ->leftJoin('persons', 'persons.id', '=', 'product.persons_id')
    ->where('companies.symbol', '=', $s->symbol)
    ->where('persons.person_type', '=', 'manager')
    ->whereDate('transaction_date', '>=', Carbon::now()->subDays($days))
    ->orderBy('transaction_date', 'desc')
    ->get();

Any suggestion how to use SQL-functions within an eloquent select statement?

Appreciate your reply!

1
  • 2
    you would need to use a DB::raw for the CONCAT stuff as it thinks you are trying to select 'columns' with select Commented Dec 16, 2020 at 22:15

1 Answer 1

1

As @lagbox stated in his comment,

You should change "CONCAT('Q', QUARTER(transaction_date))" to DB::raw("CONCAT('Q', QUARTER(transaction_date))")

$days = 365 * 2; // two years

$trx = DB::connection('mysql_prod')->table('product')->select(array("transaction_date", DB::raw("CONCAT('Q', QUARTER(transaction_date))"), "YEAR(transaction_date)", "trx_type", "amount_range", "transaction_value", "symbol"))
    ->leftJoin('companies', 'product.companies_id', '=', 'companies.id')
    ->leftJoin('persons', 'persons.id', '=', 'product.persons_id')
    ->where('companies.symbol', '=', $s->symbol)
    ->where('persons.person_type', '=', 'manager')
    ->whereDate('transaction_date', '>=', Carbon::now()->subDays($days))
    ->orderBy('transaction_date', 'desc')
    ->get();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.