I try to learn Laravel 5.5, but I have problem to use DB;
DB Table have records.
Following code return some records.
$result = DB::select(DB::raw("select * from todays where FROM_UNIXTIME(login, '%Y/%m/%d') = '2019/03/04'"));
But following code doesn't return any records. What is wrong this ?
$result = DB::table('todays')->where(DB::raw("FROM_UNIXTIME(login, '%Y/%m/%d')", DB::raw('2019/03/04')))->get();
Please help me.
Thanks
Your query seems to be wrong.
$result = DB::table('todays')->where(DB::raw("FROM_UNIXTIME(login, '%Y/%m/%d')"), DB::raw('2019/03/04'))->get();
or
$result = DB::table('todays')->where(DB::raw("FROM_UNIXTIME(login, '%Y/%m/%d')"), '2019/03/04')->get();
First one is more of an error-prone while the second one is used as pdo statement.
Related
I've tried this code:
//controller
$model = new Mymodel;
$table = $model->getTable();
$columns = Schema::getColumnListing($table);
//view.blade.php
{{print_r($columns)}}
but the result only give this output: Array ( [0] => * ) 1
More Information:
I need to connect to SQL Server database with Forticlient VPN. I think it shouldn't be the problem. But, tell if there's something I've missed.
After struggling looking for any answer, and looking for answer generally about how to get the column name straight from SSMS, I've made my temporary own solution. Here's the code:
$model = new Mymodel;
$table = $model->getTable();
$schema = DB::table('INFORMATION_SCHEMA.COLUMNS')
->select('COLUMN_NAME')
->where("TABLE_NAME", $table)
->get();
My own answer is only improvement from the base query to get column name straight from the SSMS, here the query:
select COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS
where TABLE_NAME = 'mytable_name'
In the old cakePHP 2.X I used this statement:
$this->Products->query('TRUNCATE TABLE products;');
which was the only way I could get my ids reset to 1. Now in cakephp 3.X this does not seem to truncate the table at all.
I have tried:
deleteAll();
Which works but seems to always need a condition but most importantly does not reset the ID;
query()->delete()->execute();
Deletes everything but does not reset the ids.
Does anyone know how to accomplish this in Cakephp 3.2
Include "ConnectionManager" namespace in the controller
use Cake\Datasource\ConnectionManager;
Create connection string and execute query in your action
$connection = ConnectionManager::get('default');
$results = $connection->execute('TRUNCATE TABLE tableName');
The way you can execute arbitrary queries in CakePHP3 is:
$connection = \Cake\Datasource\ConnectionManager::get('default');
$connection->execute('TRUNCATE TABLE products');
http://book.cakephp.org/3.0/en/orm/database-basics.html#database-basics
I am using laravel.In my controller I used a query(given below).Is there any modified query for this?? Or is it okk???
$sub = Studentacademicinfo::where('class','=',$cat)->where('section','=',$section)->get();
You could do this if you wanted to tidy up the code:
$sub = Studentacademicinfo::whereClass($cat)->whereSection($section)->get();
But the resulting SQL will be the same
As show at CakePHP 2.1.x - Run a query without any models in AppController
I have a query,
$q = "select id from table where id=123";
$db = ConnectionManager::getDataSource('default');
$qr = $db->rawQuery($q);
ok (!), it works... But, how to get my data?? Where the tutorial examples?
I need something like $data = $qr->fetchAll() method or $id = getMyData($qr) function.
I believe this may be the solution or at least a point in the right direction.
$q = "select id from table where id=123";
$db = ConnectionManager::getDataSource('default');
$myData = $db->query($q);
I'm trying to figure out how to have the execute method of an ZF2 SQL Statement class to return an array of objects rather than an array of arrays. The documentation/reference seems to lack severely in this area and the code isn't documented properly.
This is what I have (I'm using the PDO MySQL driver but that should not be relevant):
$sql = new Sql( $db );
$query = $sql->select()
->from('users')
->where('id > 1');
$stmt = $sql->prepareStatementForSqlObject( $query );
$results = $stmt->execute();
$results is now an array of arrays but I need it to be an array of objects instead. How can I specify this?
I think that most people use doctrine for ORM
http://www.jasongrimes.org/2012/01/using-doctrine-2-in-zend-framework-2/