app/Http/Controllers/FilmController.php
/**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
Store a newly created resource in storage.
Store a newly created resource in storage .
public function store(FilmRequest $request)
    {
        try {
            $data = $request->except(['photo', 'date', 'genres']);
            if ($request->hasFile('photo')) {
                $file = $request->file('photo');
                $imageName = time() . '.' . $request['photo']->getClientOriginalExtension();
                $request->photo->move(public_path('films'), $imageName);
                $data['photo'] = $imageName;
            }
            $data['date'] = date('Y-m-d', strtotime($request->date));
            $data['slug'] = generate_film_slug($request->name);
            DB::beginTransaction();
            $film = Film::create($data);
            $array = [];
            foreach ($request->genres as $row):
                array_push($array, [
                    'film_id' => $film->id,
                    'genre_id' => $row
                ]);
            endforeach;
            FilmGenre::insert($array);
            DB::commit();
            if ($film):
                return success_response(JsonResponse::HTTP_CREATED, 'New film created', $film);
            else:
                error_response(JsonResponse::HTTP_UNPROCESSABLE_ENTITY, 'Film not saved', []);
            endif;
        }
        catch (Exception $error)
        {
            error_response(JsonResponse::HTTP_UNPROCESSABLE_ENTITY, 'Something went wrong', $error);
        }
    }
public function store ( FilmRequest $ request ) { try { $ data = $ request -> except ( [ 'photo' , 'date' , 'genres' ] ) ; if ( $ request -> hasFile ( 'photo' ) ) { $ file = $ request -> file ( 'photo' ) ; $ imageName = time ( ) . '.' . $ request [ 'photo' ] -> getClientOriginalExtension ( ) ; $ request -> photo -> move ( public_path ( 'films' ) , $ imageName ) ; $ data [ 'photo' ] = $ imageName ; } $ data [ 'date' ] = date ( 'Y-m-d' , strtotime ( $ request -> date ) ) ; $ data [ 'slug' ] = generate_film_slug ( $ request -> name ) ; DB :: beginTransaction ( ) ; $ film = Film :: create ( $ data ) ; $ array = [ ] ; foreach ( $ request -> genres as $ row ) : array_push ( $ array , [ 'film_id' => $ film -> id , 'genre_id' => $ row ] ) ; endforeach ; FilmGenre :: insert ( $ array ) ; DB :: commit ( ) ; if ( $ film ) : return success_response ( JsonResponse :: HTTP_CREATED , 'New film created' , $ film ) ; else : error_response ( JsonResponse :: HTTP_UNPROCESSABLE_ENTITY , 'Film not saved' , [ ] ) ; endif ; } catch ( Exception $ error ) { error_response ( JsonResponse :: HTTP_UNPROCESSABLE_ENTITY , 'Something went wrong' , $ error ) ; } }
Store a newly created resource in storage.
Store a newly created resource in storage .