app/Api/V1/Controllers/ParastatalController.php
/**
     * Store a newly created parastatal in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Dingo\Api\Http\Response
     */
Store a newly created parastatal in storage.
Store a newly created parastatal in storage .
public function store(Request $request, Parastatal $parastatal, Address $address)
    {
        $request->name = trim($request->name);
        $this->validate($request, [
            'name' => 'required|string|unique:parastatals,name|min:3|regex:/^([a-z0-9 ])+$/i',
            'email' => 'required|string',
            'phone' => 'required|digits_between:8,16',
            'state_id' => 'required',
            'city_id' => 'required',
            'institution_id' => [
                'required',
                'string',
                'exists:institutions,id',              
            ],
            'address_line_1' => 'string',
            'address_line_2' => 'string',
            'pobox_number' => 'string'
        ]);
         passed
        $data = $request->only([
            'name', 'email', 'phone', 'institution_id'
        ]);
        if ($parastatal = $parastatal->create($data)) {
            $address = $address->create($request->only([
                'address_line_1', 'city_id',
                'state_id', 'pobox_number', 'address_line_2'
            ]));
            $parastatal->address_id = $address->id;
            $parastatal->save();               
            if ($parastatal->showCode()) {
                $parastatal->makeVisible(['code', 'phone', 'email', 'address_id', 'created_by', 'updated_by']);
            }
            return new Response(compact('parastatal', 'address'), 201);
        }
        throw new StoreResourceFailedException('Parastatal couldn\'t be stored');
    }
public function store ( Request $ request , Parastatal $ parastatal , Address $ address ) { $ request -> name = trim ( $ request -> name ) ; $ this -> validate ( $ request , [ 'name' => 'required|string|unique:parastatals,name|min:3|regex:/^([a-z0-9 ])+$/i' , 'email' => 'required|string' , 'phone' => 'required|digits_between:8,16' , 'state_id' => 'required' , 'city_id' => 'required' , 'institution_id' => [ 'required' , 'string' , 'exists:institutions,id' , ] , 'address_line_1' => 'string' , 'address_line_2' => 'string' , 'pobox_number' => 'string' ] ) ; $ data = $ request -> only ( [ 'name' , 'email' , 'phone' , 'institution_id' ] ) ; if ( $ parastatal = $ parastatal -> create ( $ data ) ) { $ address = $ address -> create ( $ request -> only ( [ 'address_line_1' , 'city_id' , 'state_id' , 'pobox_number' , 'address_line_2' ] ) ) ; $ parastatal -> address_id = $ address -> id ; $ parastatal -> save ( ) ; if ( $ parastatal -> showCode ( ) ) { $ parastatal -> makeVisible ( [ 'code' , 'phone' , 'email' , 'address_id' , 'created_by' , 'updated_by' ] ) ; } return new Response ( compact ( 'parastatal' , 'address' ) , 201 ) ; } throw new StoreResourceFailedException ( 'Parastatal couldn\'t be stored' ) ; }
Store a newly created parastatal in storage.
Store a newly created parastatal in storage .
//validation /* Rule::exists('institutions')->where(function ($query) {
                    $query->where('id', $request->institution_id);
                }), */ //validation passed //create address //almost useless, but we don't know if Nasir uses 'address_id' somewhere. //commented cos we changed the relationship from many-to-many // $parastatal->address()->attach($address->id); //event(new ParastatalEvent($admin, $item_, 0));