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