Olá Professor, tudo bem?
Estou com dificuldades para salvar as séries assistidas, quando clico em salvar ele retorna o seguinte erro: SQLSTATE[HY000]: General error: 1364 Field 'serie_id' doesn't have a default value (SQL: insert into seasons
() values ())
Eu escrevi o código todo em inglês mas segui os mesmos passos que o senhor.
Model:
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use App\Models\Season;
class Episode extends Model
{
protected $table = "episodes";
protected $fillable = ['number'];
public $timestamps = false;
public function season(){
return $this->belongsTo(Season::class);
}
}
Controller:
class EpisodesController extends Controller
{
public function index(Season $serieId){
return view('episodes.index',[
'episode' => $serieId -> relEpisode,
'season' => $serieId -> id
]);
}
public function watch(Season $season, Request $request){
$watchedEpisode = $request->episodes;
$season -> relEpisode ->each(function(Episode $episode)
use ($watchedEpisode) {
$episode -> watched = in_array($episode->id, $watchedEpisode);
});
$season -> push();
}
}
Migration:
*/
public function up()
{
Schema::table('episodes', function (Blueprint $table) {
// O valor "->default" cria uma padrão para os dados já existentes
$table -> boolean('watched')->default(false);
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('episodes', function (Blueprint $table) {
$table -> dropColumn('watched');
});
}
}