Dropdownlist in a edit.blade.php form - laravel

please how can I put dropdown list in an edit form with the old selected value on default?
here is my example:
<div class="form-group">
{{ Form::label('Container', 'Container:') }}
{{ Form::select('Select_cont', $containers) }}
</div>
I don´t know where to put the code of the old new value selected in my view and what should it be.
Please don´t forget that when directing to edit.blade.php I wrote thisin the function of my controller
return View::make('audio.edit',array($container))
->with('containers', $containers)
thanks a lot for your help :)

Selected value is the 3rd param of Form::select, so:
{{ Form::select('Select_cont', $containers, $selectedPreviouslyKey) }}
How to get that key depends on your code and what's in the dropdown
Pass it to the view like this
return View::make('audio.edit',array('containers' => $containers, 'selectedPreviouslyKey' => $selectedKey));
or use compact() / with etc

The View::make call isn't entirely right. It should be:
return View::make('audio.edit')->with('containers', $containers);
or
return View::make('audio.edit')->withContainers($containers);
or
return View::make('audio.edit', array('containers' => $containers));
or
return View::make('audio.edit', compact('containers'));
Also: make sure $containers exists.
The documentation on the select tag. http://laravel.com/docs/html#drop-down-lists

Related

Missing required parameter on form action

First of all, I would like to say that my English is not so good and is my first time posting here, so excuse me if I did something wrong! Well, I am starting practicing with Laravel and I am trying to create a URL for users can like posts. my URL and controller is right now this Route::post('/post/{post}/like', [LikeController::class, 'postLike'])->name('post.like'); where post is the posts id that i am trying to pass through my form action attribute. Here is my form:
#props(['id'])
<div {{ $attributes->merge(['class' => 'card-footer d-flex justify-content-around']) }}>
<form action= "{{ route('post.like' , $id) }}" method="post" >
#csrf
<button>submit</button>
</form>
</div>
If you wonder if the id is not actually passed into the component, I checked {{ dd($id) }} and it printed it.
My controller is this which it doesn't actually do anything right now. I am just trying to pass the id:
class LikeController extends Controller
{
public function postLike(Post $post) {
dd($post);
}
}
After all this i am getting the error:
Missing required parameter for [Route: post.like] [URI: post/{id}/like] [Missing parameter: id]. (View: blog\resources\views\components\post\interaction.blade.php)
I am having two days to find the problem and I am still trying this moment I am sending this... I can't found where is the mistake! If you could help me would be much appreciated! Ty in advance
You need to pass the ID as an associative array in your route().
{{ route('post.like', ['id' => $id]) }}
If the named route defines parameters, you may pass the parameters as the second argument to the route function. The given parameters will automatically be inserted into the generated URL in their correct positions and you should name 'post' instead of 'id':
Route::post('/post/{post}/like', [LikeController::class, 'postLike'])->name('post.like');
And you can call this route anywhere
route('post.like', [$postId])
If that still doesn't work for you. Then it might be issue of Route Cache Run:
php artisan route:clear
Use same name parameter
When you use "id" keyword as a parameter in web.php then also same name pass in function argument in the controller
Route::post('/post/{id}/like', [LikeController::class, 'postLike'])->name('post.like');
Controller
class LikeController extends Controller
{
public function postLike(Post $id) {
dd($id);
}
}
Sorry for my bad English.
I have no idea this could be the problem but after many changes i cast the id of the post into an integer and it actually worked <form action= "{{ route('post.like' , (int)$id) }}" method="post" > . I don't know why i should cast the id into an integer because its already an integer by default and i checked that with {{ dd(getType($id)) }} and printed "integer"! I am really confused but it works! But i am not happy because i have no idea why i should cast an integer into an integer to make it work! Doesn't make any sense! If anyone has an answer to this would be great!

can I use route() to make a DELETE request in laravel

