I’m sure it’s very simple but I couldn’t find any examples.
I am using fluent in my Laravel application to log my users’ logins and so I have created my own auth driver, however I am having a problem with a fluent query that mixes raw data and none raw, look at the following:
DB::table('logins')->insert(array(
'login_email' => $arguments['email'],
'login_date' => DB::raw('UNIX_TIMESTAMP(NOW())'),
'login_ip'=> DB::raw('INET_ATON('.$_SERVER['REMOTE_ADDR'].')'),
'login_result' => (bool)$success
));
which causes:
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '@gmail.com, UNIX_TIMESTAMP(NOW()), INET_ATON(127.0.0.1), ?)' at line 1
SQL: INSERT INTO `cs_logins` (`login_email`, `login_date`, `login_ip`, `login_result`) VALUES (?, UNIX_TIMESTAMP(NOW()), INET_ATON(127.0.0.1), ?)
Bindings: array (
0 => false,
)
so I did the following:
DB::table('logins')->insert(array(
'login_email' => DB::raw("'".$arguments['email']."'"),
'login_date' => DB::raw('UNIX_TIMESTAMP(NOW())'),
'login_ip'=> DB::raw('INET_ATON('.$_SERVER['REMOTE_ADDR'].')'),
'login_result' => DB::raw((bool)$success)
));
But as Dayle Rees says, if it looks ugly it’s not in the framework… so I want to know what I am missing here.
Use eloquent
First of all, if not already done, create a model for Login, so you can use laravel’s own ORM eloquent. Eloquent allows you to write simple and yet powerful queries in a very expressive way.
Now you can do one of these two options:
or, maybe more elegant in your case:
See the eloquent docs for more information.