Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Maison012's avatar

How to pass request from two different blades?

I have a dashboard where i get some statistics but i want this statistics to be clickable. And when user click on eny of this statistics go to another page where is full table with record. But to show just filter as statistics shows. For example on dashboard i get only employes who is active for moment

   <div class="col-xl-3 col-lg-6" data-aos="fade-left" data-aos-duration="1000">
            <div class="card card-stats mb-4 mb-xl-0">
                <div class="card-body">
                    <div class="row">
                        <h5 align="center" class="text-uppercase text-muted-mb-0"> <u> Active Record  &#x275B; <span class="text-dark" > {{ $activerecord ?? '' }} </span> &#x275C;  </u> </h5>
                    </div>
                </div>
            </div>
        </div>
$activerecord = Employes::where( 'status', '=', 'active' ) ->count();

What i want:: Where i click on this statistics go to another page wich have route 'fulltable' and on datatable to set flter to see only active employer

0 likes
34 replies
tykus's avatar

You are talking about an anchor <a> element; you can set the href attribute to your route; and in that route helper, you can add query params, e.g.

@if ($activerecord)
	<a href="{{ route('fulltable', ['status' => 'active']) }}">{{ $activerecord }}</a>
@endif

In the Controller action, you can grab that query param from the Request:

public function index()
{
	$employees = Employes::when(
        request()->has('status'),
        fn ($builder) => $builder->where('status', '=', request()->get('status'))
    )->get();

   return view('your-view-with-datatable' [
        'status' => request()->get('status'), 
        'employees' => $employees
    ]);
}

I don't know which datatable package you're using but you should be able to set the filter property from the data passed to the view

Maison012's avatar

@tykus i get syntax error here

fn ($builder) => $builder->where('status', '=', request()->get('status'))
tykus's avatar

@usertxr what PHP version are you using; does it support short closures?

function ($builder) {
    $builder->where('status', '=', request()->get('status'))
});
tykus's avatar

@usertxr use the long Closure syntax I posted above in that case.

PHP7.2 is end of life and Laravel 6 will not be security fixed after September (???) so you should consider upgrading!

Maison012's avatar

@tykus Thanks for that. But no i get an error on view

Call to undefined function App\Http\Controllers\Admin\has()
tykus's avatar

@usertxr typo…

Should be request()->has(…) in the Eloquent query

Maison012's avatar

@tykus Okay now i get route with request on URL. But still on datatable i can see all data. Should i do any ajax request for datatable or something else?

tykus's avatar

@usertxr I don't know because...

I don't know which datatable package you're using but you should be able to set the filter property from the data passed to the view

Maison012's avatar

@tykus I use this cdn script

<script src="//cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>

also i use this query to make range filter

