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
Related
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>
I am creating a blog in Laravel. I have two tables - posts and categories.
When i want to edit one post, i have a problem with categories. My post controller is sending categories to the view, i can see them while editing, but i don't know how to set the current category in the select option.
The tables are conected:
public function posts(){
return $this->hasMany('App\Post');
}
public function category(){
return $this->belongsTo('App\Category');
}
Controller:
public function edit(Post $post)
{
$categories = Category::all();
return view('admin.posts.edit', compact('post'))->withCategories($categories);
}
View:
<form action="{{ route('posts.update', ["post" => $post->id]) }}" method="post" enctype="multipart/form-data" class="form-horizontal">
#csrf
<div class="form-body">
<div class="form-group">
<label class="col-md-3 control-label">Naslov</label>
<div class="col-md-4">
<input type="text" name='title' value='{{ old("title", $post->title) }}' class="form-control">
#if($errors->has('title'))
<div class='text text-danger'>
{{ $errors->first('title') }}
</div>
#endif
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label">Kategorija</label>
<div class="col-md-4">
<select class="form-control" name="category_id">
#if(count($categories) > 0)
#foreach($categories as $category)
<option value="{{ $category->id }}">{{ $category->name }}</option>
#endforeach
#endif
</select>
#if($errors->has('category_id'))
<div class='text text-danger'>
{{ $errors->first('category_id') }}
</div>
#endif
</div>
</div>
How can i make the category display the current category of the selected post in the option select like i have for title? value='{{ old("title", $post->title) }}'
To show the currently associated Category, you can set a default "selected" option based on the $post->category_id:
<option value="{{ $category->id }}" {{ old('category_id', $post->category_id) == $category->id ? 'selected' : '' }}>{{ $category->name }}</option>
This a simple ternary that sets the selected attribute of one of the options based on the old() value, or, if old() is not set, then based on the value of $post->category_id
What you are looking for is the selected property of the <option> element.
#forelse ($categories as $category)
<option value="{{ $category->id }}" {{ $category->id == old('category_id', $post->category_id) ? 'selected' : '' }}>
{{ $category->name }}
</option>
#endforelse
A minor change as well in the in you blade template to make use of the #forelse directive for less code.
You could also change your controller code a bit:
return view('admin.posts.edit', compact('post', 'categories'));
Further the laravelcollective/html package might help you a lot in creating forms.
For some reason the value is not outputting when I press submit. There is always an error it basically says there is no data.
<div class="form-group">
<div class="">
<label for="category">Size</label>
<br>
<select name="category" class="form-control">
#foreach($size as $s)
<option value="{{ $s->id }}">{{ $s->size }}</option>
#endforeach
</select>
</div>
</div>
just using pluck and map into Form select
in your controller
$model = Model::pluck('size', 'id')->toArray();
return view('view', compact('model'))
in view
{{ Form::select('category', $model, null, ['class' => 'form-control']) }}
I have created a drop-down for categories and subcategory. It works fine when i submit the form, but when I edit the form, category field does not come with refilled data from the database, category drop-down come like it show in create form.
here is my edit:
<div class="form-group">
{!! Form::label('category','Category:') !!}
<select name="category" id="category" class="form-control input-sm">
#foreach($s as $k)
<option value="{{ $k['id'] }}">{{ $k['name'] }}</option>
#endforeach
</select>
</div>
<div class="form-group">
{!! Form::label('subcategory','Subcategory:') !!}
<select name="subcategory" id="subcategory" class="form-control input-sm">
<option value=""></option>
</select>
</div>
Controller:
public function edit($id)
{
// get the event
$event = Event::findOrFail($id);
$s = Category::all()->where('parent_id','=','0');
$r = Event::all();
$daysOfWeek = unserialize(Event::find($id)->days_of_week);
// show the edit form and pass the event
return view('event.edit',compact('event','s','r','daysOfWeek'));}
I haven't used relations for the dropdown, I have used jquery and ajax to select subcategory after I select category.
What can i do to get the value stored in database when I do edit form?
I got my answer thanks every one for help!
View:
<div class="form-group">
{!! Form::label('category','Category:') !!}
<select name="category" id="category" class="form-control input-sm">
#foreach($s as $k)
#if($k['id'] == $m)
<option value="{{ $k['id'] }}" selected="{{$m}}">{{ $k['name'] }}</option>
#else
<option value="{{ $k['id'] }}">{{ $k['name'] }}</option>
#endif
#endforeach
</select>
</div>
<div class="form-group">
{!! Form::label('subcategory','Subcategory:') !!}
<select name="subcategory" id="subcategory" class="form-control input-sm">
#foreach($subcat as $k)
#if($k['name'] == $subid)
<option value="{{ $k['id'] }}" selected="{{$subid}}">{{ $k['name'] }}</option>
#else
<option value="{{ $k['id'] }}">{{ $k['name'] }}</option>
#endif
#endforeach
<option value=""></option>
</select>
</div>
Controller:
public function edit($id)
{
$event = Event::findOrFail($id);
$s = Category::where('parent_id','=','0')->get();
$r = Event::all();
$m = Event::find($id)->category;
$subid = Event::find($id)->subcategory;
$subcat = Category::where('parent_id','=',$m)->get();
$daysOfWeek = unserialize(Event::find($id)->days_of_week);
return view('event.edit',compact('event','s','subcat','subid','r','daysOfWeek','m'));
}
There is a mistake in your Controller method edit()
Your code:
$s = Category::all()->where('parent_id','=','0'); // wrong
Isn't that supposed to be:
$s = Category::where('parent_id','=','0')->get(); // correct
You are already fetching the categories with all, and then passing the where condition, hence it is not showing you the results. where condition returns the instance of Illuminate\Database\Eloquent\Builder and not the desired results.
Update the wrong line with the correct one and the result should appear as you want.
Trying to improve the code of my app and just migrated to L5. I used to call models in my views and I know this is not best practice - I should separate data calling and views completely.
However how do you deal with a situation like this:
<div class="field">
<label for="country">Country<sup>*</sup></label>
<select class="" id="country" name="country">
<option value="{{{ old('country') ? old('country') : '' }}}">{{{ old('country') ? App\Country::find(old('country'))->country_name : '' }}}</option>
#foreach ($countries as $studio_country)
<option value="{{ e($studio_country->id) }}">{{ e($studio_country->country_name) }}</option>
#endforeach
</select>
#if ($errors->has('country'))
<div class="alert alert-warning" role="alert">
{{ $errors->first('country') }}
</div>
#endif
<br>
</div>
Basically if there is an input and the input did not pass the validation the page refreshes with the old input and the error message. I need to extract the name of the country from the DB since I only have its ID.
You would want to do something like this:
<select id="country" name="country">
#foreach ($countries as $studio_country)
<option
value="{{ $studio_country->id }}"
{{ $studio_country->id === old('country') ? 'selected' : '' }}>
{{ $studio_country->country_name }}
</option>
#endforeach
</select>