2

I am a laravel beginner. I currently have a project in progressing which need to have two data tables in one page. In my past project, I only know how to make just a datatable to display ine one view page. For now, I need to have Branch Manager Table and Bus Driver Table to be displayed in one view page. For now I only know to display only one data table.

This is the view page

                            <table id="example1" class="table table-striped first" style="width:100%">
                                <thead>
                                    <tr>
                                        <th>Branch Manager Name</th>
                                        <th>Contact Number</th>
                                        <th class="col-md-2">Action</th>
                                    </tr>
                                </thead>
                                <tbody>
                                </tbody>
                            </table>

                            <table id="example2" class="table table-striped first" style="width:100%">
                                <thead>
                                    <tr>
                                        <th>Bus Driver Name</th>
                                        <th>Contact Number</th>
                                        <th class="col-md-2">Action</th>
                                    </tr>
                                </thead>
                                <tbody>
                                </tbody>
                            </table>

This is the ajax for Branch Manager

    <script type="text/javascript">
        $(function() {
            $.ajaxSetup({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                }
            });

            var table = $('#example1').DataTable({
                processing: true,
                serverSide: true,
                ajax: "{{ route('branchManager.list') }}",
                columns: [{
                        data: 'branch_manager_name',
                        name: 'branch_manager_name'
                    },
                    {
                        data: 'contact_number',
                        name: 'contact_number'
                    },
                    {
                        data: 'action',
                        name: 'action',
                        orderable: true,
                        searchable: true
                    },
                ]
            });



        });
    </script>

This is controller for Branch Manager

    public function branchManager(Request $request)
    {
        $branchManagers = BranchManager::latest()->get();

        if ($request->ajax()) {
            $data = BranchManager::latest()->get();
            return Datatables::of($data)
            ->addIndexColumn()
            ->addColumn('action', function($row){
                $btn = '<a href="javascript:void(0)" data-toggle="tooltip"  data-id="'.$row->id.'" data-original-title="Edit" class="btn btn-sm btn-outline-light editRecord">Edit</a>';

                $btn = $btn.' <a href="javascript:void(0)" data-toggle="tooltip"  data-id="'.$row->id.'" data-original-title="Delete" class="btn btn-sm btn-outline-light deleteRecord"><i class="far fa-trash-alt btn-outline-danger"></i></a>';

                return $btn;
            })
            ->rawColumns(['action'])
            ->make(true);
        }

        return view('admin.employee', compact('branchManagers'));
    }

This is the route

Route::get('/employee', [BranchManagerController::class, 'branchManager'])->name('branchManager.list');

Can anyone explain to me how to make it. Like how many controllers do I need? Isit I need to create one more controller for Bus Driver ? Or two tables are just used in one controller? Besides, if it is needed to create one more controller, how the route would looks like? Thanks

Say Jack
  • 61
  • 1
  • 6

1 Answers1

0

I think there is no need for you to make any other controller or something like that just make a new function where you call your data through Ajax method and give that a URL in "web.php" and in blade, file define that URL in ajax:{url:"{your url}"}

ajax: {
        url: 'your path name',
        data: function (d) {
            d.search = $('input[type="search"]').val()
        }
    },

and in other table:-

ajax: {
        url: 'your another path name',
        data: function (d) {
            d.search = $('input[type="search"]').val()
        }
    },
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 05 '23 at 08:48