app/Http/Controllers/FarmsController.php
/**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
Update the specified resource in storage.
Update the specified resource in storage .
public function update(Request $request)
    {
      $this->validate($request, [
        'id' => 'required|string',
        '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_id' => 'required|string'
      ]);
      $addressToUpdate = address::find($request->address_id);
      $addressToUpdate->update([
        'street1' => $request->street,
        'suburb' => $request->suburb,
        'city_id '=>  $request->city_id,
        'country_id' =>  $request->country_id,
        'post_code' => $request->post_code
      ]);
      if($addressToUpdate->errors ||  empty($addressToUpdate)){
        return response()->json($addressToUpdate, 424);
      }
      $farmToUpdate = farms::find( $request->id);
      if(empty( $farmToUpdate)){
        return response()->json(['message'=>' no farm with id provided.'],404 );
      }
      $farmToUpdate->name = $request->name;
      $farmToUpdate->update([
        'description' => $request->description,
        'size' =>  $request->size,
        'monthly_income' => $request->monthly_income,
        'gio_location' =>  $request->gio_location,
        'address_id' =>  $addressToUpdate->id,
        'category_id' =>  $request->category_id,
        'user_id' => auth::user()->id
    ]);
     $updatedFarm = ["farm"=> $farmToUpdate, "address"=> $addressToUpdate];
      return  response()->json( $updatedFarm, 201);
    }
public function update ( Request $ request ) { $ this -> validate ( $ request , [ 'id' => 'required|string' , '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_id' => 'required|string' ] ) ; $ addressToUpdate = address :: find ( $ request -> address_id ) ; $ addressToUpdate -> update ( [ 'street1' => $ request -> street , 'suburb' => $ request -> suburb , 'city_id ' => $ request -> city_id , 'country_id' => $ request -> country_id , 'post_code' => $ request -> post_code ] ) ; if ( $ addressToUpdate -> errors || empty ( $ addressToUpdate ) ) { return response ( ) -> json ( $ addressToUpdate , 424 ) ; } $ farmToUpdate = farms :: find ( $ request -> id ) ; if ( empty ( $ farmToUpdate ) ) { return response ( ) -> json ( [ 'message' => ' no farm with id provided.' ] , 404 ) ; } $ farmToUpdate -> name = $ request -> name ; $ farmToUpdate -> update ( [ 'description' => $ request -> description , 'size' => $ request -> size , 'monthly_income' => $ request -> monthly_income , 'gio_location' => $ request -> gio_location , 'address_id' => $ addressToUpdate -> id , 'category_id' => $ request -> category_id , 'user_id' => auth :: user ( ) -> id ] ) ; $ updatedFarm = [ " farm " => $ farmToUpdate , " address " => $ addressToUpdate ] ; return response ( ) -> json ( $ updatedFarm , 201 ) ; }
Update the specified resource in storage.
Update the specified resource in storage .
// first get the address of the farm and update the farm // when address creation fails , return an error message // get the farm  corresponding to the if provided