3
votes

I have a project for multi vendor website with laravel 5.7 and vue.js. I used following code for search function.

My Web.php:

Route::post('product/search','Api\productController@search');

My Controller:

public function search(Request $request)
   {
            $searchTitle = $request->title;
            $searchCategory = $request->category;
            $searchUni = $request->uni;
            $searchCity = $request->city;
            if ($searchTitle || $searchCategory || $searchUni || $searchCity) {
                $ads = advertise::when($searchCategory, function ($query) use ($searchCategory) {
                    return $query->where('category_id', 'like', "%{$searchCategory}%");
                })
                    ->when($searchUni, function ($query) use ($searchUni) {
                        return $query->where('university', 'like', "%{$searchUni}%");
                    })
                    ->when($searchCity, function ($query) use ($searchCity) {
                        return $query->where('city', 'like', "%{$searchCity}%");
                    })
                    ->when($searchTitle, function ($query) use ($searchTitle) {
                        return $query->where('title', 'like', "%{$searchTitle}%");
                    })
                    ->where('status','=',1)->orderBy('created_at', 'desc')->paginate(18)
                    ->appends(request()->query());
            } else {
                $ads = advertise::where('status','=',1)->orderBy('created_at', 'desc')->paginate(18);

            }
            $i = 0;
            foreach($ads as $sd){
                $images = image::where('adver_id', $sd->id)->get();
    //            return response()->json($images);
                if(!$images->isEmpty()){
    //                return response()->json($images);
                    $ads[$i]->image1= $images[0]->image;
                }
                $i=$i+1;
            }
            return new productCollection($ads);
   }

Axios in my vue-route.js:

    axios.post("/api/product/search/",this.searchbox).then(response => {

                        if( response.data.data.length === 0 ) {
                            this.showTeezers = "nothing"
                            this.$store.state.teezers=[]

                        } else {
                            this.$store.state.teezers=response.data.data
                        }
                    }).catch(error => {
                        console.log(error)
                    })

When I run project in my localhost it's work well, but when I moved that into host (Linux server) it doesn't work. This error appears in the host:

"The GET method is not supported for this route. Supported methods: POST.",405 method not allowed

3
Have you tried clearing the cache in your host? Type in your putty "php artisan route:cache" if that throws an error chances are you have an error in your routesDante
@Dante Yes I do that. But nothing change.seyed mostafa farmanbar
try php artisan route:list both on server and localhost, and see the difference between two of them its kinda hard to only debug with 1 line code on route while it might be conflicted with other routes and on linux use case sensitive might be an issueGhiffari Assamar
@GhiffariAssamar I use c-panel host on linux server. Maybe I'm doing it wrong. How can I run php artisan route:list on server?seyed mostafa farmanbar

3 Answers

3
votes

try matching the callback with the uses method.

Route::post('product/search', ['uses' => 'Api\productController@search']);
2
votes

My mistake was using / after search in the following code: axios.post("/api/product/search/",this.searchbox).then(response => {..} I removed it and it was correct. Thanks to all.

1
votes

Ok, I'll list all I can think off:

Have you tried clearing the cache in your host? Type in your putty "php artisan route:cache" if that throws an error chances are you have an error in your routes

Do you see the route you want to use in "php artisan route:list" ? If you don't then the problem is that somehow laravel is not taking your route

Finally and I think this will sound bad, but try checking that the content in your server matches the one in your localhost.