0

I tried to write custom query in model as local scope:

public function scopeLastSync($query)
    {

        return DB::table('clients')
            ->select(DB::raw(
                "select COUNT(*) as total, (SELECT created_at FROM clients ORDER BY created_at DESC LIMIT 1)"
            ))->get();
    }

But it does not work. How to use easy: DB::query("native SQL query")? I tried:

return DB::raw("select COUNT(*) as total, (SELECT created_at AS date_modify FROM clients ORDER BY created_at DESC LIMIT 1)");

1 Answer 1

4

May be you are searching for this, Write Custom query in laravel 5

For Select --> DB::select('your query here.....');
For Insert --> DB::insert('your query here.....');
For Update --> DB::update('your query here.....');
For Delete --> DB::delete('your query here.....');
For General -->  DB::statement( 'your query here....' );

You don't have to write select inside select(DB::raw(...)). Try:

return DB::table('clients')
            ->select(DB::raw(
                "COUNT(*) as total, (SELECT created_at FROM clients ORDER BY created_at DESC LIMIT 1)"
            ))->get();
    }

But why to do you want to write query of your own if there is powerful Eloquent Model.

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.