PHP code example of crocodic / laravel-model

1. Go to this page and download the library: Download crocodic/laravel-model 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/ */

    

crocodic / laravel-model example snippets



namespace App\Models;

use DB;
use Crocodic\LaravelModel\Core\Model;

class BooksModel extends Model
{
    public $id;
    public $created_at;
    public $name;
}


namespace App\Models;

use DB;
use Crocodic\LaravelModel\Core\Model;

class BooksModel extends Model
{
    public $id;
    public $created_at;
    public $name;
    
    public function setConnection(){
         return "mysql";
    }

    public function setTable() {
        return "books";
    }   

    public function setPrimaryKey(){
        return "id";
    }
}

 
namespace App\Http\Controllers;

use App\Repositories\Books;

class FooController extends Controller {
    
    public function index() 
    {
        $books = Books::latest();
        return view("books", ["bookData"=>$books]);
    }
    
    public function detail($id)
    {
        $book = Books::find($id);
        return view("book_detail", ["book"=>$book]);
    }
    
    public function delete($id)
    {
        Books::deleteById($id);
        
        return redirect()->back()->with(["message"=>"Book ".$book->name." has been deleted!"]);
    }
}

    /**
    * @return App\Models\Categories
    */
    public function category() {
        return $this->belongsTo("App\Models\Categories");
    }

    // or 
    /**
    * @return App\Models\Categories
    */
    public function category() {
        return Categories::find($this->categories_id);
    }

 
namespace App\Http\Controllers;

use App\Repositories\Books;

class FooController extends Controller {
    
    
    public function detail($id)
    {
        $book = Books::find($id);
        
        $data = [];
        $data['book_id'] = $book->id;
        $data['book_name'] = $book->name;
        $data['book_category_id'] = $book->category()->id;
        $data['book_category_name'] = $book->category()->name;
        
        return view("book_detail",$data);
    }
    
}
 
$row = DB::table("books")->where("id",1)->first();

//Cast to Crocodic Laravel Model
$model = new Books($row);

//And then you can use cb model normally
echo $model->name;
 
$book = new Books();
$book->created_at = date("Y-m-d H:i:s"); //this created_at is a magic method you can ignore this
$book->name = "New Book";
$book->categories_id = 1;
$book->save();

...
$book->save();
$lastInsertId = $book->id; // get the id from id property
...
 
$book = Books::findById(1);
$book->name = "New Book";
$book->categories_id = 1;
$book->save();
 
$book = Books::findById(1);
$book->delete();
 
Books::deleteById(1);

/**
* Find all data by specific condition.
*/ 
$result = FooBar::findAllBy($column, $value = null, $sorting_column = "id", $sorting_dir = "desc");
// or 
$result = FooBar::findAllBy(['foo'=>1,'bar'=>2]);

/**
* Find all data without sorting
*/
$result = FooBar::findAll();

/**
* Count the records of table
*/ 
$result = FooBar::count();

/**
* Count the records with specific condition 
*/
$result = FooBar::countBy($column, $value = null);
// or
$result = FooBar::countBy(['foo'=>1,'bar'=>2]);

/**
* Find all datas and ordering the data to descending
*/
$result = FooBar::findAllDesc($column = "id");
// or simply
$result = FooBar::latest();

/**
* Find all datas and ordering the data to ascending
*/
$result = FooBar::findAllAsc($column = "id");
// or simply
$result = FooBar::oldest();

/** 
* Find/Fetch a record by a primary key value
*/
$result = FooBar::findById($id);
// or simply
$result = FooBar::find($id);

/**
* Create a custom query, and result laravel Query Builder collection
*/
$result = FooBar::where("foo","=",1)->first();
// or
$result = FooBar::table()->where("foo","=",1)->first();

/**
* Join a table with a simple step
*/
$result = FooBar::table()->withTable("related_table")->first();

/**
* Auto select all from a table, and make them prefix with its table name
*/
$result = FooBar::table()
->join("related_table","related_table.id","=","related_table_id")
->addSelect("foo_bar.*")
->addSelectTable("related_table") // This will produce: related_table_id, related_table_created_at, etc
->first();

/**
* Add like condition to the query
*/
$result = FooBar::table()->like("column",$yourKeyword)->get();
// It will produce same as FooBar::table()->where("columne","like","%".$yourKeyword."%")->get()

/**
* Find a record by a specific condition
*/
$result = Foobar::findBy($column, $value = null);
// or 
$result = Foobar::findBy(['foo'=>1,'bar'=>2]);

/**
* To run the insert SQL Query
*/
$fooBar = new FooBar();
$fooBar->name = "Lorem ipsum";
$fooBar->save();

/**
* To bulk insert
*/
$data = [];
$foo = new FooBar();
$foo->name = "Lorem ipsum 1";
array_push($data, $foo);
$bar = new FooBar();
$bar->name = "Lorem ipsum 2";
array_push($data, $bar);
FooBar::bulkInsert($data);


/**
* To run the update SQL Query
*/
$fooBar = FooBar::findById($value);
$fooBar->name = "Lorem ipsum";
$fooBar->save();

/**
* To delete the record by a primary key value
*/
FooBar::deleteById($value);

/**
* To delete the record by a specific condition
*/
FooBar::deleteBy($column, $value = null);
// or
Foobar::deleteBy(['foo'=>1,'bar'=>2]);

/**
* To delete after you fetch the record 
*/
$fooBar = FooBar::findById($value);
$fooBar->delete();

class Posts extends Model {
    // etc
    
    /**
    * @return Illuminate\Support\Collection
    */
    public function comments() {
        return $this->hasMany(Comments::class);
    }
    
    // or with full option
    /**
    * @return Illuminate\Support\Collection
    */
    public function comments() {
        return $this->hasMany(Comments::class, "foreign_key", "local_key", function($condition) {
            return $condition->where("status","Active");
        });
    }
}

class Comments extends Model {
    // etc
    
    /**
    * @return App\Models\Posts
    */
    public function post() {
        return $this->belongsTo(Posts::class);
    }
    
    // or with full option
    /**
    * @return App\Models\Posts
    */
    public function post() {
        return $this->belongsTo(Posts::class, "foreign_key", "local_key");
    }
}
/app/Models/BooksModel.php
 
/BooksModel.php
/CategoriesModel.php