if(request()->ajax()){
            if(!empty($request->from_date)){
                $data= T_Name::whereDate('created_at', '>=', $request->from_date) ->whereDate('created_at', '<=', $request->to_date) ->get();
            }
            else{
...
}
tykus's avatar

@usertxr so do you only fetch the datatable's data using AJAX; you cannot pass data into it from the Blade template?

Maison012's avatar

@tykus and what should i do to fetch data from another blade to this datatables request?

tykus's avatar

@usertxr that's what I am trying to answer!

do you only fetch the datatable's data using AJAX

Maison012's avatar

@tykus No i can pass data. Inside same blade when i have datatables i have done a range data seqrch using ajax request.

tykus's avatar

@usertxr that's not what I mean; what I meant was can you pass to the datatable the rows you want to display; or do you have to fetch them using AJAX request?

Maison012's avatar

@tykus If you mean row what i fetch from db than Yes . When i fetch data fom db i use ajax just to tell route when to send data:

ajax: {
        url: '{{ route('data.filter') }}',
        data: function (d) {
            d.from_date = $('input[name=from_date]').val(); // this are request to make range data search
            d.to_date = $('input[name=to_date]').val();
        },
    },

 columns: [
        { data: 'first_name', orderable: false, },
]
tykus's avatar
tykus
Best Answer
Level 104

@usertxr so everything before about where I should you how to get the data in the controller is irrelevant; just make the link (anchor tag) like I showed, and on the view get the status from the query string:

ajax: {
  url: '{{ route('data.filter') }}',
  data: function (d) {
    d.status =  (new URLSearchParams(window.location.search)).get('status')
    d.from_date = $('input[name=from_date]').val();
    d.to_date = $('input[name=to_date]').val();
  },
},

Now, there will be a status in the AJAX Request which you can use to filter your query (using when below removes the need for if/else:

if(request()->ajax()){
    $data = T_Name::when($request->from_date, function ($builder, $fromDate){
        $builder->whereDate('created_at', '>=', $request->from_date);
    })->when($request->to_date, function ($builder, $toDate) {
        $builder->whereDate('created_at', '<=', $request->to_date);
    })->when($request->get('status'), function ($builder, $status) {
        $builder->where('status', $status);
    })->get();
}
Maison012's avatar

@tykus This is good. But what if i want to add also status active for today?

like this

<a href="{{ route('data', ['status' => 'active',  'created_at' => DB::raw('CURDATE()') ]) }}" 

and just another small problem if i click directly to link to see all table, it wait a bit before to display data , i have more than 2500 records here

tykus's avatar

@usertxr why would you have a raw query expression in a link; use Carbon???

<a href="{{ route('data', ['status' => 'active',  'created_at' => today()->format('Y-m-d') ]) }}" ?

it wait a bit before to display data Because (i) you need to wait for the AJAX request/response and (ii) you're fetching too much data - who wants 2500 records at once - consider paginating the query

Maison012's avatar

@tykus I use paginator. But also i use processing serverside.

Problem with this

<a href="{{ route('data', ['status' => 'active',  'created_at' => today()->format('Y-m-d') ]) }}" ?

Is shows me no record but in route i get today date and status active

tykus's avatar

@usertxr how are you passing the created_at from the query parameters to the filters for the AJAX request?

Maison012's avatar

@tykus same as status

d.created_at =  (new URLSearchParams(window.location.search)).get('created_at');
->when($request->get('created_at'), function ($builder, $created_at) {
                $builder->where('created_at', $created_at);
            })

Also i get an error here when i try to make range data search

if(request()->ajax()){
    $data = T_Name::when($request->from_date, function ($builder, $fromDate){
        $builder->whereDate('created_at', '>=', $request->from_date); // {message: "Undefined variable: request", exception: "ErrorException",…}

    })->when($request->to_date, function ($builder, $toDate) {
        $builder->whereDate('created_at', '<=', $request->to_date);
    })->when($request->get('status'), function ($builder, $status) {
        $builder->where('status', $status);
    })->get();
} 
tykus's avatar

@usertxr created_at column contains timestamps, e.g. 2022-01-20 15:14:23, your query is evaluating equality with 2022-01-20:

->when($request->get('created_at'), function ($builder, $created_at) {
    $builder->whereDate('created_at', '>=', $created_at);
})
Maison012's avatar

@tykus Yes you are right. Thanks for this. But can you check for range search i updated above

tykus's avatar

@usertxr you can solve this

Undefined variable: request", exception: "ErrorException"

by using the $fromDate variable instead - it was my typo above

when($request->from_date, function ($builder, $fromDate) {
    $builder->whereDate('created_at', '>=', $fromDate);
})

But can you check for range search

What do you mean; if there are from_date and to_date in the Request, then these filters are applied. YOu could condense this to a single when:

if(request()->ajax()){
    $data = T_Name::when($request->from_date && $request->to_date, function ($builder) use ($request) {
        $builder->whereDate('created_at', '>=', $request->from_date)
            ->whereDate('created_at', '<=', $request->to_date);
    })->when($request->get('status'), function ($builder, $status) {
        $builder->where('status', $status);
    })->get();
} 
Maison012's avatar

@tykus What do you mean; if there are from_date and to_date in the Request, then these filters are applied. YOu could condense this to a single when:

This was a new good trick. Thank you

tykus's avatar

@usertxr okay, if you're all set then please mark the thread closed

Maison012's avatar

@tykus If i can ask another questions to extent some statistics

href="{{ route('data', ['status' => 'active', 'created_at' => today()->format('Y-m-d') ]) }}"

How about current month start on date 1 of this month until current date. I mean like this

Carbon::now()->startOfMonth()->toDateString();
Carbon::now()->endOfMonth()->toDateString();

also this

Carbon::now()->subMonth()->startOfMonth()->toDateString();
Carbon::now()->subMonth()->endOfMonth()->toDateString();

how can i write this on href=".. today() .."

Please or to participate in this conversation.