app/Http/Controllers/EtudiantController.php
/**
     * Store a newly created resource in storage.
     *
     * @param  Request  $request
     * @return Response
     */
Store a newly created resource in storage.
@param  Request  $request
@return Response
Store a newly created resource in storage . @param Request $request @return Response
public function store(Request $request)
    {
        $message = "";
        $data = $request->only('email', 'password','phone','name');
        $data['password'] = Hash::make($data['password']);
        $etudiant = new Etudiant($data);
        $coop = Cooperative::all()->first();
        $etudiant->cooperative()->associate($coop);
        try{
            $etudiant->save();
            $message = "success";
        }
        catch(QueryException $e){
            abort(403, 'Unauthorized action.');
        }
        return response()->json(['message' => $message]);
    }
public function store ( Request $ request ) { $ message = " " ; $ data = $ request -> only ( 'email' , 'password' , 'phone' , 'name' ) ; $ data [ 'password' ] = Hash :: make ( $ data [ 'password' ] ) ; $ etudiant = new Etudiant ( $ data ) ; $ coop = Cooperative :: all ( ) -> first ( ) ; $ etudiant -> cooperative ( ) -> associate ( $ coop ) ; try { $ etudiant -> save ( ) ; $ message = " success " ; } catch ( QueryException $ e ) { abort ( 403 , 'Unauthorized action.' ) ; } return response ( ) -> json ( [ 'message' => $ message ] ) ; }
Store a newly created resource in storage.
Store a newly created resource in storage .
// Takes the request data and form an array // Hash the password //Create a new student from the data (mass assignment) //Select the first cooperative it finds; //Create association between a student and a cooperative //Save it to the database