app/Api/V1/Controllers/InstitutionController.php
/**
     * Store a newly created institution in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param Institution $institution
     * @param Address $address
     * @return \Dingo\Api\Http\Response
     * @throws \Illuminate\Validation\ValidationException
     */
Store a newly created institution in storage.
Store a newly created institution in storage .
public function store(Request $request)
    {
        $request->name = trim($request->name);
        $this->validate($request, [
            'name' => 'required|string|unique:institutions,name|min:3|regex:/^([a-z0-9 ])+$/i',
            'email' => 'required',
            'phone' => 'required|digits_between:8,16',
            'state_id' => 'required',
            'city_id' => 'required',
            'address_line_1' => 'string',
            'address_line_2' => 'string',
            'pobox_number' => 'string'
        ]);
        $data = $request->only([
            'name', 'email', 'phone'
        ]);
        if ($institution = Institution::query()->create($data)) {
            $address = Address::query()->create($request->only([
                'address_line_1', 'city_id',
                'state_id', 'pobox_number', 'address_line_2'
            ]));
            $institution->address_id = $address->id;
            $institution->save();
            if ($institution->showCode()) {
                $institution->makeVisible(['code', 'phone', 'email', 'address_id', 'created_by', 'updated_by']);
            }
            return new Response(compact('institution', 'address'), 201);
        }
        throw new StoreResourceFailedException('Institution couldn\'t be stored');
    }
public function store ( Request $ request ) { $ request -> name = trim ( $ request -> name ) ; $ this -> validate ( $ request , [ 'name' => 'required|string|unique:institutions,name|min:3|regex:/^([a-z0-9 ])+$/i' , 'email' => 'required' , 'phone' => 'required|digits_between:8,16' , 'state_id' => 'required' , 'city_id' => 'required' , 'address_line_1' => 'string' , 'address_line_2' => 'string' , 'pobox_number' => 'string' ] ) ; $ data = $ request -> only ( [ 'name' , 'email' , 'phone' ] ) ; if ( $ institution = Institution :: query ( ) -> create ( $ data ) ) { $ address = Address :: query ( ) -> create ( $ request -> only ( [ 'address_line_1' , 'city_id' , 'state_id' , 'pobox_number' , 'address_line_2' ] ) ) ; $ institution -> address_id = $ address -> id ; $ institution -> save ( ) ; if ( $ institution -> showCode ( ) ) { $ institution -> makeVisible ( [ 'code' , 'phone' , 'email' , 'address_id' , 'created_by' , 'updated_by' ] ) ; } return new Response ( compact ( 'institution' , 'address' ) , 201 ) ; } throw new StoreResourceFailedException ( 'Institution couldn\'t be stored' ) ; }
Store a newly created institution in storage.
Store a newly created institution in storage .
//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             // $institution->address()->attach($address->id); //event(new InstitutionEvent($admin, $item_, 0));