<?php

namespace App\Http\Controllers;

use App\Models\Carrera;
use App\Models\Material;
use Illuminate\Http\Request;

class MaterialController extends Controller
{


    public function store(Request $request)
    {
        $material =Material::create($request->except(['carrera']));
        $material->carreraMaterial()->sync($request->carrera);
        $carrera= Carrera::where('id','=',$request->carrera)->first();
        return redirect()->route('carrera.edit',compact('carrera'))->with('infoMaterial','Se anexó el material');
    }


    public function update(Request $request,Material $material)
    {
        $material->update($request->except(['carrera']));
        $material->carreraMaterial()->sync($request->carrera);
        $carrera= Carrera::where('id','=',$request->carrera)->first();
        return redirect()->route('carrera.edit',compact('carrera'))->with('infoMaterial','Se modificó el material');
    }


    public function destroy(Material $material,Request $request)
    {

        $carrera=$request->carrera;
        $material->carreraMaterial()->detach();
        $material->delete();
        session()->flash('swal',[
            'icon'=>'success',
            'title'=>'Eliminado!',
            'text'=>'Se ha eliminado el registro correctamente'
        ]);
        return redirect()->route('carrera.edit',compact('carrera'));

    }
}
