mightyteja
mightyteja

Reputation: 913

unable to redirect records based on the dynamic address

I am trying to link edit, View and delete buttons to the records. But I am unable link it to corresponding routes.

Attached the image of what I trying to develop.

Add dynamic select dropdown

Blade file:

    <div class="container">
        {!!Form::open()!!}
        <div class="form-group">
            <label for=""> District</label>
            <select name="district_option" id="district_option" class="form-control">
                <option value="0" disabled="true" selected="true">------ Select District --------</option>
                @foreach ($districts as $key => $district)
                <option value="{{$district->id}}">{{$district->district_name}}</option>
                @endforeach
            </select>
        </div>
        <div class="form-group">
            <label for=""> Municipality</label>
            <select name="municipal_option" id="municipal_option" class="form-control">
                <option value="0" disabled="true" selected="true">------ Select District First--------</option>
            </select>
        </div>
<table class="table table-striped" >
    <thead>
        <tr> 

           <td> Bank Name</td>
        <td>Bank Acc No </td>
        <td>Bank IFSC Code</td>
        <td>Action</td>
        <td>Action</td>
        </tr>        
    </thead>
            <tbody id="bank_list">
                <tr> 
                        {{-- <td>1</td>
                        <td>1</td>
                        <td>1</td> --}}
                </tr>
            </tbody>
</table>
        {!!Form::close()!!}

        <div class="container">
            <h1 id="bank_name"></h1>
        </div>
    </div>


    @include('admin.includes.footer')
    <script>
            $('#district_option').on('change',function(e){
        console.log(e);
        var district_id = e.target.value;
        var BASEURL = "{!! url('admin/bank/') !!}";
        console.log(district_id);
        $.get( BASEURL + '/municipalview?district_id=' + district_id, function(data){
            console.log(data);
            $('#municipal_option').empty();
            $('#municipal_option').append('<option value="0" disabled="true" selected="true">------ Select District First--------</option>');
         $.each(data,function(index, municipalObj){
            $('#municipal_option').append('<option value="'+ municipalObj.id +'"> '+ municipalObj.municipal_name +' </option>');
               })
                });
                }); 


        $('#municipal_option').on('change',function(e){
        // console.log(e);  
        var municipal_id = e.target.value; 
        var BASEURL = "{!! url('admin/bank/') !!}";
        console.log(municipal_id);
        $.get( BASEURL + '/bankview?municipal_id=' + municipal_id, function(data){
            console.log(data);
            $("#bank_list tr").remove();
            $.each(data,function(index, bankObj){
                $('#bank_list').append('<tr class=""><td> ' + bankObj.bank_name + '</td> <td> ' + bankObj.bank_ac_no + ' </td>  <td> '+ bankObj.bank_ifsc_code +' </td> <td> <button class="btn btn-primary"> <a href="{!! url('bank/{{$bank->id}}/edit')!!}"> Edit </a>  </td> <td> </td>    </tr>');
            });

                });
                }); 


    </script>

Route file:

Route::resource('bank', 'bankController');
Route::get('admin/bank/municipalview/', 'bankController@municipalView');
Route::get('admin/bank/bankview/', 'bankController@bankView');

Controller File:

    public function municipalView()
{ 

    $districts_id = Input::get('district_id');
    $municipalities = Municipality::where('district_id', '=', $districts_id )->get();
    return response()->json($municipalities) ;
}   

public function bankView(Request $request)
{

    $municipals_id = Input::get('municipal_id');
    $banks = Bank::where('municipal_id', '=', $municipals_id )->get();
    return response()->json($banks) ;
}  

The URL in the HREF of a is called as it is and when it loads the ID is not fetched instead the URL is parsed

http://localhost/public/bank/%3C?php%20echo%20e($bank-%3Eid);%20?%3E/edit

And to delete button, I would like to pass this,

{!!Form::open(['action' => ['bankController@destroy', $bank->id], 'method' =>
                            'POST'])!!}
                            {{Form::hidden('_method', 'DELETE')}}
                            {{Form::submit('Delete', ['class' => 'btn btn-danger'])}}
                            {!!Form::close()!!}

Would this be feasible

Please assist me on this. Please suggest me if my approach is correct on this as I am new to development.

Upvotes: 1

Views: 42

Answers (2)

mightyteja
mightyteja

Reputation: 913

Answer to the Second question: Conversion of Form collective to normal form - Addition of Delete button.

To do this via a regular link instead of through AJAX or another type of form request we can set up a special route that will respond to a normal GET request:

In routes, define this in addition to the resource:

Route::get('bank/{bank}/delete', 'bankController@delete');

The Form collective can be re written as

<form action="bank/'+ bankObj.id +'/delete"> <input type = "hidden" name = "_method" value = "DELETE"><input type="submit" class = "btn btn-danger"/></form>

Note: For future reference!!

Upvotes: 0

Laerte
Laerte

Reputation: 7083

Since you are trying to print an action route from a Javascript code, it is trying to print the php echo code directly, and not the result of the function.

You should do something like:

<a href="/your_route/' + bankObj.id + '">

Upvotes: 1

Related Questions