PHP code example of monurakkaya / opencart-eloquent

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

    

monurakkaya / opencart-eloquent example snippets


use App\Models\Catalog\Category\Category;

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

use App\Models\Catalog\Category\Category;

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

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

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
    }
}

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...

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')))
  


namespace App\Models\Extension\Module

use App\Models\Model;
use App\Models\Catalog\Product\Product;

class ProductVideo extends Model {
    private $table = DB_PREFIX.'product_video';
    private $primaryKey = 'product_video_id';
    
    public function product()
    {
        return $this->belongsTo(Product::class, 'product_id') 
    }
}

use App\Models\Extension\Module\ProductVideo;

$video = ProductVideo::with('product')->first();
$video->product // Product object


namespace App\Traits\Extension\Payment\MyModule\Traits\HasPrice;

trait HasPrice {
    public function getDiscountedPrice($percent = 20) {
        return $this->price * (100 - $percent) / 100;
    }
}
xml
<file path="app/Models/Catalog/Product/Product.php">
    <operation>
        <search><![CDATA[//ocmod]]></search>
        <add position="after">
            <![CDATA[
            public function getNameAttribute() {
                return $this->description->name;
            }
            ]]>
        </add>
    </operation>
</file>
xml
<file path="app/Models/Catalog/Product/Product.php">
    <operation>
        <search><![CDATA[//ocmod]]></search>
        <add position="after">
            <![CDATA[
            public function videos() {
                return $this->hasMany(\App\Models\Extension\Module\ProductVideo::class, 'product_id', 'product_id');
            }
            ]]>
        </add>
    </operation>
</file>