PHP code example of coding-socks / reloquent

1. Go to this page and download the library: Download coding-socks/reloquent 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/ */

    

coding-socks / reloquent example snippets


use CodingSocks\Reloquent\Model;

class Movie extends Model
{
    protected $schema = [
        'title' => ['type' => 'string', 'textSearch' => true],
        'year' => ['type' => 'number'],
        'directors' => ['type' => 'array'],
    ];
}

$movie = new Movie()
$movie->title = "Alice in Wonderland";
$movie->year = 1951;
$movie->directors = ['Clyde Geronimi', 'Wilfred Jackson', 'Hamilton Luske'];
$movie->save()

Movie::query()
    ->where('title', 'matches', ['Alice', 'Wonderland'])
    ->where('year', '<=', 2000)
    ->where('directors', 'contains', ['Clyde Geronimi', 'Hamilton Luske'])
    ->get();

    // [...]

    'connections' => [

        // [...]

        'redis' => [
            'driver' => 'redis',
            'connection' => 'default',
            'data_structure' => 'hash',
        ],

        // [...]

    ],
    
    // [...]



namespace App\Models;

use CodingSocks\Reloquent\Model;

class Movie extends Model
{
}



use App\Models\Movie;

// Create

$movie = new Movie();
$movie->title = "Matrix";
$movie->year = 1999;
$movie->directors = ['Lana Wachowski', 'Lilly Wachowski'];
$movie->save();

// Fetch

$movie = Movie::find('01FTDC7A39ZGTCNH2D3DN5RPKR');

// Update

$movie = Movie::find('01FTDC7A39ZGTCNH2D3DN5RPKR');
$movie->title = "The Matrix";
$movie->save();

// Delete

$movie = Movie::find('01FTDC7A39ZGTCNH2D3DN5RPKR');
$movie->delete()



namespace App\Models;

use CodingSocks\Reloquent\Model;

class Movie extends Model
{
    protected $schema = [
    'title' => ['type' => 'string', 'textSearch' => true],
    'year' => ['type' => 'number'],
    'directors' => ['type' => 'array'],
];
}



use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateMoviesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('movies', function (Blueprint $table) {
            $table->string('title')->textSearch();
            $table->integer('year');
            $table->array('directors');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('movies');
    }
}

Movie::query()
    ->where('title', 'matches', ['Alice', 'Wonderland'])
    ->where('year', '<=', 2000)
    ->where('directors', 'contains', ['Clyde Geronimi', 'Hamilton Luske'])
    ->get();

$movie = Movie::find('DOES_NOT_EXIST');
$movie->title; // null
$movie->year; // null
$movie->directors; // null

$exists = Movie::exists($movie->id) // false