Querying

Select

To generate a simple select statement, you can use the select() method within the model.

namespace Application\Models;
use MightyCore\Database\Model;
class UsersModel extends Model
{
public function get(){
return $this->select('name', 'age')->get();
}
}

The select method can be open for SQL injection. Do not use any dynamic or request injected values or strings directly into the select method. To protect from threats, it is important that select be used with care.

Optional Select

There are times where it is tedious to write select() on most queries. To ensure productivity, selects are optional in which it would default to return all columns of the table.

namespace Application\Models;
use MightyCore\Database\Model;
class UsersModel extends Model
{
public function get(){
// This would return all data in this table.
return $this->get();
}
}

Using it with where:

namespace Application\Models;
use MightyCore\Database\Model;
class UsersModel extends Model
{
public function get(){
return $this->where('name', 'Bob')->get();
}
}

Joins

To join tables, simply use the join methods:

  • leftJoin()

  • outerJoin()

  • rightJoin()

  • innerJoin()

namespace Application\Models;
use MightyCore\Database\Model;
class UsersModel extends Model
{
/*
* This will left join profiles on its userId column
* to the userId column of users table.
*/
public function getWithProfile(){
return $this->where('name', 'Bob')
->leftJoin('profiles', 'userId', '=', 'userId')
->get();
}
}