首页 > 解决方案 > Laravel 5.6,如何从一个函数返回两个视图

问题描述

我将 Laravel 5.6 与 Ajax 一起用于 CRUD 操作。我的问题有点难以解释,但我会尽量生动。我有一个带分配功能的 AssetController。这个函数有一个返回语句,它 用数据库中的return view('/assets/unassigned_asset_ajax', compact('asset_categories', 'asset_received','assets', 'manufacturers', 'departments', 'staffs', 'unassigned_assets', 'assigned_assets'));当前数据替换未分配选项卡中的表(这是为了避免在提交时重新加载并且工作得很好)但是我想返回两个不同的 HTML 文件(unassigned_asset_ajax & assigned_asset_ajax)这将替换已分配未分配中的表选项卡,具有一个功能(分配功能),为什么因为我在视图中使用选项卡所以当我分配未分配的资产时,资产应该从未分配的选项卡移动到已分配的选项卡并重新加载页面。我如何完成这项工作,从一个函数返回两个视图?

资产控制器.php

public function assign(Request $request){
        $rules = array(
            'assignment_id' => '',
            'asset_id' => 'required',
            'staff_id' => 'required',
            'department_id' => 'required',
            'date' => 'required',
            'status' => 'required',
        );
        $validator = Validator::make($request->all(), $rules);
        if ($validator->fails())
            return response::json([
                'fail' => true,
                'errors' => $validator->errors()
            ]);

        else{
            $assignments =  new Assignment;
            $assignments  ->assignment_id = $request->assignment_id;
            $assignments  ->staff_id  = $request->staff_id;
            $assignments  ->asset_id = $request->asset_id;
            $assignments  ->department_id = $request->department_id;
            $assignments  ->date  = $request->date;
            $assignments  ->status = $request->status;

            $asset = Asset::where('asset_id', '=' ,$assignments->asset_id = $request->asset_id)->
            where('status', '=' ,'Unassigned')->first();
            $asset->status = 'Assigned';

            $asset->save();
            $assignments ->save();

            $asset_categories = Asset_category::all();
            $asset_received = Asset_received::all();
            $assets = Asset::all();
            $unassigned_assets = Asset::all();
            $assigned_assets = Assignment::select('asset_id')->groupBy('asset_id')->get();
            $manufacturers = Manufacturer::all();
            $departments = Department::all();
            $staffs = Staff::all();

                return view('/assets/unassigned_asset_ajax', compact('asset_categories', 'asset_received',
                    'assets', 'manufacturers', 'departments', 'staffs', 'unassigned_assets', 'assigned_assets'));

        }

        }

替换表格的JS文件。

  $(document).on('click', 'button.assign-asset', function() {
    $.ajax({
        type: 'post',
        url: '/assignUnassigned',
        data: {
            '_token': $('input[name=_token]').val(),
            'asset_id': $('input[name=un_asset_id]').val(),
            'staff_id': $("#staff_id option:selected").val(),
            'department_id': $("#department_id option:selected").val(),
            'date': $('input[name=unassigned_date]').val(),
            'status': $("#unassigned_status option:selected").val(),
        },
        success: function(data) {
            if((data.errors)){
                $('#edit-error').removeClass('hidden');
                $('#edit-error').text(data.errors.tag_id);
                $('#edit-error').text(data.errors.staff);
                $('#edit-error').text(data.errors.department);
                $('#edit-error').text(data.errors.date);
                $('#edit-error').text(data.errors.status);
            }else{
                $('#edit-error').remove();
                $('#unassigned').html(data);
            }
        }
    });
});

index.blade.php(查看)

<div class="tab-pane" id="assigned">
                        <div class="row">
                            <div class="col-lg-12">
                                <div class="card-box">
                                    <h4 class="m-t-0 header-title"><b>ASSIGNED ASSETS</b></h4>

                                    <table id="assigned-asset-table" class="table table-borderless" data-page-size="7">
                                        <thead>
                                        <tr>
                                            <th>Tag ID</th>
                                            <th>Category</th>
                                            <th>Colour</th>
                                            <th>Serial Number</th>
                                            <th>Model Number</th>
                                            <th>No Of Times Assigned</th>
                                        </tr>
                                        </thead>
                                        <tbody>
                                        @foreach($assigned_assets->all() as $assigned)

                                                <tr>
                                                    <td>{{ $assigned->asset['tag_id'] }}</td>
                                                    <td>{{ $assigned->asset->asset_categories['category'] }}</td>
                                                    <td>{{ $assigned->asset['colour'] }}</td>
                                                    <td class="serial">{{ $assigned->asset['serial_number'] }}</td>
                                                    <td>{{ $assigned->asset['model_number'] }}</td>
                                                    <td>{{ $assigned->count }}</td>
                                                    <div class="button-list">
                                                        <td>
                                                            <a href='{{ url("/update_assignment/{$assigned->assignment_id}") }}' class="label label-success">Update</a> |
                                                            <a href='' class="label label-success" data-toggle="modal" data-target="#custom-width-modal-{{ $assigned->asset['asset_id'] }}">View Assignments</a>
                                                            <a href='' class="label label-inverse" data-toggle="modal" data-target="#myModal-{{-- $user_support->asset['asset_id'] --}}">View Repairs</a>
                                                            <a href='' class="label label-success" data-toggle="modal" data-dismiss="modal"  data-target="#addsupport-modal-{{ $assigned->asset['asset_id'] }}">Add Support</a>
                                                            <a href='{{ url("/reassign/{$assigned->assignment_id}") }}' class="label label-success">Reassign</a> |
                                                            <a href=' {{ url("/delete_assignment/{$assigned->assignment_id}") }}' class="label label-danger">Delete</a>
                                                        </td>
                                                    </div>
                                                </tr>
                                        @endforeach

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

