I defined an Eloquent model in Laravel AssessmentCategory
with a with
relation with another Eloquent model AssessmentQuestion
:
<?php
namespace App\Http\Models;
use Illuminate\Database\Eloquent\Model;
class AssessmentCategory extends Model {
protected $table = 'assessment_categories';
protected $with = ['questions'];
public function scopeJsonColumns($query) {
return $query->selectRaw('assessment_categories.id, assessment_categories.title');
}
public function questions() {
return $this->hasMany(AssessmentQuestion::class, 'category_id');
}
}
the AssessmentCategory
now always loads its relation (child) data, but I want to call the AssessmentCategory
somewhere without loading the relation (child) data.
AssessmentCategory::jsonColumns()->get();
CodePudding user response:
You can use without()
Method when want to get data without relation.
AssessmentCategory::without('questions')->jsonColumns()->get();
CodePudding user response:
There is multiple way to fix you problem.
- You can disable eager loading for current query at all:
AssessmentCategory::setEagerLoads([])->get();
- You can exclude only one relation from eager loading:
AssessmentCategory::without('questions')->get();
- Or you can disable eager loading at all by removing:
protected $with = ['questions'];
note that in this case you need yo overwrite all previous queries.