Msoft's avatar
Level 1

Method links does not exist in Laravel 5.6 app?

working with laravel 5.6 and in My comtroller I have two tables like vehicles and uploads.relationship with both two tables are, Vehicle Model,

public function uploads()
    {
        return $this->hasMany(Upload::class);
    }

and Upload Model,

 public function vehicle()
    {
        return $this->belongsTo(Vehicle::class);
    }

and I have following index function in My VehicleController,

$vehicles = Vehicle::with('uploads')
            ->orderBy('adtype','DESC')

            ->latest('updated_at')
            ->paginate(5);

            return view('vehicles.index')->withVehicles($vehicles);

and index blade file is,

<form method="GET" action="{{ url('search') }}">
      {{csrf_field()}}
            <div class="row">
                <div class="col-md-6">
                    <input type="text" name="search" class="form-control" placeholder="Search">
                </div>
                <div class="col-md-6">
                    <button class="btn btn-info">Search</button>
                </div>
            </div>
        </form>

        <br> 

@forelse( $vehicles as $vehicule )


                    @if( $vehicule->uploads->count() > 0 )
                        <a href="{{ route('vehicles.show', $vehicule->id) }}">
                            @php
                                $upload = $vehicule->uploads->sortByDesc('id')->first();
                            @endphp

                            <div style="border-style: solid; background-color: {{ $vehicule->adtype === 1 ? '#FFEFD5' : '#FFFFFF' }} ">
                            <img src="/images/{{ $upload->resized_name }}" height="150" width="250"></a>


                        {{ Carbon\Carbon::parse($vehicule->created_at)->diffForHumans()}} 
                            {{$vehicule->provincename}}
                          {{$vehicule->milage}}

                          </div>
<br>
                           <hr>

                    @endif

            @empty
                <td>No Advertisment to display.</td>
            @endforelse

</div>

</div>
</div>
{{ $vehicles->links() }}
</div>
@endsection

my pagination is working fine, but in my index file I have search input using algolia. when I use keyword and click search button following error is occured,

(2/2) ErrorException

Method links does not exist. (View: C:\Users\banda\Desktop\ddddd\resources\views\vehicles\index.blade.php)

how can fix this problem?

0 likes
23 replies
Snapey's avatar

You haven't shown what happens when you post to url('search')

1 like
Msoft's avatar
Level 1

@Snapey when I clicked search button occured above error message.

my route for url('search ') is,

Route::get('search','SearchController@search');

and SearchController

if($request->has('search')){
            $vehicles = Vehicle::search($request->get('search'))->get()->sort(function ($a, $b) {
        if ($a->adtype !== $b->adtype) {
            return $b->adtype - $a->adtype;
        }
    
        return $a->adtype
            ? ($a->updated_at->gt($b->updated_at) ? -1 : 1)
            : ($a->created_at->gt($b->created_at) ? -1 : 1);
    });  
        }else{
            $vehicles = Vehicle::get();
        }
        return view('vehicles.index', compact('vehicles'));
    
andylord565's avatar

on SearchController change $vehicles = Vehicle::get(); to $vehicles = Vehicle::paginate(5);

also

$vehicles = Vehicle::search($request->get('search'))->paginate(5)->sort(function ($a, $b) {

Cronix's avatar

@msoft The problem is your SearchController code. You don't paginate() the $vehicles results there like you are in the VechicleController, but both controllers are sharing the same vehicles.index view that is expecting paginated results. So you need to send a paginated instance of $vehicles from both controllers to that view, otherwise links() won't exist in the non-paginated $vehicles instance.

1 like
Msoft's avatar
Level 1

@Cronix then how can do SearchController code as VehicleController index function??? I tried with many ways but got errors

Cronix's avatar

Hard to say offhand. Show your Vehicles::search() method. Your first query (VehicleController) is straightforward using standard DB query methods. Your other one is more complex and using a custom method.

Simplify your query. Get it working without sort() and stuff. Once you get the pagination part working, then work on refining it how you need it.

Msoft's avatar
Level 1

@Cronix SearchController@search function is,

public function search(Request $request)
    {
        if($request->has('search')){
            $vehicles = Vehicle::search($request->get('search'))->get()->sort(function ($a, $b) {
        if ($a->adtype !== $b->adtype) {
            return $b->adtype - $a->adtype;
        }
    
        return $a->adtype
            ? ($a->updated_at->gt($b->updated_at) ? -1 : 1)
            : ($a->created_at->gt($b->created_at) ? -1 : 1);
    });  
        }else{
            $vehicles = Vehicle::get();
        }
        return view('vehicles.index', compact('vehicles'));
    }
    
Cronix's avatar

You already showed that. I was talking about the Vehicle::search() method in the Vehicle model that you're using there

Vehicle::search($request->get('search'))...
Snapey's avatar

You cannot paginate your search because you are using sort on eloquent collection.

Best you could do with the code you have now is sort the results on each page, which would not be any use.

I thought you marked your previous post as resolved?

Msoft's avatar
Level 1

@Cronix

use Searchable;
     protected $guarded = [];

    public function searchableAs()
    {
        return 'categoryname';
    }
Msoft's avatar
Level 1

@Snapey yes I have tried with following code,

if($request->has('search')){
            $vehicles = Vehicle::search($request->get('search'))
                        ->orderBy('adtype','DESC')

            ->latest('updated_at')
            ->paginate(10);
    });  //line 34
        }else{
            $vehicles = Vehicle::paginate(10);
        }
        return view('vehicles.index', compact('vehicles'));
    }

it occured

1/1) FatalErrorException

syntax error, unexpected ')'
in SearchController.php line 34
Snapey's avatar

Line 34 should not exist

Indent your code carefully and this would be obvious.

Cronix's avatar

I haven't used Scout, but it has a pagination method. Just try

Vehicle::search($request->get('search'))->paginate(10);

and if that works, try doing the sorting in the query itself instead of using sort() on the result. Not exactly sure what you're trying to do with your ordering there... Why not just order by updated_at? If it wasn't ever updated, created_at and updated_at would be equal.

maybe try

Vehicle::search($request->get('search'))->latest('updated_at')->paginate(10);
Cronix's avatar

We don't see your code, so should we try guessing what you're doing wrong?

Msoft's avatar
Level 1

@Cronix in your answer this one is working,

Vehicle::search($request->get('search'))->paginate(10);

but search results did not display according to the updated_at values.

but when used

Vehicle::search($request->get('search'))->latest('updated_at')->paginate(10);

it is got

Method latest does not exist.
Cronix's avatar

Maybe put latest() before search(). I can't advise you on using Scout (which changes things) - I don't use it.

Msoft's avatar
Level 1

if it is work latest method my problem may solve

Cronix's avatar

if it is work latest method my problem may solve

Does that mean this is working now?

Msoft's avatar
Level 1

@Cronix working without latest method but without latest it is not satisfy my business logic here....

Georg's avatar

Just out of curiosity what happens you remove this line ->latest('updated_at')

Please or to participate in this conversation.