Why did I need to swap $model and $relation in model relationship controller?
              
              #221
            
            
          -
| Hey guys, I ran into something recently that maybe someone could help me figure out. I have  I added a controller like this: <?php
namespace App\Http\Controllers\Api;
use App\Job;
use App\Task;
use Illuminate\Http\Request;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Orion\Http\Controllers\Controller;
class JobTaskController extends Controller {
    protected $model = Job::class;
    protected $relation = 'tasks';
    /**
     * Builds Eloquent query for fetching entity(-ies).
     *
     * @param Request $request
     * @param array $requestedRelations
     * @return Builder
     */
    protected function buildFetchQuery(Request $request, array $requestedRelations): EloquentBuilder {
        $query = parent::buildFetchQuery($request, $requestedRelations);
        $query->whereHas('job', fn($query) => $query->where('jobs.company_id', '=', $request->user()->company_id));
        return $query;
    }
    public function exposedScopes(): array
    {
        return ['withCalculatedQuantity', 'withTotalHours', 'withTotalOvertimeHours'];
    }
}And registered it in      Orion::resource('jobs',      JobController::class);
    Orion::resource('tasks',     TaskController::class);
    Orion::hasManyResource('jobs', 'tasks', JobTaskController::class);But I found the API responses are giving me Jobs rather than Tasks? (at this endpoint:  But when I switch the      protected $model = Task::class;
    protected $relation = 'job';... then it works? But it doesn't seem like this is exactly the right answer either, so I'm a little bit confused. Is there anything obvious I'm messing up here? Thank you for checking this out! | 
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
| For anyone looking for the answer to this. One is supposed to extend Orion\Http\Controllers\RelationController (not Controller) for controllers associated with relations. | 
Beta Was this translation helpful? Give feedback.
For anyone looking for the answer to this. One is supposed to extend Orion\Http\Controllers\RelationController (not Controller) for controllers associated with relations.