Depois de configurar para instanciar pela interface apareceu esse erro:
<?php namespace App\Repositories;
use App\Http\Requests\SeriesFormRequest; use App\Models\Episode; use App\Models\Season; use App\Models\Series; use Illuminate\Support\Facades\DB;
class EloquentSeriesRepository implements SeriesRepository {
public function add(SeriesFormRequest $request)
{
return DB::transaction(function () use ($request): Series {
$serie = Series::create($request->all());
$seasons = [];
for ($i = 1; $i <= $request->seasonsQty; $i ++) {
$seasons[] = [
'series_id' => $serie->id,
'number' => $i
];
}
Season::insert($seasons);
$episodes = [];
foreach ($serie->seasons as $season) {
for ($j = 1; $j <= $request->episodesPerSeason; $j ++) {
$episodes[] = [
'season_id' => $season->id,
'number' => $j
];
}
}
Episode::insert($episodes);
return $serie;
});
}
}
<?php
namespace App\Http\Controllers;
use App\Http\Requests\SeriesFormRequest; use App\Models\Series; use App\Repositories\SeriesRepository; use Illuminate\Http\Request;
class SeriesController extends Controller { public function __construct(private SeriesRepository $repository) { }
public function index(Request $request)
{
$series = Series::all();
$mensagemSucesso = session('mensagem.sucesso');
return view('series.index')->with('series', $series)
->with('mensagemSucesso', $mensagemSucesso);
}
public function create()
{
return view('series.create');
}
public function store(SeriesFormRequest $request)
{
$serie = $this->repository->add($request);
return to_route('series.index')
->with('mensagem.sucesso', "Série '{$serie->nome}' adicionada com sucesso");
}
public function destroy(Series $series)
{
$series->delete();
return to_route('series.index')
->with('mensagem.sucesso', "Série '{$series->nome}' removida com sucesso");
}
public function edit(Series $series)
{
return view('series.edit')->with('serie', $series);
}
public function update(Series $series, SeriesFormRequest $request)
{
$series->fill($request->all());
$series->save();
return to_route('series.index')
->with('mensagem.sucesso', "Série '{$series->nome}' atualizada com sucesso");
}
}
<?php
namespace App\Providers;
use App\Repositories\EloquentSeriesRepository; use App\Repositories\SeriesRepository; use Illuminate\Support\ServiceProvider;
class SeriesRepositoryProvider extends ServiceProvider { public array $bindings = [ SeriesRepository::class => EloquentSeriesRepository::class ]; }
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model;
class Series extends Model { use HasFactory; protected $fillable = ['nome'];
public function seasons()
{
return $this->hasMany(Season::class, 'series_id');
}
protected static function booted()
{
self::addGlobalScope('ordered', function (Builder $queryBuilder) {
$queryBuilder->orderBy('nome');
});
}
}