How to get data from db to layout in Laravel 8.* - php

So I know how to retrieve data to views(using classes in controller)
but how can I return some data to layout which doesnt(or rather i dont know where they are) have its classes where i could get code to do so.
here is my form in which i would like to put cities name.
I always do it by giving to my frontend controller data from db using
$events = DB::select('select * from events');
return view('frontend/index', ['event' => $events]);
Edit
How to pass $city->id from loop to action?
My app.blade form:
<form method="POST" action="{!! route('cityView', ['id' => $city->id]) !!}" class="form-inline">
<div class="form-group">
<select name="city_id" class="form-control">
<option>xd</option>
#foreach(\App\Models\City::all() as $city)
<option value="{{ $city->id }}">{{ $city->name }}</option>
#endforeach
</select>
</div>
<button type="submit" class="btn btn-info">Search</button></a>
</form>
But how can I do it for a layout?
Would making a layout controller help with it? Or maybe make another class inside existing controller and return view of layout view?
Second solution would be adding this form to every single view since there will be only 3 of them but i prefer solution with app.blade

You can call data directly from layout something like this:
<form method="POST" action="#" class="form-inline">
<div class="form-group">
<select name="room_size" class="form-control">
<option></option>
#foreach(\App\Models\City::all() as $city)
<option value="{{ $city->id }}">{{ $city->name }}</option>
#endforeach
</select>
</div>
<button type="submit" class="btn btn-info">Search</button>
</form>

Related

How to show data from table via Model except a custom row

I'm using Laravel 8 for my project and in this project, I have made a page for creating users by admins.
And basically, each user has a role, so in order to show the roles, I have added this:
<div class="form-group">
<label class="col-sm-2 control-label">Role</label>
<select class="form-control" name="role" dir="ltr">
#foreach(\App\Models\Role::all() as $role)
<option value="{{ $role->id }}">{{ $role->name }}</option>
#endforeach
</select>
</div>
And all of roles goes like this:
Now I don't want to print Super Admin in this form field. So my question is, how can I prevent \App\Models\Role::all() from printing Super Admin in select option?
I would really appreciate any idea or suggestion from you guys...
Thanks in advance.
Instead of using all, you could do something like where('name', '!=', 'SuperAdmin')->get() or whereNotIn('name', [...hidden_roles])->get()
Now this should also be in your controller as the view shouldn't be interested with the business logic
Even better create a method in your user model called something like getNonAdminRoles (and you can add to this later if there are more hidden roles), or a more laravel way and create a scope https://laravel.com/docs/8.x/eloquent#local-scopes
You can do something like this in the view:
<div class="form-group">
<label class="col-sm-2 control-label">Role</label>
<select class="form-control" name="role" dir="ltr">
#foreach(\App\Models\Role::all() as $role)
#if($role->slug != 'super-admin')
<option value="{{ $role->id }}">{{ $role->name }}</option>
#endif
#endforeach
</select>
</div>

How to pass all the rows from table to blade view?

Controller:
public function index(){
$bike1 = Bike::get()->first();
$bike2 = Bike::get()->skip(1)->first();
return view('compare.index')->with(['bike1'=> $bike1, 'bike2'=> $bike2 ]);
}
Here I have passed only the first and second row to the blade view.
Blade view:
Select first bike
<form action ="{{ route('compare.get', [$bike1->id, $bike2->id]) }}" method="get">
<div class="first_bike" style="width:200px;">
<select name="bike1">
<option value="{{ $bike1->id }}"> {{ $bike1->name }}</option>
</select>
</div>
<br>
Select second bike
<div class="second_bike" style="width:200px;">
<select name="bike2">
<option value="{{ $bike2->id }}"> {{ $bike2->name }}</option>
</select>
</div>
<br>
<button type="submit"> Compare </button>
</form>
The get controller passes the value to another view that displays the data. It works.
How can I pass all the rows to the blade view? I know I can use all functions and loop the variable in the blade to get all the rows. Like this
#foreach($bike1 as $b1)
<option value="{{ $b1->id }}"> {{ $b1->name }}</option>
#endforeach
#foreach($bike2 as $b2)
<option value="{{ $b2->id }}"> {{ $b2->name }}</option>
#endforeach
If I do this, then how can I use the $b1 and $b2 as route parameters in the action properties in the form?
Can I manipulate the solution from the controller leaving my blade as it is? Or how do I change the blade view in order to achieve my goal? I am stuck
well if I am getting you correctly your Controller should be like
public function index(){
$bike1 = Bike::all();
$bike2 = Bike::all();
return view('compare.index')->with(['bike1'=> $bike1, 'bike2'=> $bike2 ]);
}
Then you modify your view to
Select first bike
<form action ="{{ route('compare.get'}}" method="get">
<div class="first_bike" style="width:200px;">
<select name="bike1">
#foreach($bike1 as $b1)
<option value="{{ $b1->id }}"> {{ $b1->name }}</option>
#endforeach
</select>
</div>
<br>
Select second bike
<div class="second_bike" style="width:200px;">
<select name="bike2">
#foreach($bike2 as $b2)
<option value="{{ $b2->id }}"> {{ $b2->name }}</option>
#endforeach
</select>
</div>
<br>
<button type="submit"> Compare </button>
</form>
For submitting you don't have to put them as route parameters,, just let the form submit and get the GET variables in the Controller that you are submitting the form to,,You can use the Request object to do this
$bike1 = Request->input('bike1');
$bike2 = Request->input('bike2');
hope you understand this,, just make sure the Request object is set as a parameter in the controller that you are submitting to

