R
R
Rishat Sultanov2017-03-24 21:53:09
MySQL
Rishat Sultanov, 2017-03-24 21:53:09

What is the correct way to use a raw expression for a query?

Good afternoon, ladies and gentlemen.
Actually, I would like to ask.
How would you implement a raw table query expression? (Laravel)
In my case there is a table. And it has n number of entries.
What is the correct way to use the query builder to display the number of records in a table without creating possible SQL injection points?
I'm trying to do according to the documentation:
Swears at

Call to undefined method Illuminate\Database\Query\Builder::table()

Raw expression according to the documentation.
$loler = Kyrpost::table('kyrposts')
            ->select(Kyrpost::raw('count(*) from kyrposts'))
            ->get();

It seems that the documentation in Russian was not updated or something is crooked for me :)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
D3lphi, 2017-03-24
@rishatss

You are trying to call the table() method on a model. Although, the model is already bound to the table. Use:

Kyrpost::select(\DB::raw('count(*) from kyrposts'))
            ->get();

or:
\DB::table('kyrposts')
            ->select(\DB::raw('count(*) from kyrposts'))
            ->get();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question