无法根据动态地址重定向记录

时间:2018-12-11 14:40:45

标签: php laravel laravel-5 laravel-blade

我正在尝试将“编辑”,“查看”和“删除”按钮链接到记录。但我无法将其链接到相应的路线。

附上我要开发的图像。

Add dynamic select dropdown

刀片文件:

    <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::resource('bank', 'bankController');
Route::get('admin/bank/municipalview/', 'bankController@municipalView');
Route::get('admin/bank/bankview/', 'bankController@bankView');

控制器文件:

    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) ;
}  

a的HREF中的URL照原样调用,并且在加载时未获取ID而是解析了URL

  

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

要删除按钮,我想通过

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

这可行吗

请协助我。 如果我的方法是正确的,请提出建议,因为我是开发的新手。

2 个答案:

答案 0 :(得分:1)

由于您试图从Javascript代码打印操作路线,因此它试图直接打印php echo代码,而不是功能的结果。

您应该执行以下操作:

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

答案 1 :(得分:0)

第二个问题的答案:表格集体转换为普通表格-添加删除按钮。

要通过常规链接而不是通过AJAX或其他类型的表单请求来执行此操作,我们可以设置一条特殊的路由来响应普通的GET请求:

在路线中,除了资源外,还定义以下内容:

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

Form集合可以重写为

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

注意: 供将来参考!