PHPackages                             monurakkaya/opencart-eloquent - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. monurakkaya/opencart-eloquent

ActiveProject[Database &amp; ORM](/categories/database)

monurakkaya/opencart-eloquent
=============================

Eloquent support for OpenCart models

v0.0.5(4y ago)62323MITPHPPHP &gt;=7.0

Since Aug 6Pushed 4y ago3 watchersCompare

[ Source](https://github.com/monurakkaya/opencart-eloquent)[ Packagist](https://packagist.org/packages/monurakkaya/opencart-eloquent)[ RSS](/packages/monurakkaya-opencart-eloquent/feed)WikiDiscussions main Synced yesterday

READMEChangelog (5)Dependencies (4)Versions (6)Used By (0)

OPENCART-ELOQUENT
=================

[](#opencart-eloquent)

Yet another OpenCart database package for lazy laravel developers. I wrote this package to use with my OpenCart extensions. It does not replace whole opencart models with eloquent models. It just gives you the opportunity to use eloquent ORM flexibility while querying

### INSTALLATION

[](#installation)

1. First, install the package via composer:

```
composer require monurakkaya/opencart-eloquent
```

2. Then copy ocmod file to your system directory

```
cp storage/vendor/monurakkaya/opencart-eloquent/src/*.xml upload/system
```

3. Go to your admin panel and refresh modifications.

### EXAMPLES

[](#examples)

To get categories count which have products in it, just type:

```
use App\Models\Catalog\Category\Category;

$categories_count = Category::whereHas('products')->count();
```

To get categories with products count, just type:

```
use App\Models\Catalog\Category\Category;

$categories = Category::withCount('products')->get();

echo $categories->first()->products_count // 5
echo $categories->first()->description->name // Electronics
```

Options and values

```
use App\Models\Catalog\Option\Option;

$options = Option::with('description', 'values.description')->get();

foreach ($options as $option) {
    echo $option->description->name; // Color
    foreach ($option->values as $value) {
        $value->description->name; // Dark
    }
}
```

What about product options and option values?

```
use App\Models\Catalog\Product\Product;

$product = Product::with('options.values')->find(5);

foreach($product->options as $option) {
    echo $option->option->description->name; // Color
    foreach ($option->values as $value) {
        $value->value->description->name; // Grey
    }
}

// Be careful about (N+1) which opencart doesn't care at all...
```

Pagination

```
use App\Models\Catalog\Manufacturer\Manufacturer;

$manufacturers = Manufacturer::with('description')
    ->paginate(
        $this->config->get('config_limit_admin'),
        ['*'],
        'page',
        $this->request->get['page']
     );

//below lines belongs to opencart logic.
$pagination = new \Pagination();
$pagination->total = $manufacturers->total();
$pagination->page = $manufacturers->currentPage();
$pagination->limit = $this->config->get('config_limit_admin');
$pagination->url = $this->url->link('extension/module/some_of_my_extensions/manufacturer', 'user_token=' . $this->session->data['user_token']. '&page={page}', true);
$pagination = $pagination->render();

$this->response->setOutput($this->load->view('extension/module/some_of_my_extensions', compact('manufacturers', 'pagination')))

```

Easy right? :)

This project will be maintained as long as I continue to use OpenCart.

### WRITING YOUR EXTENSION MODELS

[](#writing-your-extension-models)

just put your models in your upload/app directory. Recommended way is follow opencart directory structure like:
`upload/admin/controller/extension/module/product_video.php``upload/admin/view/template/extension/module/product_video.twig``upload/app/Models/Extension/Module/ProductVideo.php`

```
