PHP code example of immachakata / ci4-relationships

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

    

immachakata / ci4-relationships example snippets


namespace App\Models;

use CodeIgniter\Model;
use CI4Extensions\Database\RelationshipsTrait;

class UserModel extends Model
{
    use RelationshipsTrait;
    
    ... 
    // declare relationships
    public function initialize(){
        $this->hasOne('avatar', AvatarModel::class, 'user_id');
        $this->hasMany('links', LinkModel::class, 'user_id');
    }
}

class UserController extends BaseController
{
    public function index(){
        ...
        $this->respond([
            'user' => model(UserModel::class)->with('avatar')->findAll()
        ]);
    }
}