Querybuilder

The querybuilder is a tool to write sql query's more efficient. To write sql query's alternatively, use the base model methods.

Fetching data

There is no limit on the number of column names you can use as arguments.

<?php
            
  use database\DB;
             
  DB::try()->select('column', 'column')->from('table')->fetch();

Fetching data but only the first collection calling the first function method.

<?php
            
  use database\DB;
             
  DB::try()->select('column', 'column')->from('table')->first();

Fetching data but a bit shorter calling the all function method.

<?php
            
  use database\DB;
             
  DB::try()->all('table')->fetch();   

Fetching data but on a condition using the where function method.

<?php
            
  use database\DB;
             
  DB::try()->select('column', 'column')->from('table')->where('column', 'operator', 'value')->fetch();

Fetching data but on a double condition using the where function method and and.

<?php
            
  use database\DB;
              
  DB::try()->select('*')->from('table')->where('column', 'operator', 'value')->and('column', 'operator', 'value')->fetch();

Fetching data but on a negative condition by calling the whereNot function method.

<?php
            
  use database\DB;
              
  DB::try()->select('*')->from('table')->whereNot('column', 'operator', 'value')->fetch();

Fetching data on condition where a certain value does not occur by calling the whereNotIn function method.

<?php
            
  use database\DB;
              
  DB::try()->select('*')->from('table')->whereNotIn('column', 'value')->fetch();

Fetching data on a condition but on one or the other using the where function method and or.

<?php
            
  use database\DB;
               
  DB::try()->select('*')->from('table')->where('column', 'operator', 'value')->or('column', 'operator', 'value')->fetch();    

Fetching data but ordered calling the order function method.

<?php
                 
  use database\DB;  
             
  DB::try()->all('table')->order('column')->fetch();    

Fetching data but on descending order by calling the desc function method.

<?php
                 
  use database\DB;
              
  DB::try()->all('table')->order('column')->desc()->fetch(); 

Fetching data but on ascending order by calling the asc function method.

<?php
                 
  use database\DB;
              
  DB::try()->all('table')->order('column')->asc()->fetch(); 

Fetching data but limited by calling the limit function method.

<?php
                 
  use database\DB;
              
  DB::try()->all('table')->limit('number')->first();

Fetching data but only fetching the last inserted id by calling the getLastId function method.

<?php
                 
  use database\DB;
              
  DB::try()->getLastId('table')->first();

Fetching data and joining tables calling the join function method.

<?php
                              
  use database\DB;
                   
  DB::try()->select('table1.column', 'table2.column')->from('table1')->join('table2')
    
    ->on('table1.column', '=', 'table2.column')->where('table1.column', 'operator', 'value')->fetch();

Fetching data and joining tables calling the joinLeft function method.

<?php
                              
  use database\DB;
                   
  DB::try()->select('table1.column', 'table2.column')->from("table1")->joinLeft('table2')
    
    ->on('table1.column', '=', 'table2.column')->where('table1.column', 'operator', 'value')->fetch();

Fetching data but using raw sql code by calling the raw function method.


<?php
                 
  use database\DB;
             
  DB::try()->raw('sql')->fetch();

Inserting data

Inserting data by calling the insert function method.

<?php
                          
  use database\DB;
                 
  DB::try()->insert('table', [
    
    'column' => 'value',
    'column' => 'value',
    'column' => 'value',          
    'column' => 'value'

  ]);        

Updating data

Updating data by calling the update function method and set on condition.

<?php
                            
  use database\DB;
                       
  DB::try()->update('table')->set([
                
    'column' => 'value',      
    'column' => 'value'
                              
  ])->where('column', '=' ,'value')->run();              

Deleting data

Deleting data by calling the delete function method on condition.

<?php
                       
  use database\DB;
                        
  DB::try()->delete('table')->where('column', '=', 'value')->run();

Debugging

Getting the sql query code output by calling the getQuery function method.

<?php
            
  use database\DB;      
             
  DB::try()->select('*')->from('table')->getQuery();