Eu estou a muito tempo tentando solucionar esse problema e não achei nada, fiz exatamente tudo que foi feito na vídeo aula única diferença que criei um controller para episódios:
SQLSTATE[HY000]: General error: 1 no such column: seasons.serie_id (SQL: select "episodes".*, "seasons"."serie_id" as "laravel_through_key" from "episodes" inner join "seasons" on "seasons"."id" = "episodes"."season_id" where "seasons"."serie_id" = 28)
Api.php:
<?php
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Api\SeriesControllerApi;
use App\Http\Controllers\Api\SeasonsControllerApi;
use App\Http\Controllers\Api\EpisodesControllerApi;
Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
return $request->user();
});
Route::apiResource('/series', SeriesControllerApi::class);
Route::get('/series/{series}/seasons', [SeasonsControllerApi::class, 'index']);
Route::get('/series/{series}/episodes', [EpisodesControllerApi::class, 'index']);
Controller de episódios:
<?php
namespace App\Http\Controllers\Api;
use App\Models\Serie;
use App\Http\Controllers\Controller;
class EpisodesControllerApi extends Controller
{
public function index(Serie $series)
{
return $series->episodes;
}
}
Model de Série:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use App\Models\Season;
use App\Models\Episodes;
class Serie extends Model
{
use HasFactory;
protected $fillable = ['name', 'cover'];
public function seasons()
{
return $this->hasMany(Season::class, 'series_id');
}
public function episodes()
{
return $this->hasManyThrough(Episodes::class, Season::class);
}
protected static function booted()
{
self::addGlobalScope('ordered', function (Builder $queryBuilder) {
$queryBuilder->orderBy('name');
});
}
}