How to use the models

A Eloquent model is something different than a CodeIgniter model

The nice thing about the Illuminate Eloquent models, is that they can be used in CodeIgniter models.

<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

use Db\Eloquent\Models\Car\Model as CarModel;
use Db\Eloquent\Models\Brand\Model as BrandModel;

class Carpark extends CI_Model
{
    private $count = 0;
    private $total = 0;
    private $results = null;

    public function __construct()
    {
        parent::__construct();

        // You can also use CodeIgniter models itself
        $this->load->model('whatever/Model', 'WhateverModel'); 

    }

    function getBrands($search = false, $start = 0, $limit = 25)
    {
        $set = CarModel::with(
            'brand_info'
            'cuser',
            'muser'
        )
            ->whereHas('semester_info', function ($q) {
                $q->where('current', '=', 1);
            })
            ->where('active', '=', 1);

        if ($search !== false && $search !== '') {

            $set->where(function ($query) use ($search) {
                $query->where('name', 'LIKE', '%' . $search . '%');

                $columns = ['engine', 'color'];

                foreach ($columns as $column) {
                    $query->orWhere($column, 'LIKE', '%' . $search . '%');
                }

                // also search within the brand information
                $query->orWhereHas('brand_info', function ($q) use ($search) {
                    $q->where(function ($q) use ($search) {
                        $q->where('name', 'LIKE', '%' . $search . '%');
                    });
                });

            });
        }

        $this->total = $set->count();

        $this->results = $set->orderby('number')
            ->limit($limit)
            ->offset($start)
            ->get();

        $this->count = $set->count();

        return [
            'success' => true,
            'records' => $this->results,
            'count' => $this->count,
            'total' => $this->total,
            'message' => $this->count + ' cars have been loaded'
        ];
    }

    ...
}
Last update: March 14th at 1:05pm