laravel中的show方法返回索引方法

时间:2019-03-27 04:25:39

标签: laravel vue.js vuejs2 axios

在laravel 5.7的资源控制器中尝试使用show方法时遇到问题。我正在使用VueJS和axios来处理HTTP请求。索引方法称为show方法。我通过get方法和 ID 参数进行调用。

路线:

POST      | products                                | products.store                    | App\Http\Controllers\ProductoController@store                             | web          |
|        | GET|HEAD  | products/create                         | products.create                   | App\Http\Controllers\ProductoController@create                            | web          |
|        | DELETE    | products/{product}                      | products.destroy                  | App\Http\Controllers\ProductoController@destroy                           | web          |
|        | PUT|PATCH | products/{product}                      | products.update                   | App\Http\Controllers\ProductoController@update                            | web          |
|        | GET|HEAD  | products/{product}                      | products.show                     | App\Http\Controllers\ProductoController@show                              | web          |
|        | GET|HEAD  | products/{product}/edit                 | products.edit                     | App\Http\Controllers\ProductoController@edit                              | web          |

web.php:


Route::get('/', function () {
    return view('welcome');
});

Auth::routes();

Route::get('/home', 'HomeController@index')->name('home');

//Productos
Route::resource('products', 'ProductoController');
Route::get('duplicar/{param?}', 'ProductoExtController@duplicar');
Route::post('mostrarProductos', 'ProductoExtController@mostrarProductos');
Route::post('guardarValoresEditados', 'ProductoExtController@guardarValoresEditados');
Route::get('imprimirListadoPrecios', 'ProductoExtController@imprimirListadoPrecios');
Route::post('mostrarProductosStickers', 'ProductoExtController@mostrarProductosStickers');
Route::post('imprimirStickers','ProductoExtController@imprimirStickers');



//Colecciones
Route::resource('colecciones', 'ColeccionController');

//Categorias
Route::resource('categorias', 'CategoriaController');

//Crostas
Route::resource('crostas', 'CrostaController');
Route::get('crostasaut/{param?}', 'CrostaExtController@autocomplete');

//Folias
Route::resource('folias', 'FoliaController');
Route::get('foliasaut/{param?}', 'FoliaExtController@autocomplete');

Route::get('{path}', 'HomeController@index')->where('path','([A-z\d-\/_.]+)?');

这是我对axios的http请求:

editarProducto(pDatosFila){
      axios.get('products', {
        params: {
          id: pDatosFila.f014_id
        }
      })
      .then(function (response) {
        console.log(response);
      })
      .catch(function (error) {
        console.log(error);
      })
      .then(function () {
        // always executed
      }); 
    }

最后,这是响应:

current_page: 1
data: [{f014_id: 25, f014_id_old: null, f014_nombre: "carlos ruales", f014_deleted: null,…},…]
first_page_url: "http://localhost:8000/products?page=1"
from: 1
last_page: 1
last_page_url: "http://localhost:8000/products?page=1"
next_page_url: null
path: "http://localhost:8000/products"
per_page: 10
prev_page_url: null
to: 7
total: 7

1 个答案:

答案 0 :(得分:4)

您当前正在做的是:

axios.get('products', {
    params: {
      id: pDatosFila.f014_id
    }
})

等同于:axios.get('/ products?id = id ')(格式不正确)

您需要做的是:

axios.get('/ products / product_id ')

因此,您可以执行以下操作:

axios.get('products/'+ pDatosFila.f014_id)

,这将起作用。 :)

PS:始终在浏览器的开发人员工具中检查“网络”标签。

参考:axios