Monday, April 10, 2017

How i do using laravel to get user related things

How i do that when One Branch Admin login and add medicines it show only its own branch name and the company name in the input field, which the branch it is. but when i do its show all the branches and all the companies names which is not related to this branch.

Controller:

public function getmedicineform(){
        $compny = Company::all();
        $branch = Branch::all()->unique('user_id','=', Auth::user()->id)->uniqueStrict();
        $user = User::where('id','user_id')->whereIn('id', $branch)->get();
        return view("medicinesdetails.addProduct")->with('company', $compny)->with('branch',$branch)->with('user',$user);
    } 

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">Company</label>
            <div class="col-sm-9">
                <select class="form-control" name="company_id">
                    <option value="">Select</option>
                    @foreach($company as $compnyy)
                        <option value=""></option>
                    @endforeach
                </select>
                @if($errors->has('company_id'))
                    
                @endif
            </div>
        </div>

        <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>

Model:

class Medicine extends Model
{
    protected $table = 'medicines';
    public function medicine(){
        return $this->hasOne('App\Medicine', 'user_id', 'id');
    }



via Haider

Advertisement