samedi 26 septembre 2020

Laravel - Avoid duplicate controller code when using @include directive from within another view

I have a controller function that renders a view called Dashboard and other function with duplicate code for updating sub view.

public function dashboard(Request $request) {
        
    // GET ALL THE VARIABLES AND PASS THEM TO THE VIEW
   
    return view('pages.dashboard.dashboard', compact('variables'));
}
public function dashboardIndicators(Request $request) {
        
    // GET ALL THE VARIABLES AND PASS THEM TO THE SUB VIEW
    return view('pages.dashboard.dashboardindicators', compact('variables'));
    }

Dashboard view includes the subview for displaying some data of the entire page:

<div id="dashboardindicators">
        @include('pages/dashboard/dashboardindicators')
</div>

I update the subview data with AJAX when user changes a dropdown and is working correct:

<script>
    $(document).ready(function () {
    
    $('.dropdown_get').on("change", function(){

        KTApp.block("#dashboardindicators", {
                overlayColor: "#000000",
                type: "v2",
                state: "success",
                message: "Procesando..."
            }),
        
        $.ajax({
        url: "/dashboard/indicators",
        method: 'GET',
        data: {
            market_center_id: $("#kt_market_center_dropdown option:selected").val(),
            mega_agent_id: $("#kt_mega_agent_dropdown option:selected").val(),
            team_id: $("#kt_team_dropdown option:selected").val(),
        },
        success: function (data) {
            $("#dashboardindicators").html(data)
           setTimeout(function () {
                KTApp.unblock("#kt_blockui_1_content")
            })
        }
        });
    
    });
});
</script>

My problem is that the controller function that renders Dashboard view has the same code as the function that returns the data for subview when AJAX is called although the only difference is the view that both are ruturning.

Is there any way I can avoid duplicating code and render Dashboard view correctly and don't break subview AJAX data update?

Regards



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/36aUi8e
via IFTTT

Aucun commentaire:

Enregistrer un commentaire