1
votes

I am trying to access a route in Laravel web route from Vue, but it return Error:

Request failed with status code 500 response "message": "Class 'App\Http\Controllers\Admin\Customer' not found",

This is my controller routes in web.php

 Route::group(['prefix'  =>  'admin'], function () {
      Route::group(['prefix'  =>   'customers'], function() {

            Route::get('/', 'Admin\CustomerController@index');
            Route::post('/store', 'Admin\CustomerController@store');
            Route::post('/{id}/edit', 'Admin\CustomerController@update');
            Route::get('/{id}', 'Admin\CustomerController@delete');
            Route::get('/api/search', 'Admin\CustomerController@search');

        });
  });

This is my search query in my customercontroller class

public function search()
{
    $results = Customer::orderBy('firstname')
        ->when(request('q'), function($query) {
            $query->where('firstname', 'like', '%'.request('q').'%')
                ->orWhere('lastname', 'like', '%'.request('q').'%')
                ->orWhere('shop_name', 'like', '%'.request('q').'%')
                ->orWhere('shop_address', 'like', '%'.request('q').'%')
                ;
        })
        ->limit(6)
        ->get();

    return response()
        ->json(['results' => $results]);
}

vue template where i tried to access customerUrl

  data(){
     return{
        form: {},
        errors: {},
        customerUrl:'/admin/customers/api/search',
        productUrl:'/admin/products/api/search'
      }
   },

enter image description here

2
Add use App\Customer at top of your controller classporloscerros Ψ

2 Answers

1
votes

You have to import App\Customer on your CustomerController:

use App\Customer

class CustomerController extends Controller
{
    //...

or just declare the namespace when using Customer:

public function search()
{
    $results = \App\Customer::orderBy('firstname')
    //...
1
votes

You can also try like this.

use App\Customer;

public function search()
{

$results = Customer::orderBy('firstname')
//...