Reputation: 51
I am working on my job project and I can count job by category by using this code
$category = Category::withCount('job')->take(7)->get();
but when I want to count job by category with job status =1 I can not how to do it. here is my code
note: in job table when status = 1 mean job post is active and when status=1 job post expired
$category = Category::withCount('job')->where('status',1)->take(7)->get();
my job table
$table->integer('company_id');
$table->string('jobTitle');
$table->longText('jobDescription');
$table->longText('jobRequirement');
$table->integer('contractType_id');
$table->integer('category_id');
$table->integer('salaryRange_id');
$table->integer('location_id');
$table->string('hire');
$table->boolean('status');
$table->integer('count_view');
$table->string('deadLine');
$table->integer('level_id');
$table->integer('degree_id');
$table->integer('preferred_experience_id');
$table->string('language');
my category table
Schema::create('categories', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id');
$table->string('name');
$table->timestamps();
});
$category = Category::withCount('job')->where('status',1)->take(7)->get();
Upvotes: 0
Views: 187
Reputation: 3328
Category::withCount(['job'=>function($query){
$query->where('status',1);
}])->take(7)->get();
Upvotes: 1