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

ToxifiedM's avatar

Unable to put session in Laravel Vue using Axios

I am trying to create an endpoint and send the data to then endpoint and requesting it later within the controller method. As the input value changes, it fires axios put method and pass the updated perPage data to an endpoint, I am getting a successful response, with status 200 in the console log, but the value isn't being updated or created in the sessions. I just don't know what am I missing here. Please throw some light here. Thanks!

Web

Route::put('/set-per-page', [UserController::class, 'setPerPage'])->name('set.per_page');

Controller

public function setPerPage(Request $request)
{
    $request->session()->put('perPage', $request->perPage);
    return session('perPage');
}

Component

<template>
    <input-group inline>
        <input-select v-model="perPage" placeholder="Per Page">
            <option value="5">5</option>
            <option value="10">10</option>
        </input-select>
    </input-group>
</template>

<script>
    export default {
        props: {
            perPageData: {
                type: Number
            }
        },

        data() {
            return {
                perPage: this.perPageData
            }
        },

        watch: {
            perPage: {
                handler: function() {
                    axios.put(route('set.per_page', { perPage: this.perPage })).then((response)=>{ console.log(response) })
                }
            }
        }
    }
</script>
0 likes
18 replies
Wakanda's avatar

@toxifiedm controller

public function setPerPage(Request $request)
{
    session()->put('perPage', $request->perPage);
    return session('perPage');
}
ToxifiedM's avatar

I think it doesn't really matters using $request->session() or just session(), because its still operating the same way.

Wakanda's avatar

@toxifiedm

are you receiving anything from the request?

public function setPerPage(Request $request)
{
dd($request->all());
    session()->put('perPage', $request->perPage);
    return session('perPage');
}
ToxifiedM's avatar

When I am doing dd, there is no response on the screen and also the response in the console log also contains no data.

enter image description here

But when I omit dd, I get the data in the response in the console log

enter image description here

Wakanda's avatar

@toxifiedm to see the dd data visit the network tab on your chrome dev tools and pay attention to the headers and preview

ToxifiedM's avatar

Okay, so when I set the perPage to 10, this is what I get in the preview.

array:1 [
  "perPage" => "10"
]

What can be the issue, why the session key with value isn't being updated or created? Please help me here, need to learn and rectify this part, as many things in my application depends on this :(

Wakanda's avatar

@toxifiedm now try this to see if you are persisting the value to the session

public function setPerPage(Request $request)
{

   session()->put('perPage', $request->perPage);

	dd(session('perPage'));
	
    return session('perPage');
}

if the session is returning a value

try to refactor to

public function setPerPage(Request $request)
{

   $perPage = session()->put('perPage', $request->perPage);
	
    return $perPage;
}
ToxifiedM's avatar

Thanks for your revert! Okay, so when I dd(session('perPage')); I am getting the value 10 as when I update it to 10 from the frontend. But when I reload the page it should pick the new perPage value as a session for it exists. And in my controller I have defined perPage as a public property and within the __construct() method I am defining $this->perPage = session()->get('perPage', $this->perPage); and passing the perPage as a data prop to the frontend, but still the perPage value reverts back to 5 which is the default value. Here is my complete controller for reference. Any idea why such kind of behavior is happening?

enter image description here

Controller

{
    public $perPage = 5;

    public function __construct()
    {
        $this->perPage = session()->get('perPage', $this->perPage);
    }

    public function index(Request $request)
    {
        $filters = $request->all('search');

        $rowsQuery = User::query()
                    ->select('id', 'name', 'email', 'role_id', 'created_at')
                    ->withRole()
                    ->filter($request->only('search'))
                    ->paginate($this->perPage)
                    ->withQueryString();

        return Inertia::render('Backend/Management/AudienceManagement/Users/Index', [
            'filtersData' => $filters,
            'usersData' => $rowsQuery,
            'perPageData' => $this->perPage
        ]);
    }

    public function setPerPage(Request $request)
    {
        session()->put('perPage', $request->perPage);

        dd(session('perPage'));
        
        return session('perPage');
    }
}
Wakanda's avatar

