Sometimes we need to access protected property in Laravel. Like laravel pagination has some protected properties. Suppose you want to access total properties then you will face an error like cannot access protected property illuminate\pagination\lengthawarepaginator::$total. So I will show you how to access laravel pagination protected property or you may say laravel access protected property.

So let's see the example of cannot access protected property laravel and how we can solve it:

app/Http/Controllers/TutorialController.php

<?php

namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Contracts\Database\Eloquent\Builder;

class TutorialController extends Controller
{   
    public function index(Request $request)
    {
    	$data = User::orderBy('created_at','desc')->paginate(5);

        $data = json_encode($data);
        $data = json_decode($data);
        // now you can access all the protected property like....
        dd($data->total);
    }
}

 

Read also: Laravel Pagination Links With OnEachSide Example

 

Conclusion

Now we know how to solve cannot access protected property illuminate pagination lengthawarepaginator total. Hope this cannot access protected property laravel tutorial will help you to get laravel access protected property.