I'm using laravel and trying to delete something. Is it possible to specify the DELETE method on laravel's route()??
e.g
route('dashboard-delete-user', ['id' => $use->id, 'method'=> 'delete'])
or something like that??
EDIT:
What I meant was could I specify that in a link or a button in my blade template. Similar to this:
href="{{ route('dashboard-delete-user') }}
Yes, you can do this:
Route::delete($uri, $callback);
https://laravel.com/docs/master/routing#basic-routing
Update
If for some reason you want to use route only (without a controller), you can use closure, something like:
Route::get('delete-user/{id}', function ($id) {
App\User::destroy($id);
return 'User '.$id.' deleted';
});
No or at least I haven't figure out how to.
The only way for this to work out of the box would be to build a form to handle it. At the very minimum, you would need...
<form action="{{ route('dashboard-delete-user') }}" method="POST">
{{ method_field('DELETE') }}
{{ csrf_field() }}
<button type="submit" value="submit">Submit</button>
</form>
Or you can just create the get route which you are trying to link to and have it handle the logic. It doesn't need to be a route which only respondes to delete requests to delete a resource.
Yes you can, using a URL helper. https://laravel.com/docs/5.2/helpers#urls
There are several options to choose from.

Laravel input checkbox if selected on edit

I have an array that I am setting up as checkboxes
<?php $buslist = array('Brooklyn','Lakewood'); ?>
#foreach ($buslist as $buses)
{{Form::label('brooklyn_1',$buses)}}
{{Form::checkbox('BusList2[]', $buses,false, ['id'=> $buses]) }}
#endforeach
Then I switch it to an string with a , using implode
But when I try to edit my information, none of the checkboxes are selected and the information gets lost on update.
What code can i put into the blade checkboxes, if that bus is in my string list?
Checkboxes are finicky creatures. In your form you actually have to do something like this
{!! Form::hidden('new-group-user-member', 0) !!}
{!! Form::checkbox('new-group-user-member', true, NULL) !!} Member
When a checkbox is not checked nothing gets sent to the server so you never know when to change the value in the backend. If you add a hidden form field with the same name and a value of 0 it will get sent with the form even though the checkbox is not checked.
In third parameter use in_array($buses, $buslist) instead of false. Your blade code will look like this:
{{Form::checkbox('BusList2[]', $buses,in_array($buses, $buslist), ['id'=> $buses]) }}

laravel passing parameters from view to route

Using a view with user input. I then want to pass to a route. This what I found so far:
href="{{URL::to('customers/single'$params')}}"
I want to pass the user input as the above $params to my route. This is sample of my route:
Route::get('customer/{id}', function($id) {
$customer = Customer::find($id);
return View::make('customers/single')
->with('customer', $customer);
As soon as I can pass the parameter I can do what I want with the route, which I know how.
Basically you can pass parameter to routes by doing:
Route::get('user/{name}', function($name)
{
//
})
->where('name', '[A-Za-z]+');
In your anchor tag, instead of doing href={{URL...}}, do something like:
{{ URL::to('user/$param') }}
For more information on routing, visit link
This is what I have and works:
<a <button type="button" class="buttonSmall" id="customerView" href="{{URL::to('customer',array('id'=>'abf'))}}" >View</button></a>
But I need the array value 'abf' to be the value of a textbox.
This worked for me in my view anchor tag
href="{{ URL::to('user/'.$param) }}"
instead of what was specified above
href="{{ URL::to('user/$param') }}"
You can user it in View as I used:
<a class="stocks_list" href="/profile/{{ Auth::user()->username }}">Profile</a>
Hope it helps you.

Multiple select dropdown in laravel

Here is my code in the view, I want to display a dropdown that contains the selected data in an edit view.
{{Form::select('Select_targets[]', $_targets,Input::old('Select_target', $profile->Target_idTarget-1), array('multiple' => true))}}
I tried this code but it just displays one selected value. Please I need your help!
Thanks in advance
I think you have an error around your old input and default values for the dropdown. Here is a stripped down example that does work:
Controller:
//current selected
$target = 3;
/array with options
$targets[1] = 'target 1';
$targets[2] = 'target 2';
$targets[3] = 'target 3';
return View::make('form')->with('targets',$targets)->with('target',$target);
View:
{{ Form::open() }}
{{ Form::select('targets[]', $targets, Request::old('targets') ? Request::old('targets') : $target, array('multiple' => true)); }}
{{ Form::submit('send') }}
{{ Form::close() }}
use wherein method of laravel incase you want to take all the option as query from request

Resources