PHP code example of caiochami / bundles

1. Go to this page and download the library: Download caiochami/bundles library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

caiochami / bundles example snippets




header("Content-Type: application/json;");

use Bundles\Request;

$request = new Request;

//all empty string variables are converted to null automatically
$request->validate([
    "name" => [" "patients.*.age" => ["


  use Bundles\Model;
 use Bundles\DatabaseConnection as Database;

 class User extends Model
 {

    protected static $tableName = "users";

    protected static $columns = "id,name,address.city";

    protected static $joins = "";

    protected static $key = "id";

    public function addresses(){
        $addresses = DB::use(self::$conn)
        ->table('addresses')
        ->select(['id', 'city'])
        ->where('user_id', $this->id)
        ->retrieve()
        ->get();
   
        $this->addresses = $addresses;
   
    }

 }

class Address extends Model
 {

    protected static $tableName = "addresses";

    protected static $columns = "id,city";

    protected static $joins = "";

    protected static $key = "id";

    protected static $fillable = [ "city" ];

    public function user(){
        $user = DB::use(self::$conn)
        ->table('users')
        ->select(['id', 'name'])
        ->where('id', $this->id)
        ->retrieve()
        ->first();
   
        $this->user = $user;
   
    }

    //mutator
    
    public function setCityAttribute($value){
        return strtolower($value);
    }


 }

 $connection = Database::attempt([
    "db_host" =>  "HOST",
    "db_name" =>  "NAME",
    "db_user" =>  "USER",
    "db_psw" =>  "PASSWORD"
])

 //creating a user
 $user = new User($connection);
 $user->name = "Foo";
 $user->save();

 //or

 $address = Address::create(["city"=> "My homeland"]);

 //finding and updating the user

 $user = User::find($connection, 1); 
 //or 
 $address = Address::use($connection)->where("id", 1)->first();

 $user->name = "Bar";
 $user->save();

 //or

 User::update($connection, 1, [
     "name" => "Bar"
 ]);

 // deleting a user

 User::destroy($connection, 1);

 $user = User::find($connection, 1);
 $user->delete();

 //querying all addresses

 Address::all($connection);

 //or more specific

 User::use($connection)
 ->where("name", "LIKE", "%bar%")
 ->whereBetween("birthday", "1993-01-01", "2020-01-01"),
 ->orWhere("address.city", "California")
 ->with(['addresses'])
 ->limit(15)
 ->retrieve()
 ->get();


//for debugging you can pass an array just like this
User::find($connection, 1, ["debug" => true]);

//or 

User::where("birthday", ">", "1972-11-11")
->orderBy(["name", "DESC"])
->retrieve(["debug" => true, "show_query" => true ])
->get();