<div class="tab-pane" id="unassigned">
                        <div class="row">
                            <div class="col-lg-12">
                                <div class="card-box">
                                    <h4 class="m-t-0 header-title"><b>UNASSIGNED Asset Details</b></h4>

                                    <table id="unassigned-asset-table" class="table table-borderless" data-page-size="7">
                                        <thead>
                                        <tr>
                                            <th>Tag ID</th>
                                            <th>Category</th>
                                            <th>Manufacturer</th>
                                            <th>Serial Number</th>
                                            <th>Model Number</th>
                                            <th>Colour</th>
                                        </tr>
                                        </thead>
                                        <tbody>
                                        @foreach($unassigned_assets->where('status', '=' ,'Unassigned') as $unassigned)
                                            <tr class="unassigned_asset{{$unassigned->asset_id}}">
                                                <td>{{ $unassigned->tag_id}}</td>
                                                <td>{{ $unassigned->asset_categories['category']}}</td>
                                                <td>{{ $unassigned->manufacturers['manufacturer']}}</td>
                                                <td>{{ $unassigned->serial_number}}</td>
                                                <td>{{ $unassigned->model_number}}</td>
                                                <td>{{ $unassigned->colour}}</td>
                                                <td>
                                                    <a href='#' class="assign-unassigned label label-success" data-asset-id="{{$unassigned->asset_id}}"
                                                       data-tag-id="{{$unassigned->tag_id}}">
                                                        <span class="glyphicon glyphicon-edit"></span> Assign
                                                    </a>
                                                    |<button class=" btn btn-inverse" data-toggle="modal" data-target="#listrecorded{{--$asset_receive->asset_received_id--}}">
                                                        <span id="r-footer_action_Btn" class="glyphicon glyphicon-eye-open"></span> View
                                                    </button>
                                                </td>
                                            </tr>
                                        @endforeach
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>

这就是 index.php 的样子 索引.php

返回的 HTML 文件 (unassigned_asset_ajax)

<div class="row">
<div class="col-lg-12">
    <div class="card-box">
        <h4 class="m-t-0 header-title"><b>UNASSIGNED Asset Details</b></h4>

        <table id="unassigned-asset-table" class="table table-borderless" data-page-size="7">
            <thead>
            <tr>
                <th>Tag ID</th>
                <th>Category</th>
                <th>Manufacturer</th>
                <th>Serial Number</th>
                <th>Model Number</th>
                <th>Colour</th>
            </tr>
            </thead>
            <tbody>
            @foreach($unassigned_assets->where('status', '=' ,'Unassigned') as $unassigned)
                <tr class="unassigned_asset{{$unassigned->asset_id}}">
                    <td>{{ $unassigned->tag_id}}</td>
                    <td>{{ $unassigned->asset_categories['category']}}</td>
                    <td>{{ $unassigned->manufacturers['manufacturer']}}</td>
                    <td>{{ $unassigned->serial_number}}</td>
                    <td>{{ $unassigned->model_number}}</td>
                    <td>{{ $unassigned->colour}}</td>
                    <td>
                        <a href='#' class="assign-unassigned label label-success" data-asset-id="{{$unassigned->asset_id}}"
                           data-tag-id="{{$unassigned->tag_id}}">
                            <span class="glyphicon glyphicon-edit"></span> Assign
                        </a>
                        |<button class=" btn btn-inverse" data-toggle="modal" data-target="#listrecorded{{--$asset_receive->asset_received_id--}}">
                            <span id="r-footer_action_Btn" class="glyphicon glyphicon-eye-open"></span> View
                        </button>
                    </td>
                </tr>
            @endforeach
            </tbody>
        </table>
    </div>
</div>

要返回的另一个 HTML 文件 (assigned_asset_ajax)