Passing multiple items from selectbox is not working in laravel

In my laravel project, am showing select box which contains various clinics, each clinic have api_key and id . I want to pass both id and api_key to Laravel Controller when a form is submitted.
But now am getting only api_key .ID is not getting.
Following is my code in view page
<form method="post" action="{{ route('viewcode') }}" name="cliniclist" id="cliniclist" enctype="multipart/form-data" novalidate>
{{ csrf_field() }}
<select class="selectpicker" data-style="btn-info btn-fill btn-block" id="clinic_id" name="clinic_id" key="clinic_key">
#foreach($clinics as$clinic)
<option value="{!! $clinic->api_key !!}" id="{!! $clinic->api_key !!}" clinic_key="{!! $clinic->clinicID !!}">{!! $clinic->clinicName !!}</option>
#endforeach
</select>
<div class="col-xs-12 col-sm-12 col-md-12 form-action">
<button type="submit" class="btn btn-fill btn-info">Submit</button>
Cancel
</div>
</form>
Here in my code am printing clinic id in clinic_key attribute
Following is my code in controller
public function store(Request $request)
{
echo $request->clinic_key;
die();
$clinic_api_key = $request->clinic_id;
return view('locations.viewcode')->with(['clinic_api' => $clinic_api_key]);
}
What is the problem here
You can do it simply like
<option value="{!! $clinic->api_key.'-'.$clinic->id !!}">{!! $clinic->clinicName !!}</option>
And inside controller do it this way
$split = explode("-",$request->clinic_id);
$clinic_api_key = $split[0];
$clinic_id = $split[1];
Updated Answer. With little jquery you can do in this way.
<form method="post" action="{{ route('viewcode') }}" name="cliniclist" id="cliniclist" enctype="multipart/form-data" novalidate>
{{ csrf_field() }}
<select class="selectpicker" data-style="btn-info btn-fill btn-block" id="clinic_id" name="clinic_id" key="clinic_key">
#foreach($clinics as$clinic)
<option value="{!! $clinic->api_key !!}" data-clinic_id="{!! $clinic->clinicID !!}">{!! $clinic->clinicName !!}</option>
#endforeach
</select>
<input type="hidden" name="api_key" id="api_key">
<input type="hidden" name="id" id="id">
<div class="col-xs-12 col-sm-12 col-md-12 form-action">
<button type="submit" class="btn btn-fill btn-info">Submit</button>
Cancel
</div>
</form>
Jquery Code, as you can see i have placed two hidden inputs inside your form, so you will access these two inputs inside your controller except select name.
$(document).on('change','#clinic_id',function(){
var clinic_api_key = $('option:selected',this).val();
var clinic_id = $('option:selected',this).data('clinic_id');
$("#api_key").val(clinic_api_key);
$("#id").val(clinic_id);
});
Now in controller
$request->api_key;
$request->id;
Option tag can have only one value. Since you are trying to get 2 values there, you might want to join them with some special characters like hyphen "-" or maybe colon ":". In that way you can separate them by that special character in our controller later.
<select class="selectpicker" data-style="btn-info btn-fill btn-block" id="clinic_id" name="clinic_id" key="clinic_key">
#foreach($clinics as$clinic)
<option value="{!! $clinic->api_key.'-'.$clinic->id !!}" id="{!! $clinic->api_key !!}" clinic_key="{!! $clinic->clinicID !!}">{!! $clinic->clinicName !!}</option>
#endforeach
</select>
And then in your controller just get the value of clinic_id field and separate them.
public function store(Request $request)
{
//make sure clinic_id and id does not contain '-' in their values
$clinic_data = explode('-',$request->clinic_id);
$clinic_id = $clinic_data[1];
$clinic_api_key = $clinic_data[0];
return view('locations.viewcode',compact('clinic_id','clinic_api_key'));
}
for the above code to work. please make sure your api_key and id data from the database does not contain "-" or will break. In that case, use some other separator.

