Search
 
SCRIPT & CODE EXAMPLE
 

PHP

count child products for each parent Product laravel

//First you need to set relations in Products Models
class ParentProduct extends Model
{
  public function childProduct() {
          return $this->hasMany(ChildProduct::class, 'parent', 'id')->orderBy('rang', 'desc');
      }
}

class ChildProduct extends Model
{
  public function parentProduct() {
          return $this->hasOne(ParentProduct::class, 'id', 'parent')->orderBy('rang', 'desc');
      }
}

//Than call it In ProductsController
$allProducts = ParentProduct::with(['childProduct'])->orderByDesc('rang')->get()

//Than Use it In blade
@foreach($allProducts as $product)
	{{ count($product->$childProduct) }}
@endforeach
Comment

PREVIOUS NEXT
Code Example
Php :: PHP Forward POST content into Python script 
Php :: customer io send_at api 
Php :: php function argument spicific types array check 
Php :: sudo apt-get install php7.0-gd 
Php :: PHP strrchr — Find the last occurrence of a character in a string 
Php :: get current route laravel 
Php :: php parse json 
Php :: how to delete image from floder in laravel 
Php :: loop through values of hash php 
Php :: how login one user with id in laravel 
Php :: Yii2 Fatal Error: Require_Once() 
Php :: laravel migrate only new tables 
Php :: what sign is less than or equal to php 
Php :: cakephp 2 with customize link 
Php :: CORSS oauth/token lavarel 
Php :: PHP Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; 
Php :: php create Hmac sha256 
Php :: how to concat in where clause like laravel query builder 
Php :: how to restart php-fpm on ubuntu 
Php :: How to read session in laravel 
Php :: Message: Too few arguments to function Admin::tindakan_vaksin1(), 1 passed in C:xampphtdocsloginsystemcoreCodeIgniter.php on line 532 and exactly 2 expected 
Php :: php array order by date 
Php :: barryvdh/laravel-dompdf laravel 8 header and footer every page 
Php :: image watermark on image laravel 8 
Php :: php array formatted output 
Php :: convert post name to id 
Php :: wp-config.php repair 
Php :: laravel php 8 ububtu 
Php :: php iterate through array 
Php :: wp_query post count 
ADD CONTENT
Topic
Content
Source link
Name
6+9 =