armancs's avatar

Get value for dropdown list and input it database

Hello laravel experts, I am going to add a drop down list and getting data from database.its done. but the problem is i want to add extra two field and those field data store in database. I mean when i choose and select a value from dropdown just like coosing country from country table and in city field i want to add city in city table how can i do that please help me.

My code below->

in view file :-

        <form class="cmxform form-horizontal tasi-form" action="{{URL::to('university-add')}}" method="post"  enctype="multipart/form-data">
                <input type="hidden" name="_token" value="<?php echo csrf_token(); ?>">
                <div class="box-body">                         
                   
                  
                 
                    <div class="form-group">
                        <label for="country_name" class="col-sm-3  control-label">Select Country : <span style="color:red">&nbsp;*</span></label>
                        <div class="col-sm-7">
                            <select class="selectpicker form-control" id="number" data-live-search="true">
                                <option>Select Country</option>
                                @foreach($country as $country)
                                <option value="{{$country->id}}">{{$country->name}}</option>
                                @endforeach
                            </select>
                        </div>
                    </div>  
                    <div class="form-group">
                        <label for="city" class="col-sm-3  control-label">Select City : <span style="color:red">&nbsp;*</span></label>
                        <div class="col-sm-7">
                            <select class="selectpicker form-control" id="number" data-live-search="true">
                                
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="university_address" class="col-sm-3 control-label">University Name :</label>
                        <div class="col-sm-7">
                            <input type="text" name="university_address" class="form-control">
                        </div>
                    </div>  

                    <div class="box-footer">
                        <div class="col-md-3"></div>
                        <div class="col-md-6">
                            <button class="btn btn-success" type="submit">Save </button>&nbsp;&nbsp; 
                            <a href="{{URL::to('university-info')}}"><button class="btn btn-danger" type="button">Cancel &nbsp;</button></a>
                        </div>
                        <div class="col-md-3"></div>
                    </div>

Controller:-

public function university_add(){
    $country             = DB::table('countries_tbl')
                         ->groupBy('name')
                         ->get();
    $city             = DB::table('region_tbl')
                         ->groupBy('name')
                         ->get();
    

    return view('adminpanel.university-add',compact('country','city'));
}

public function store(Request $request) {
    
    $university_add      = new UniversityModel;
    $university_add->university_name=  $request->input('name');
    $university_add->save();


  
}


Route:

    Route::resource('university-info', 'Adminpanel\UniversityController');
    Route::post('university-add','Adminpanel\UniversityController@university_add');

Error i am getting when submit :

(1/1) MethodNotAllowedHttpException

and don't get the city name from database although it will be dependent but i am not able to make this dependent dropdown.

0 likes
10 replies
m7vm7v's avatar

Hi @armancs It depends if you want to associate the Country with he City, which I believe you would want to. If so in the form you have to make the country list as a radio buttons or better - select from the list and have to give it a name attribute and the options with value="country_id".

Then wrap all that in a with a submit button and action that points to a controller method, in which you could simply do a

$city = City::create([
    'name' => request('city_name'),
    'country_id' => request('country_id')
]);
armancs's avatar

don't understand what you are saying. here the dropdown list is with search option.i got the value from country table.now i want to add the city from input field to insert data in another table.

m7vm7v's avatar

Lets walk trough the requirements one more time - Is it the case you want that when a user visits a page there will be a list of countries and an input field for a city name. When the user clicks on a country and fill the city name then for example click on a button (or you could do it trough AJAX without a button) 'Create' then in the database you want to see that there is a new record in the cities table with name, country_id or name. Is that all correct or I've miss understood what you try to achieve. If not then could you explain briefly and provide pictures if you believe that would help. Thanks

Cronix's avatar

here the dropdown list is with search option

What "dropdown list"? I don't see any <select> elements, just a text input for university_address and some anchor tags.

armancs's avatar

@m7vm7v seems like you are talking.Please check my updated code thanks.Now i want to put city value to city table dependent on country dropdown.the city field input by me.

m7vm7v's avatar

@armancs If that so then my first reply remains what you would need to do. Just in the I would use the the country->id for the value and the name after. So when you hit the controller for the country you would have the needed id with no need to query that information by the name. Hope that makes sense.

armancs's avatar

@m7vm7v i don't need to assign for insert country id? like city insert

Cronix's avatar

So, you want to select a country from the dropdown, and have a field to enter a city name for that country? Ok, let's start with the city name form field. Where is that?

I see a select input element for the country.

I see a text input for a "university address".

I don't see anything for a city name.

Cronix's avatar
Cronix
Best Answer
Level 67
// changed name to country_id
// changed options so value was the actual country id, text is country name
<select class="selectpicker form-control" name="country_id" id="countries" data-live-search="true">
    <option>---------Select Country----------</option>
    @foreach($countries as $country)
    <option value="{{$country->id}}">{{$country->name}}</option>
    @endforeach
</select>

// added input for city name
<input type="text" name="city_name">

in controller:

$country_id = $request->country_id;
$city_name = $request->city_name;

// save how you will...

You don't show your form open tags. You will need to have to form action go to the controller that will store the new city. The form method should be a post.

You don't show whether you have a csrf token on the form. You will need one.

Obviously, you also need a route.

armancs's avatar

I don't understand @Cronix can you please explain again.Although i am new in laravel. thanks for reply. i updated my code please check.

Please or to participate in this conversation.