app/Api/V1/Controllers/ParastatalController.php
/**
     * Update the specified parastatal by id in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Dingo\Api\Http\Response
     */
Update the specified parastatal by id in storage.
Update the specified parastatal by id in storage .
public function update(Request $request, $id)
    {
        $this->validate($request, [
            'name' => 'required|string|min:3|regex:/^([a-z0-9 ])+$/i|unique:parastatals,name,' . $id,
            'email' => 'required|string',
            'phone' => 'required|digits_between:8,16',
            'state_id' => 'required',
            'city_id' => 'required',
            'institution_id' => [
                'required',
                'exists:institutions,id',
            ],
            'address_line_1' => 'string',
            'address_line_2' => 'string',
            'pobox_number' => 'string'
        ]);
        if ($parastatal = Parastatal::find($id)) {
            $address = Address::updateOrCreate(
                ['id' => $parastatal->address_id],
                $request->only('address_line_1', 'city_id', 'state_id', 'pobox_number', 'address_line_2')
            );
            if (!$address) {
                throw new UpdateResourceFailedException('Unable to complete process, try again');
            }
            $data = $request->only('name', 'email', 'phone', 'institution_id');
            $data['address_id'] = $address->id;
            if ($parastatal->update($data)) {
                if ($parastatal->showCode()) {
                    $parastatal->makeVisible(['code', 'phone', 'email', 'address_id', 'created_by', 'updated_by']);
                }
                return $parastatal;
            }
            throw new UpdateResourceFailedException('Unable to complete process, try again');
        }
        throw new UpdateResourceFailedException('Parastatal not found');
    }
public function update ( Request $ request , $ id ) { $ this -> validate ( $ request , [ 'name' => 'required|string|min:3|regex:/^([a-z0-9 ])+$/i|unique:parastatals,name,' . $ id , 'email' => 'required|string' , 'phone' => 'required|digits_between:8,16' , 'state_id' => 'required' , 'city_id' => 'required' , 'institution_id' => [ 'required' , 'exists:institutions,id' , ] , 'address_line_1' => 'string' , 'address_line_2' => 'string' , 'pobox_number' => 'string' ] ) ; if ( $ parastatal = Parastatal :: find ( $ id ) ) { $ address = Address :: updateOrCreate ( [ 'id' => $ parastatal -> address_id ] , $ request -> only ( 'address_line_1' , 'city_id' , 'state_id' , 'pobox_number' , 'address_line_2' ) ) ; if ( ! $ address ) { throw new UpdateResourceFailedException ( 'Unable to complete process, try again' ) ; } $ data = $ request -> only ( 'name' , 'email' , 'phone' , 'institution_id' ) ; $ data [ 'address_id' ] = $ address -> id ; if ( $ parastatal -> update ( $ data ) ) { if ( $ parastatal -> showCode ( ) ) { $ parastatal -> makeVisible ( [ 'code' , 'phone' , 'email' , 'address_id' , 'created_by' , 'updated_by' ] ) ; } return $ parastatal ; } throw new UpdateResourceFailedException ( 'Unable to complete process, try again' ) ; } throw new UpdateResourceFailedException ( 'Parastatal not found' ) ; }
Update the specified parastatal by id in storage.
Update the specified parastatal by id in storage .
//validation /* Rule::exists('institutions')->where(function ($query) use ($request) {
                    $query->where('id', $request->institution_id);
                }), */ //update address //get parastatal info //commented cos we changed the relationship from many-to-many // $parastatal->address()->sync($address, false); //event(new ParastatalEvent($admin, $item, 1)); //return new Response(['status'=>  $parastatal->name . ' updated'], 201);