<div class="row">
<div class="col-lg-12">
    <div class="card-box">
        <h4 class="m-t-0 header-title"><b>ASSIGNED ASSETS</b></h4>

        <table id="assigned-asset-table" class="table table-borderless" data-page-size="7">
            <thead>
            <tr>
                <th>Tag ID</th>
                <th>Category</th>
                <th>Colour</th>
                <th>Serial Number</th>
                <th>Model Number</th>
                <th>No Of Times Assigned</th>
            </tr>
            </thead>
            <tbody>
            @foreach($assigned_assets->all() as $assigned)
                {{--@if($assignment)--}}
                <tr>
                    <td>{{ $assigned->asset['tag_id'] }}</td>
                    <td>{{ $assigned->asset->asset_categories['category'] }}</td>
                    <td>{{ $assigned->asset['colour'] }}</td>
                    <td class="serial">{{ $assigned->asset['serial_number'] }}</td>
                    <td>{{ $assigned->asset['model_number'] }}</td>
                    <td>{{ $assigned->count }}</td>
                    <div class="button-list">
                        <td>
                            <a href='{{ url("/update_assignment/{$assigned->assignment_id}") }}' class="label label-success">Update</a> |
                            <a href='' class="label label-success" data-toggle="modal" data-target="#custom-width-modal-{{ $assigned->asset['asset_id'] }}">View Assignments</a>
                            <a href='' class="label label-inverse" data-toggle="modal" data-target="#myModal-{{-- $user_support->asset['asset_id'] --}}">View Repairs</a>
                            <a href='' class="label label-success" data-toggle="modal" data-dismiss="modal"  data-target="#addsupport-modal-{{ $assigned->asset['asset_id'] }}">Add Support</a>
                            <a href='{{ url("/reassign/{$assigned->assignment_id}") }}' class="label label-success">Reassign</a> |
                            <a href=' {{ url("/delete_assignment/{$assigned->assignment_id}") }}' class="label label-danger">Delete</a>
                        </td>
                    </div>
                </tr>
                {{--@endif--}}
            @endforeach
            {{--@foreach($unassigned_assets->where('status', '=' ,'Unassigned') as $unassigned)--}}
            {{--<tr class="unassigned_asset{{$unassigned->asset_id}}">--}}
            {{--<td>{{ $unassigned->tag_id}}</td>--}}
            {{--<td>{{ $unassigned->asset_categories['category']}}</td>--}}
            {{--<td>{{ $unassigned->manufacturers['manufacturer']}}</td>--}}
            {{--<td>{{ $unassigned->serial_number}}</td>--}}
            {{--<td>{{ $unassigned->model_number}}</td>--}}
            {{--<td>{{ $unassigned->colour}}</td>--}}
            {{--<td>--}}
            {{--<a href='#' class="assign-unassigned label label-success" data-asset-id="{{$unassigned->asset_id}}"--}}
            {{--data-tag-id="{{$unassigned->tag_id}}">--}}
            {{--<span class="glyphicon glyphicon-edit"></span> Assign--}}
            {{--</a>--}}
            {{--|<button class=" btn btn-inverse" data-toggle="modal" data-target="#listrecorded--}}{{--$asset_receive->asset_received_id--}}{{--">--}}
            {{--<span id="r-footer_action_Btn" class="glyphicon glyphicon-eye-open"></span> View--}}
            {{--</button>--}}
            {{--</td>--}}
            {{--</tr>--}}
            {{--@endforeach--}}
            </tbody>
        </table>
    </div>
</div>

网页.php

Route::post('/assignUnassigned', 'AssetController@assign');

提前感谢您的任何建议。

标签: javascriptphpjquerylaravellaravel-routing

解决方案


您可以将视图 html 存储到变量或数组中,并将它们作为 json 返回。然后在 javascript 中访问这些值并直接放入 div。阅读render()函数。

在控制器中:

return response::json([
            'view_1' => view('/assets/unassigned_asset_ajax', compact('asset_categories', 'asset_received', 'assets', 'manufacturers', 'departments', 'staffs', 'unassigned_assets', 'assigned_assets'))->render(),
            'view_2' => view('/assets/assigned_asset_ajax', compact('asset_categories', 'asset_received', 'assets', 'manufacturers', 'departments', 'staffs', 'unassigned_assets', 'assigned_assets'))->render()
        ]);

在 Javascript 中:

if ((data.errors)) {
        $('#edit-error').removeClass('hidden');
        $('#edit-error').text(data.errors.tag_id);
        $('#edit-error').text(data.errors.staff);
        $('#edit-error').text(data.errors.department);
        $('#edit-error').text(data.errors.date);
        $('#edit-error').text(data.errors.status);
    } else {
        $('#edit-error').remove();
        $('#unassigned').html(data.view_1);
        $('#assigned').html(data.view_2);
    }

推荐阅读