1

It happens I'm doing a query and still not have the field of condition (where ) and found that maybe you can do it this way

$results = DB::select(
    DB::raw('select * from some_table WHERE some_id = ?'),
    array($id)
); 
var_dump($results);

But not how to use it, can someone explain me please?

3
  • $results = DB::table('some_table')->where('some_id', '=', 12)->get(); Commented Mar 17, 2016 at 17:49
  • Now if you want to check the id in an array: $results = DB::table('some_table')->whereIn('some_id', '=', $idArray)->get(); Commented Mar 17, 2016 at 17:50
  • You could use Eloquent - but as you have posted a raw query this should work: $results = DB::select(DB::raw('select * from some_table WHERE some_id = :someid'), array('someid' => $id)); var_dump($results); Commented Mar 17, 2016 at 17:52

3 Answers 3

1
$results = DB::table('some_table')->where('some_id', '=', 12)->get();
//This is to check for an id.


$idArray = [1, 2, 3, 4];

$results = DB::table('some_table')->whereIn('some_id', '=', $idArray)->get();
//checks for id in an array
Sign up to request clarification or add additional context in comments.

Comments

1

If you prefer use raw query:

$results = DB::select(
    DB::raw('select * from some_table WHERE some_id = '.$id)
); 

Although I would recommend using raw queries if you don't have to.

Comments

1

It looks to be an issue with how you're sanitizing your variable(s) - I don't know much about Laravel but looking at documentation it is pointing towards this approach:

$results = DB::select(DB::raw('select * from some_table WHERE some_id = :someid'), array('someid' => $id)); var_dump($results);

Also, are you sure you need to select all columns?

Just bear in mind when choosing between eloquent & raw - if you have a complicated query returning a large data set Eloquent could be a big performance hit, otherwise Eloquent is a dream!

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.