@toxifiedm try

    public $perPage = 5;

    public function __construct()
    {
        $this->perPage = session()->get('perPage', $this->perPage);
    }

    public function index(Request $request)
    {
        $filters = $request->all('search');

        $rowsQuery = User::query()
                    ->select('id', 'name', 'email', 'role_id', 'created_at')
                    ->withRole()
                    ->filter($request->only('search'))
                    ->paginate($this->perPage)
                    ->withQueryString();

        return Inertia::render('Backend/Management/AudienceManagement/Users/Index', [
            'filtersData' => $filters,
            'usersData' => $rowsQuery,
            'perPageData' => $this->perPage
        ]);
    }

    public function setPerPage(Request $request)
    {
        $perPage = session()->put('perPage', $request->perPage);

        $this->perPage = $perPage
        
        return session('perPage');
    }
}

When you refresh you would expect $perPage to reset to 5 since every time when that class constructs you are setting the value to 5 however via ajax it should maintain the state

ToxifiedM's avatar

Mate I really appreciate your efforts, thanks a ton for that! But still its the same, what I am seeking for is as the perPage value changes, the data should be paginated to the value selected as well as the perPage select input should show the value which the user selected, but even after session being created, the construct method is unable to take the value form the key perPage so by default 5 value is being assigned. Where the error lies, just not able to point.

ToxifiedM's avatar

@wakanda Is this the correct way of implementation for the above objective? What can be done to make it work, as I see there are no mistakes so far!

Wakanda's avatar

@toxifiedm try

public function setPerPage(Request $request)
    {
        $perPage = session()->put('perPage', $request->perPage);

        $this->perPage = $perPage
        
        $this->index(); // you have to pass params/argument here
    }

if that doesn't work you may want to point all your requests (Axios) to the index method and do this

public function index(Request $request)
{
    $filters = $request->all('search');

    if ($request->perPage) {
        $perPage = session()->put('perPage', $request->perPage);

        $this->perPage = $perPage;
    }

    $rowsQuery = User::query()
                ->select('id', 'name', 'email', 'role_id', 'created_at')
                ->withRole()
                ->filter($request->only('search'))
                ->paginate($this->perPage)
                ->withQueryString();

    return Inertia::render('Backend/Management/AudienceManagement/Users/Index', [
        'filtersData' => $filters,
        'usersData' => $rowsQuery,
        'perPageData' => $this->perPage
    ]);
}
ToxifiedM's avatar

With $this->index() getting an exception

"ArgumentCountError"

and getting the following message within the network tab.

"App\Http\Controllers\Backend\Management\AudienceManagement\UserController::index(), 0 passed in C:\xampp\htdocs\InertiaVue\app\Http\Controllers\Backend\Management\AudienceManagement\UserController.php on line 45 and exactly 1 expected"

ToxifiedM's avatar

By mistakenly I clicked on the best answer, what argument do I need to pass within $this->index()? like how should I format it?

ToxifiedM's avatar

I tried your method, Its doing nothing, and when I remove the default value and just construct it with $this->perPage = session()->get('perPage'), the pagination is destroyed as there is no perPage value in the session, but then I update it through the setPerPage() public method, as the construct method will be receiving the key value of perPage, still the pagination is destroyed, that means setPerPage method isn't actually setting the session, other wise it would have been accessed by the construct method. I am still thinking why this kind of behavior is happening?

ToxifiedM's avatar
ToxifiedM
OP
Best Answer
Level 1

@wakanda The actual problem was, we cant use sessions() within public function __construct() so it was unable to get the value from the session. As per the current setup, with a minor work around the problem was solved.

Using an inline middleware, we can hook into the request's routing, so that we have full access to the session.

public function __construct()
{
    $this->middleware(function ($request, $next) {

        $this->perPage = session()->get('perPage', $this->perPage);

        return $next($request);
    });
}

Please or to participate in this conversation.