0

I want to get all results from two tables akcii, feeds and after search throw them (now query just returns all records without any conditions for search), here is the query (mysql):

DB::query('SELECT id, title, text FROM (
        SELECT id, title, text FROM `akcii` UNION ALL
        SELECT id, title, body FROM `feeds`
    ) temp_table
        ORDER BY `id` desc
        LIMIT 0, 10')

Results are exacly that I need but after I can't convert it Collection? If I call ->get() method I'm getting an error:

QueryException in Connection.php line 651:
SQLSTATE[HY000]: General error: 1096 No tables used (SQL: select *)

What am I doing wrong?

2
  • 1
    In phpmyadmin this query return records well. Commented Dec 15, 2015 at 8:42
  • 1
    I'm using Mysql 5.5.41 Commented Dec 15, 2015 at 8:42

1 Answer 1

2

DB::query() method does not get any arguments, so the query you pass there is simply ignored. You're calling get() on an empty builder, which does not know what table to run the query on. Generated query is just SELECT *, hence the SQL error.

If you want that to work, you should call DB::select() method, e.g.:

$results = DB::select($your_query);

Still, what you'll get is an array, not a Collection object. If you want to make a collection out of it, do the following:

$collection = new Collection($results);
Sign up to request clarification or add additional context in comments.

8 Comments

Can you show us the API documentation where you see that DB::query() takes no arguments?
Connection is the class behind DB facade
Thanks, works, but now, cant use paginate() method.
You can't have Eloquent do the pagination on database level if you're using a raw query. Query needs to be built using query builder so that Eloquent knows how to apply pagination criteria.
|

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.