1

I am using Laravel 5. I need to save some sql statements in a field of a table of the database (these statements are used to get some results). I want to get these statements in my controller and execute them using Laravel, but the statements are only strings, Let's suppose the following

$statement = Table::where('ID', 1);
$statement = $statement->STATEMENT;

In $statement I have a string like this

$statement = 'SELECT SUM(VAL) FROM TABLE';

What I need to know is how to execute in the database the statement saved in my string var $statement I finally want to have something like

$result = 10 (the result of executing 'SELECT SUM(VAL) FROM TABLE', which was in $statement)

Thanks!

2
  • So do you know how read the statement but dont how execute it? Commented Sep 15, 2015 at 19:35
  • Yeah, that is. I don't know how to execute my $statement :-) Commented Sep 15, 2015 at 19:37

1 Answer 1

3

This is called raw queries in laravel. For example:

DB::select(DB::raw('select * from users''));

So in your case(if You've already got $statement):

DB::select(DB::raw($statement));
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.