Laravel 5.6,如何从一个函数返回两个视图

时间:2018-07-09 11:35:56

标签: javascript php jquery laravel laravel-routing

我正在将Laravel 5.6与Ajax一起用于CRUD操作。 我的问题有点难以解释,但我会尽量生动。我有一个带分配功能的AssetController。该函数有一个return语句,它是: 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),它们将用一个函数(赋值函数)替换 Assigned Unassigned 标签中的表格,原因是我在视图中使用Tabs,因此当我分配未分配资产时,资产应在重新加载页面时从UNASSIGNED TAB移至ASSIGNED TAB。 如何做到这一点,从一个函数返回两个视图?

AssetController.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'));

        }

        }

T 替换表的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 的外观 Index.php

正在返回的HTML文件(unssigned_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>

Web.php

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

预先感谢您的任何建议。

1 个答案:

答案 0 :(得分:1)

您可以将视图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()
        ]);

在Java语言中:

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