Laravel Eloquent - Select all columns plus a subquery using Eloquent only

Ajith Gopi

I need to select all columns from a table and an additional columns using subquery. For example,

SELECT *, (SELECT COUNT(*) FROM transactions WHERE transactions.customer=customers.id) AS transactions FROM customers

Right now, I have come up with using selectRaw like

$customers = Customer::selectRaw('*, (SELECT COUNT(*) FROM transactions WHERE transactions.customer=customers.id) AS transactions')->get();

But I'd like to do it the eloquent way without using raw queries.

HijenHEK

as i see here you have 1-N relation between customer and transaction , if you have set up your models with eloquent relationships belongTo and hasMany , then you can use the With for eager load subqueries :

Eloquent eager load and eager load count relation

//customers + transactions for each one
$customers = Customer:with('transactions')->get();

// customers + transaction count for each one
$customers = Customer:withCount('transactions')->get(); 

Customer Model

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Customer extends Model
{
    /**
     * Get the transactions for the customer.
     */
    public function transactions()
    {
        return $this->hasMany(Transaction::class);
    }
}

Transaction Model

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Transaction extends Model
{
    /**
     * Get the customer of this transaction.
     */
    public function customer()
    {
        return $this->belongsTo(Customer::class);
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Laravel Eloquent WHERE field=(...) subquery

laravel eloquent select dynamic

How to select specific columns in laravel eloquent

Get Specific Columns Using “With()” Function in Laravel Eloquent

How to create a subquery using Laravel Eloquent?

Concat columns using “with()” function in Laravel Eloquent

How to write select query with subquery using laravel Eloquent Querybuilder?

Laravel Eloquent with two “WHERE NOT IN” in subquery

Subquery in Laravel Eloquent

How to return an array using Laravel Eloquent select?

Fill laravel select with eloquent model using pluck

Select in eloquent join laravel

groupBy('created_at') using subquery in laravel eloquent

Laravel eloquent using null as column value on select?

Use a subquery in laravel eloquent with group by

Adding a count column for a filtered relationship to a select with a subquery in Laravel Eloquent

Laravel 5.2 eloquent eager loading select columns

Select and count using Laravel Eloquent

Laravel Eloquent subquery and count subquery results

Select all but the most recent row in a database using Eloquent in Laravel

Laravel join only using Eloquent

laravel 5 - Select all columns without asterix when joining tables with eloquent or query builder

Is there a way to select columns using eloquent one to many relationship in laravel 5.2?

Laravel eloquent select data only specific term

Get specific columns using “only()” function in Laravel Eloquent

Creating a subquery using laravel eloquent

Laravel Eloquent - Unable to fetch columns using array

Select from select using Laravel eloquent

Laravel Eloquent Subquery with condition