davy_yg's avatar
Level 27

Pagination

Hello,

I am trying to create pagination in laravel and receiving error.

ErrorException in 1e886a45102a3e4898f23b52cd7ca771 line 369: Call to undefined method stdClass::links() (View: C:\xampp\htdocs\soulfy_repo\framework\resources\views\soulfy\setting.blade.php)

This is my codes:

HomeController.php

  use Illuminate\Pagination\LengthAwarePaginator;
  use Illuminate\Pagination\Paginator;

  ....

public function getBackgroundTheme()
{

    $query = DB::table('theme_background')->paginate(4);

    if (request()->has('menu')) {
        $theme = DB::table('kategori_name')->where('kategori_theme', request('menu'))->first();

        $query = $query->where('kategori_id', $theme->kategori_id);
    }

    $model = $query->get();

    
    return view('soulfy.setting', [
        'themes'=>$model,
        'user' => auth()->user(),
    ]);
    
}

setting.blade.php

      @if(isset($themes))
            @foreach($themes as $m)

                <tr><img width="100px" height="100px" src="{{url('/')}}/uploads/theme/{{$m->pic_name}}.jpg"/></tr>
                                    

            @endforeach
@endif

{{ $m->links() }}

How to fix the above error and make the pagination works?

0 likes
9 replies
developre's avatar

Use should use $themes->links() rather than the $m->links()

davy_yg's avatar
Level 27

Still receiving error message:

ErrorException in 1e886a45102a3e4898f23b52cd7ca771 line 375: Call to undefined function links() (View: C:\xampp\htdocs\soulfy_repo\framework\resources\views\soulfy\setting.blade.php)

Snapey's avatar

You overwrote the query inside your if statement. Also, you don't use get() and paginate at the same time

public function getBackgroundTheme()
{

    $query = DB::table('theme_background');

    if (request()->has('menu')) {

        $theme = DB::table('kategori_name')->where('kategori_theme', request('menu'))->first();

        $query = $query->where('kategori_id', $theme->kategori_id);
    }

    $themes = $query->paginate(4);

    
    return view('soulfy.setting', [
        'themes'=>$themes,
        'user' => auth()->user(),
    ]);
    
}
2 likes
davy_yg's avatar
Level 27

Call to a member function links() on array (View: C:\xampp\htdocs\soulfy_repo\framework\resources\views\soulfy\setting.blade.php)

setting.blade.php

       <table>

                                    @if(isset($themes))
                                    @foreach($themes as $m)

                                    <tr>
                                    <td><img width="100px" height="100px" src="{{url('/')}}/uploads/theme/{{$m->pic_name}}.jpg"/></td>
                                    <td><img width="100px" height="100px" src="{{url('/')}}/uploads/theme/{{$m->pic_name}}.jpg"/></td>
                                    <td><img width="100px" height="100px" src="{{url('/')}}/uploads/theme/{{$m->pic_name}}.jpg"/></td>
                                    </tr>

                                    <tr>
                                    <td><div class="box"><input type="checkbox" name="pic" value=""></div></td>
                                    <td><input type="checkbox" name="pic" value=""></td>
                                    <td><input type="checkbox" name="pic" value=""></td>
                                    </tr>

                                    @endforeach
                                    @endif

                                    {{ $themes->links() }}

                                    </table>

HomeController.php

 public function getBackgroundTheme()
 {

    $query = DB::table('theme_background');

    if (request()->has('menu')) {
        $theme = DB::table('kategori_name')->where('kategori_theme', request('menu'))->first();

        $query = $query->where('kategori_id', $theme->kategori_id);
    }

    //$model = $query->get();
    $theme = $query->paginate(4);
    
    return view('soulfy.setting', [
        'themes'=>$theme,
        'user' => auth()->user(),
    ]);


    // return redirect('/home/setting' . 
  }
michimawan's avatar

adding @Snapey $m should be changed to $themes and put inside isset block

  @if(isset($themes))
            @foreach($themes as $m)

                <tr><img width="100px" height="100px" src="{{url('/')}}/uploads/theme/{{$m->pic_name}}.jpg"/></tr>
                                    

            @endforeach
    {{ $themes->links() }}
@endif


davy_yg's avatar
Level 27

ErrorException in 1e886a45102a3e4898f23b52cd7ca771 line 396: Call to a member function links() on array (View: C:\xampp\htdocs\soulfy_repo\framework\resources\views\soulfy\setting.blade.php)

line 396:

  <?php echo e($themes->links()); ?>

Why I cannot paginate?

Someone mentions that I should remove the ->first() calling method since only return a single element.

This is strange that I receive many pictures shown as a result. It's a picture gallery which is based under a single selected category.

Snapey's avatar

paginate needs to be on an eloquent model I think, not on a DB:: statement. Sorry. Should have realised earlier. Too focussed on the other issues

davy_yg's avatar
Level 27

I think I find the problem. My laravel version is 5.1.8

This works:

   <table>
                                        @foreach ($themes->chunk(3) as $chunk)
                                            <tr>
                                            @foreach ($chunk as $theme)
                                                <td>
                                                    <img width="100px"
                                                        height="100px"
                                                        src="{{url('/')}}/uploads/theme/{{$theme->pic_name}}.jpg"/> 
                                                    <input type="checkbox"/>
                                                </td>
                                            @endforeach
                                            </tr>
                                        @endforeach
                                    </table>

                                    

                                    {!! $themes->render() !!}

Now another problem, why the pagination comes vertical ?

Snapey's avatar

My laravel version is 5.1.8

Useful to know ¯\ಠ_ಠ

Please or to participate in this conversation.