After successful getting the result from filter how to show in blade file in Laravel 8

Viewed 352

I am working on the filter part in search page but the issue is In the main search page all the result looped from the controller, and Now I am using jquery for the filter process, but things are confusing. How to do this in a right way?

So here is the process :

Step 1 : user search something Like "support" and then system go to the searchController file and give the result

return view('/search')->with(["documents" => $results, "filters" => $filters]);

Here $filter indicates the category filters; Like this enter image description here

Then When User click on any category then it will filter the result but now the issue is I am using jquery and now things getting weird. Can somebody help me on this.

JS Code :

$(document).on("click", ".category_filter1",function() {
    var test = new Array();
    $("input[name='category_filter']:checked").each(function() {
        test.push($(this).val());
    });
    showValue(test);
});

function showValue(data){
    $.ajax({
        'url': 'search/filter/'+(data.length > 0 ? data : "all"),
        'type': 'GET',
        success: function(response){ // What to do if we succeed
            if(response.data == "success")
                document.getElementById('result').innerHTML = response.categories;
        },
        error: function(response){
            // alert('Error');
        }
    });
}

and The Controller from where all result came from :

public function filter($data){
        $t_data = explode(',' , $data);
        $filters = $this->load_filters();
        if(count($t_data)>0 && $t_data[0]!== "all"){
            $results = DB::table('documents')
                        ->whereIn('category', $t_data)
                        ->paginate(5);
        }else{
            $results = Document::paginate(5);
        }
        return redirect('/search')->with(["documents" => $results, "filters" => $filters]); // This part is really confusing
    }

So the flow will be like this :

enter image description here

1 Answers

Summarizing from as discussed in comments,

  • In order to call the controller without refreshing the page you need to use ajax, (use jquery or any other frontend framework). Return your response as json,
return response()->json($your_return_array);
  • For images store it in public folder, such as public/images/your-image.png. Then call it using url() method in your blade.
<img src="{{url('/images/your-image.png')}}" alt="Image"/>
Related