PHP code example of jamesaspence / grandiloquent

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

    

jamesaspence / grandiloquent example snippets




use Grandiloquent/GrandModel;

class Book extends GrandModel
{
    public function chapters()
    {
        return $this->hasMany(Chapter::class);
    }
     
}



use Grandiloquent/GrandModel;

class Chapter extends GrandModel
{
    public function book()
    {
        return $this->belongsTo(Book::class);
    }
}

$book = Book::find(1);
$chapters = [];
for($i = 0; $i < 5; ++$i)
{
    $chapter = new Chapter();
    $chapter->name = "Chapter $i";
    $chapters[] = $chapter;
}
$book->chapters()->saveMany($chapters);

/** @var GrandCollection $books */
$books = Book::take(10)->get();
$books->saveMany();