我之前已經發布了沒有真正的回復,所以我再試一次。我正在嘗試構建一個簡單的表來從 mongodb 資料庫中讀取資料,該表將通過使用 laravel 中的查詢構建器來檢索資料需要使用 vue 組件顯示但是當我嘗試加載頁面時出現錯誤訊息:如果需要,完整的錯誤訊息:現在編輯錯誤已得到修復新問題是沒有資料只顯示表的標題
ErrorException
Array to string conversion
at vendor/laravel/framework/src/Illuminate/Routing/ResourceRegistrar.php:416
412▕ protected function getResourceAction($resource, $controller, $method, $options)
413▕ {
414▕ $name = $this->getResourceRouteName($resource, $method, $options);
415▕
? 416▕ $action = ['as' => $name, 'uses' => $controller.'@'.$method];
417▕
418▕ if (isset($options['middleware'])) {
419▕ $action['middleware'] = $options['middleware'];
420▕ }
14 vendor frames
15 routes/api.php:20
Illuminate\Support\Facades\Facade::__callStatic()
3 vendor frames
19 routes/api.php:21
Illuminate\Routing\RouteRegistrar::group()
如果您希望復制,這是我使用的代碼:
api.php 路由:
Route::middleware('api')->group(function () {
Route::resource('/home', [ProductController::class,'home']);
});
web.php 路由:
Route::get('{any}', function () {
return view('home');
})->where('any', '.*');
PostController Home 方法:
public function home() {
$posts = Post::paginate(4);
return response()->json($posts);
}
家庭組件:
<template>
<div>
<table>
<tr>
<td>Id</td>
<td>Title</td>
<td>Status</td>
</tr>
<tr v-for="El in results" v-bind:key="El.id" >
<td>{{El.id}}</td>
<td>{{El.STATUS}}</td>
</tr>
</table>
</div>
</template>
<script>
export default{
data() {
return {
results: []
};
},
methods: {
fetch() {
axios.get('/api/home')
.then(response => this.results = response.data)
.catch(error => console.log(error));
}
}
}
</script>
主頁.blade.php:
@extends('layouts.app')
@section('content')
<div class="container" id="app">
<home-component></home-component>
</div>
@endsection
代碼似乎運行良好,問題從 api 路由處理程式開始,但為什么以及如何解決它超出了我的范圍。所以,任何和所有的幫助和建議都值得贊賞。編輯:最初的問題是關于一個錯誤,該錯誤已被洗掉,但被上面提到的一個新問題取代。
uj5u.com熱心網友回復:
您的問題是您為該方法提供了太多引數Route::resource(...)。
該Route::resource方法的目的是根據資源控制器模式定義多個路由及其對應的控制器動作。
在您的情況下,您肯定沒有使用資源豐富的控制器,因為您已將控制器操作定義為public function home(). 要解決此問題,您可以簡單地Route::get(...)為您在 vue 組件中呼叫的端點定義。
Route::middleware('api')->group(function () {
Route::get('/home', [ProductController::class, 'home']);
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/480688.html
標籤:javascript php 拉拉维尔 Vue.js
