I am trying to do that when i add a new branch, I want to create an Admin for this branch, but in this the branch form have an input field of branch Admin when i am adding a new branch it does not create a branch because the Admin input field is empty, because there is no Admin available. And i want to do that whether it is or not the Admin is existing or the branch is existing, when i am adding/creating the branch or branch-Admin it not give the validation error.
Add-Branch View:
<div class="col-xs-12 col-sm-12 col-md-6 col-lg-6">
<form class="form-horizontal branch-form" action="/add/branch" 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 $compny)
<option value=""></option>
@endforeach
</select>
@if($errors->has('company_id'))
@endif
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">BranchAdmin</label>
<div class="col-sm-9">
<select class="form-control" name="user_id">
<option value="">Select</option>
@foreach($user as $usr)
<option value=""></option>
@endforeach
</select>
@if($errors->has('user_id'))
@endif
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Branch-Name</label>
<div class="col-sm-9">
<input type="text" class="form-control" name="name" placeholder="Name">
@if($errors->has('name'))
@endif
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Email</label>
<div class="col-sm-9">
<input type="text" class="form-control" name="email" placeholder="Email">
@if($errors->has('email'))
@endif
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Address</label>
<div class="col-sm-9">
<input type="text" class="form-control" name="address" placeholder="Location">
@if($errors->has('address'))
@endif
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Contact</label>
<div class="col-sm-9">
<input type="text" class="form-control" name="contact" placeholder="Contact">
@if($errors->has('contact'))
@endif
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Open-Hours</label>
<div class="col-sm-9">
<input type="text" class="form-control" name="open_hours" placeholder="Shop-Open-Hours">
@if($errors->has('open_hours'))
@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 branch" class="btn btn-default">Submit</button>
</div>
</div>
</form>
</div>
Branch-Admin View:
<div class="col-xs-12 col-sm-12 col-md-6 col-lg-6">
<form class="form-horizontal register-form" action="/make/branch/admin" method="POST">
<div class="form-group">
<label class="col-sm-3 control-label">Name</label>
<div class="col-sm-9">
<input type="text" class="form-control" name="name" placeholder="Name">
@if($errors->has('name'))
@endif
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Email</label>
<div class="col-sm-9">
<input type="email" class="form-control" name="email" placeholder="Email">
@if($errors->has('email'))
@endif
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Password</label>
<div class="col-sm-9">
<input type="password" class="form-control" name="password" placeholder="Password">
@if($errors->has('password'))
@endif
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Contact</label>
<div class="col-sm-9">
<input class="form-control" name="phone" placeholder="Contact number">
@if($errors->has('phone'))
@endif
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">CNIC</label>
<div class="col-sm-9">
<input type="text" id='cnic' class="form-control" name="cnic" placeholder="CNIC">
@if($errors->has('cnic'))
@endif
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Address</label>
<div class="col-sm-9">
<textarea class="form-control" type="text" name="address" placeholder="Address" rows="5"></textarea>
@if($errors->has('address'))
@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="Register" class="btn btn-default">Register</button>
</div>
</div>
</form>
</div>
Branch Controller:
public function savebranchinfo(Request $request){
$validator = Validator::make($request->all(),[
'company_id' => 'required',
'user_id'=>'required',
'name' => 'required|min:5',
'email' =>'required|unique:branch,email',
'address' =>'required',
'contact' =>'required|max:11|unique:users,contact',
'open_hours' =>'required',
]);
if($validator->passes()){
$branch = new Branch();
$branch->company_id = $request->company_id;
$branch->user_id = $request->user_id;
/*dd($request->company_id);*/
$branch->branch_name = $request->name;
$branch->email = $request->email;
$branch->address = $request->address;
$branch->contact = $request->contact;
$branch->open_hours = $request->open_hours;
if($branch->save() || Auth::user()->type === 'BranchAdmin'){
$request->session()->flash('message','Successfully save!!');
return redirect('/add/branch');
}else{
return redirect('/add/branch')->withErrors($validator)->withInput();
}
}
BranchAdmin Controller:
public function saveadmininfo(Request $request){
$validator = Validator::make($request->all(), [
'name' => 'required|min:4',
'email' => 'required|email|unique:users',
'password' => 'required|min:4|',
'phone' => 'required',
'cnic' => 'required|unique:users,CNIC|min:15|max:15',
'address' => 'required'
]);
if ($validator->passes()) {
$user = new User();
$user->name = $request->name;
$user->email = $request->email;
$user->password =bcrypt($request->password);
$user->contact = $request->phone;
$user->CNIC = $request->cnic;
$user->address = $request->address;
$user->verified = true;
$user->verification_token = '';
$user->type = 'BranchAdmin';
if ($user->save()) {
$request->session()->flash('message', 'Successfully registered Branch Admin!!');
return redirect('/make/branch/admin');
}
}else{
return redirect('/make/branch/admin')->withErrors($validator)->withInput();
}
}
}
Model:
public function branch(){
return $this->hasOne('App\Branch', 'user_id', 'id');
}
via Haider