app/Http/Controllers/BookController.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(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'isbn' => 'required|regex:/\d{3}-\d{10}/', 
        ]);
        if ($validator->fails()) {
            return response()->json(['error' => 'Invalid ISBN'], 400);
        }
        $book = Book::create($request->all());
        if ($request->categories) {
            $this->saveCategories($book, $request);
        }
        return response()->json($book, 201);
    }
public function store ( Request $ request ) { $ validator = Validator :: make ( $ request -> all ( ) , [ 'isbn' => 'required|regex:/\d{3}-\d{10}/' , ] ) ; if ( $ validator -> fails ( ) ) { return response ( ) -> json ( [ 'error' => 'Invalid ISBN' ] , 400 ) ; } $ book = Book :: create ( $ request -> all ( ) ) ; if ( $ request -> categories ) { $ this -> saveCategories ( $ book , $ request ) ; } return response ( ) -> json ( $ book , 201 ) ; }
Store a newly created resource in storage.
Store a newly created resource in storage .
// TODO: may add request object if a validation getting more complex in the future