【问题标题】:Modal with controller in LaravelLaravel 中带控制器的模态
【发布时间】:2017-02-27 16:44:32
【问题描述】:

嘿,朋友们,我正在创建一个简单的模式来向我展示提供商的数据,老实说,这让我付出了很多;有人可以帮帮我吗?

模态:

<div class="modal fade" id="myModal" tabindex="-1" role="dialog"
     aria-labelledby="myModal">
    <div class="modal-dialog"
         role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                            aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="myModal">Detalle del Proveedor: </h4>
            </div>
            <div class="modal-body">

                <div class="table-responsive">
                    <table class="table table-stripped table-bordered table-hover" id="table-detalle-proveedores">
                        <thead>
                        <tr>
                            <th>Nombre</th>
                            <th>Apellido</th>
                            <th>Telefono</th>
                            <th>Email</th>
                            <th>Dirección</th>
                        </tr>
                        </thead>
                        <tbody>

                        </tbody>
                    </table>
                </div>

            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Cerrar</button>
            </div>
        </div>
    </div>
</div>

模态按钮

<a href="" class="btn btn-primary btn-detalle-proveedores" data-id="{{ $proveedores->id }}"
   data-path="{{ route('admin.proveedores.item') }}" data-toggle="modal"
   data-target="#myModal"
   data-token="{{ csrf_token() }}">
    <i class="fa fa-external-link"></i>
</a>

路线

Route::post('admin/proveedores/item', [
    'as'   => 'admin.proveedores.item',
    'uses' => 'ProveedoresController@Item']);

控制器功能

public function item(Request $request)
{
    $items = Proveedores::select($request->id);

    return json_encode($items);
}

我正在测试那个和其他的,但我得到它的最大值是在控制台中向我显示一个空对象

【问题讨论】:

    标签: html ajax laravel modal-dialog


    【解决方案1】:

    首先,在您的 javascript 中,您将 id 作为 proveedores_id 传递,但在您的控制器中,您尝试使用 $request-&gt;id 访问它。

    看看https://laracasts.com/series/laravel-from-scratch-2017/episodes/8可能是个好主意

    其次,仅使用 select 您将返回 Builder 的 json 编码版本。

    要让您的请求实际返回Proveedores 的实例,您可以执行以下操作:

    public function item(Request $request)
    {
        $item = Proveedores::findOrFail($request->id);
    
        return compact('item');
    }
    

    这也意味着您可以删除成功方法中的 for 循环,并简单地使用 response.item.* 访问数据,例如

    function (response) {
    
        console.log(response)
    
        table.html('')
    
        var fila = "<tr>" +
                "<td>" + response.item.name + "</td>" +
                "<td>" + response.item.last_name + "</td>" +
                "<td>" + response.item.tel + "</td>" +
                "<td>" + response.item.address + "</td>" +
                "</tr>";
    
        table.append(fila);
    
    }
    

    希望这会有所帮助!

    【讨论】:

    • find 和 findorfail 有什么区别?
    • @basosneo find 将返回 null 如果它不存在。 findOrFail() 会抛出一个异常,最终会变成 404 响应。 laravel.com/docs/5.3/eloquent#retrieving-single-models
    • 因为我让它和'find'一起工作,因为'findorfail'不起作用
    • @basosneo 这很奇怪,因为findOrFail 只是在幕后使用find。你遇到了什么错误?
    • 我没有收到任何东西(既没有错误也没有数据),但是如果我收到了我的供应商的数据,我会发现
    猜你喜欢
    • 1970-01-01
    • 2021-09-06
    • 2012-09-17
    • 2014-10-23
    • 2011-03-29
    • 2013-04-27
    • 1970-01-01
    • 2017-10-21
    • 2014-11-23
    相关资源
    最近更新 更多