Monday, April 10, 2017

Trying to add medicines related branch

I am trying to do that when one branchAdmin(1) add new medicines in its branch its ok to add medicines, but why these medicines are also added in another branch(2) without adding by its own branchAdmin, whatever medicines are added by branchAdmin(1) its add another branches?

Medicines Controller:

public function saveMedinfo(Request $request){
        $validator = Validator::make($request->all(),[
            'product_name' => 'required',
            'product_type' =>'required',
            'potency' =>'required',
            'manufact' => 'required',
        ]);
        if($validator->passes()){
            $med = new Medicine();
            $med->branch_id = $request->branch_id;
            $med->medicines_name = $request->product_name;
            $med->type = $request->product_type;
            $med->potency = $request->potency;
            $med->manufacturer = $request->manufact;

            if($med->save()){
                $request->session()->flash('message','Successfully Save!!');
                return redirect('/add/product');
            }
        }else{
            return redirect('/add/product')->withErrors($validator)->withInput();
        }
    }

BranchAdmin Controller:

public function getBranch($id){
        if($this->isUserBranchOwner($id)){
            $branch = Branch::find($id);
            return view('branch')->with('branch', $branch);
        }else{
            return redirect('/branch/'.$this->getUserBranchId());
        }
    }

    public function isUserBranchOwner($id){
        return Branch::where('user_id','=', Auth::user()->id)->where('id', '=', $id)->count();
    }

    protected function getUserBranchId(){
        return Branch::where('user_id','=', Auth::user()->id)->first()->id;
    }

AddMedicines View:

<div class="col-xs-12 col-sm-12 col-md-6 col-lg-6">
    <form class="form-horizontal product-form" action="/add/product" method="post">
        <div class="form-group">
            <label class="col-sm-3 control-label">Branch</label>
            <div class="col-sm-9">
                <select  class="form-control" name="branch_id">
                    <option value="">Select</option>
                    @foreach($branch as $brnch)
                        <option value=""></option>
                    @endforeach
                </select>
                @if($errors->has('branch_id'))
                    
                @endif
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">Medicine-Name</label>
            <div class="col-sm-9">
                <input type="text" class="form-control" name="product_name" placeholder="Product Name">
                @if($errors->has('product_name'))
                    
                @endif
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">Medicine-Type</label>
            <div class="col-sm-9">
                <select class="form-control" name="product_type">
                    <option value="">Select</option>
                    <option value="tablet">Tablet</option>
                    <option value="syrup">Syrup</option>
                    <option value="injectable">Injectable</option>
                </select>
                @if($errors->has('product_type'))
                    
                @endif
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">Potency</label>
            <div class="col-sm-9">
                <input type="text" class="form-control" name="potency" placeholder="Potency">
                @if($errors->has('potency'))
                    
                @endif
            </div>
        </div>

        <div class="form-group">
            <label class="col-sm-3 control-label">Manufacturer</label>
            <div class="col-sm-9">
                <input type="text" class="form-control" name="manufact" placeholder="manufacturer">
                @if($errors->has('manufact'))
                    
                @endif
            </div>
        </div>

        @if(Session::has('message'))
            <div class="form-group">
                <label class="col-sm-3 control-label"></label>
                <div class="col-sm-9">
                    <div class="alert alert-info" role="alert"></div>
                </div>
            </div>
        @endif

        <div class="form-group">
            <label class="col-sm-3 control-label"></label>
            <div class="col-sm-9">
                <button type="submit" value="Add product" class="btn btn-default">Submit</button>
            </div>
        </div>
        
    </form>
    </div>



via Haider

Advertisement