app/Http/Controllers/FarmsController.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)
    {
        $this->validate($request, [
            'name' => 'required|string',
            'description' => 'required|string',             
            'size' =>  'required|string',
            'street' => 'required|string',
            'suburb'  => 'required|string',
            'post_code'  => 'required|string',
            'city_id' => 'required|integer',        
            'country_id' => 'required|integer',
            'monthly_income' => 'string',
            'gio_location' => 'string',
            'category_id' => 'required|string'
          ]);
          $address = address::create([
            'street1' => $request->street,
            'suburb'=> $request->suburb,
            'city_id' =>  $request->city_id,
            'country_id' =>  $request->country_id,
            'post_code' => $request->post_code
          ]);
          if($address->errors){
            return response()->json($address, 424);
          }
          $newFarm = farms::create([
            'name' => $request->name,
            'description' => $request->description,
            'size' =>  $request->size,
            'monthly_income' => $request->monthly_income,
            'gio_location' =>  $request->gio_location,
            'address_id' => $address->id,
            'category_id' =>  $request->category_id,
            'user_id' => auth::user()->id
          ]);
          if($newFarm->errors){
            return response()->json($newFarm, 424);
          }
          return response()->json($newFarm, 201);
    }
public function store ( Request $ request ) { $ this -> validate ( $ request , [ 'name' => 'required|string' , 'description' => 'required|string' , 'size' => 'required|string' , 'street' => 'required|string' , 'suburb' => 'required|string' , 'post_code' => 'required|string' , 'city_id' => 'required|integer' , 'country_id' => 'required|integer' , 'monthly_income' => 'string' , 'gio_location' => 'string' , 'category_id' => 'required|string' ] ) ; $ address = address :: create ( [ 'street1' => $ request -> street , 'suburb' => $ request -> suburb , 'city_id' => $ request -> city_id , 'country_id' => $ request -> country_id , 'post_code' => $ request -> post_code ] ) ; if ( $ address -> errors ) { return response ( ) -> json ( $ address , 424 ) ; } $ newFarm = farms :: create ( [ 'name' => $ request -> name , 'description' => $ request -> description , 'size' => $ request -> size , 'monthly_income' => $ request -> monthly_income , 'gio_location' => $ request -> gio_location , 'address_id' => $ address -> id , 'category_id' => $ request -> category_id , 'user_id' => auth :: user ( ) -> id ] ) ; if ( $ newFarm -> errors ) { return response ( ) -> json ( $ newFarm , 424 ) ; } return response ( ) -> json ( $ newFarm , 201 ) ; }
Store a newly created resource in storage.
Store a newly created resource in storage .
// first create an address of the farm and store the address in the database // when address creation fails , return an error message // create the farm and store it in the db 