PHP code example of alvin0 / database-json-laravel

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

    

alvin0 / database-json-laravel example snippets


namespace  App\DatabaseJson\Models;

use DatabaseJson\Model;  

class User extends  Model
{

}

namespace  App\DatabaseJson\Migrations;

use DatabaseJson\DatabaseJson;
use DatabaseJson\Migration;

class CreateTableUserMigrateMigrate extends  Migration
{
	/**
	* How to create table
	*
	* DatabaseJson::table('NameTable',array(
	* {field_name} => {field_type} More information about field types and usage in PHPDoc
	* ));
	*/
	/**
	* Run the migrations.
	*
	* @return  void
	*/

	public  function  up()
	{
		DatabaseJson::create('users', array(
			'name' => 'string',
			'old' => 'integer',
			'created_at' => 'string',
			'updated_at' => 'string',
		));
	}

}

namespace  App\DatabaseJson\Migrations;

use DatabaseJson\DatabaseJson;
use DatabaseJson\Migration;

class CreateTableUserMigrateMigrate extends  Migration
{
	/**
	* Run the migrations.
	*
	* @return  void
	*/
	public  function  up()
	{
		DatabaseJson::table('users')->addFields([
			//'name' => 'string'
			//{field_name} => {field_type} More information about field types and usage in PHPDoc
		]);
		
		//DatabaseJson::table('users')->deleteFields([
			//'name',
			//{field_name}
		//]);
	}

}

namespace  App\DatabaseJson\Models;

use DatabaseJson\Model;  

class User extends  Model
{
	/**
     * Indicates if the model should be timestamped.
     *
     * @var bool
     */
    public $timestamps = false;
    
}

use App\DatabaseJson\Models\User;

$user = new User;
$user->name = 'alvin';
$user->old = 27;
$user->save();

use App\DatabaseJson\Models\User;

$user = User::create([
	'name' => 'alvin',
	'old' => 27
]);

use App\DatabaseJson\Models\User;

$user = new User;
$user->id = 1;
$user->name = 'alvin';
$user->old = 27;
$user->save();

use App\DatabaseJson\Models\User;

$id = 1;
$user = User::update([
	'name' => 'alvin',
	'old' => 28
],$id);

use App\DatabaseJson\Models\User;

$id = 1;
$user = User::where('name','alvin')->delete();
$userById = User::find(1)->delete();

use App\DatabaseJson\Models\User;

$id = 1;
$user = User::delete();

use App\DatabaseJson\Models\User;

$users = User::all();

use App\DatabaseJson\Models\User;

$users = User::where('name','alvin')
	->where('old', '>=', 18)
	->get();

use App\DatabaseJson\Models\User;

$users = User::paginate(10);

use App\DatabaseJson\Models\User;

$users = User::where('old', '>=', 18)->paginate(10);

return $this->belongsTo('App\DatabaseJson\Models\User', 'local_key', 'foreign_key');

namespace  App\DatabaseJson\Models;

use DatabaseJson\Model;  

class Blog extends  Model
{
	public  function  user()
	{
		$this->belongsTo(User::class);
	}
    
}

return $this->hasMany('App\DatabaseJson\Models\Blog', 'foreign_key', 'local_key');

namespace  App\DatabaseJson\Models;

use DatabaseJson\Model;  

class User extends  Model
{
	public  function  blogs()
	{
		return  $this->hasMany(Blog::class, 'user_id');
	}
    
}

	//return model App\DatabaseJson\Models\User
	$userBlog = Blog::find(1)->user

	//return Illuminate\Support\Collection
	$blogsByUser = User::find(1)->blogs

	$users = User::with('blogs')->where('id', 1)->get();
 



namespace App;

namespace  App\DatabaseJson\Models;

class User extends Model
{
    /**
     * Get the user's first name.
     *
     * @param  string  $value
     * @return string
     */
    public function getFirstNameAttribute($value)
    {
        return ucfirst($value);
    }
}
 

$user = \App\DatabaseJson\Models\User::find(1);

$firstName = $user->first_name;

 



namespace App;

namespace  App\DatabaseJson\Models;

class User extends Model
{
    /**
     * The accessors to append to the model's array form.
     *
     * @var array
     */
    protected $appends = ['first_name'];
}