Adding data with multiple tables in one form many to many relationship in laravel 5.4

I am trying to insert with two tables with many to many relationship: Assignments and Collectors. I also make a intermediate or pivot table between them named assignment_collector.
create.blade.php in assignment folder
<form method="POST" action="{{ route('assignments.store') }}">
{{ csrf_field() }}
<div class="form-group">
<label for="area_id">Area: </label>
<select class="form-control" name="area_id">
#foreach ($areas as $area)
<option value= "{{ $area->id }}">
{{ $area->campus }} {{ $area->building }} {{ $area->floor }}
</option>
#endforeach
</select>
</div>
<div class="form-group">
<label for="mts_id">MTS: </label>
<select class="form-control" name="mts_id">
#foreach ($mts as $mt)
<option value= "{{ $mt->id }}">
{{ $mt->location }}
</option>
#endforeach
</select>
</div>
<div class="form-group">
<input type="hidden" class="form-control" id="status" name= "status" value="Active">
</div>
<div class="form-group">
<label for="collector_id">Collector: </label>
<select class="form-control" name="collector_id">
#foreach ($assignments as $assignment)
#foreach($assignment->collectors as $collector)
<option value= "{{ $collector->id }}">
{{ $collector->firstname }} {{ $collector->lastname }}
</option>
#endforeach
#endforeach
</select>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Add</button>
</div>
</form>
AssignmentsController
public function store(Request $request)
{
$this->validate($request, [
'area_id' => 'required',
'mts_id' => 'required',
'status' => 'required',
'collector_id' => 'required'
]);
$assignment = new Assignment();
$assignment->area_id = $request->area_id;
$assignment->mts_id = $request->mts_id;
$assignment->status = $request->status;
$assignment->save();
$assignment_collector = new AssignmentCollector();
$assignment_collector->collector_id = $request->collector_id;
$assignment_collector->assignment_id = $assignment->id;
$assignment_collector->save();
return redirect('/assignments');
}
Assignment Model
public function collectors()
{
return $this->belongsToMany(Collector::class);
}
Collector Model
public function assignments()
{
return $this->belongsToMany(Assignment::class);
}
I got an error on this: App\AssignmentCollector since there is no AssignmentCollector and as I read the tutorials, you don't need to make a model for the pivot table. I reading the other articles but I get even more confused. Please help me because I'm still new in Laravel and I have get confused. Thanks!
You don't need a Model for your assignment collector table. What you want to do is create the Assignment then assign
$assignment->collectors()->create([
// all the collector data
]);

Laravel Dropdown Form Get Form Database

I am using laravel 5.4 . I am trying to get data form Department table and data come also the problem there more dropdown menu come also please tell how can i solve this problem. Thanks advance.[
<div class="form-group">
{{Form::label('department','Department')}}
#foreach($department as $value)
{{Form::select('department',[$value->name], ' ', array('placeholder' => 'Select Department','class'=>'form-control'))}}
#endforeach
</div>
It's not entirely clear what you're after. Your current code seems to output several select boxes, one for each department and my best guess is that you want to output only one select box, with each department as an option in it. You can achieve that by doing something like this (You may have to adapt this code slightly to suit your needs):
The manual approach
<div class="form-group">
<label for="department">Department</label>
<select id="department" name="department" class="form-control">
<option value="">Select Department</option>
#foreach ($departments as $department)
<option value="{{ $department->id }}">{{ $department->name }}</option>
#endforeach
</select>
</div>
Using Laravel Collective's Form Builder
<div class="form-group">
{!! Form::label('department', 'Department') !!}
{!! Form::select('department', ['' => 'Select Department'] + $departments->pluck'name', 'id')) !!}
</div>
Try to do this way :-
<div class="form-group">
{{Form::label('department','Department')}}
<select class="form-control" name="department">
#foreach($department as $value)
<option value="$value">$value</option>
#endforeach
</select>
</div>

Categories