You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Vue+Axios对接Laravel后端CRUD遇500内部服务器错误求助

Hey there! Let's work through this 500 error step by step—since you're new to the Vue + Laravel stack, I'll keep things straightforward and include working examples to help you get your CRUD up and running.

First, Figure Out What's Causing the Server Error

A 500 Internal Server Error means something broke on Laravel's side, but the browser doesn't tell you the details. Your first stop should be Laravel's error logs:

  • Open the file storage/logs/laravel.log in your Laravel project.
  • Look for the most recent error entry (it'll be near the bottom). Common issues here include:
    • Missing routes for /api/despesas
    • A broken database query in your controller
    • A missing Despesa model or incorrect table configuration
    • Permission issues with the storage folder

Fix the Laravel Side (Core Setup)

Let's make sure your backend is configured correctly for the /api/despesas endpoint:

1. Define the API Route

Open routes/api.php and add this route (it maps the GET request to a controller method):

use App\Http\Controllers\DespesaController;

// GET /api/despesas to fetch all expenses
Route::get('/despesas', [DespesaController::class, 'index']);

2. Create the Controller & Index Method

Run this command in your terminal to generate the controller:

php artisan make:controller DespesaController

Then open app/Http/Controllers/DespesaController.php and update the index method:

namespace App\Http\Controllers;

use App\Models\Despesa;
use Illuminate\Http\Request;

class DespesaController extends Controller
{
    public function index()
    {
        // Fetch all records from the despesas table
        $despesas = Despesa::all();
        
        // Return data in the format your Vue app expects
        return response()->json(['data' => $despesas]);
    }
}

3. Ensure the Despesa Model & Table Exist

  • Make sure you have a Despesa model (run php artisan make:model Despesa if not).
  • Create the despesas table with a migration:
    php artisan make:migration create_despesas_table
    
    Open the migration file (in database/migrations) and add your desired columns, then run:
    php artisan migrate
    

Fix the Vue/Axios Side

Now let's make sure your frontend is sending requests correctly:

1. Configure Axios Base URL

In your Vue project (e.g., main.js for Vue 2, main.js/main.ts for Vue 3), set the base URL for Axios to point to your Laravel API:

import axios from 'axios';

// Vue 2
Vue.prototype.$axios = axios;
axios.defaults.baseURL = 'http://127.0.0.1:8000/api';

// Vue 3
import { createApp } from 'vue';
const app = createApp(App);
app.config.globalProperties.$axios = axios;
axios.defaults.baseURL = 'http://127.0.0.1:8000/api';

2. Update Your Component's Fetch Method

Rewrite your component's getDespesa method to include error handling (so you can see more details if something goes wrong):

export default {
  data() {
    return {
      despesas: []
    };
  },
  created() {
    this.getDespesa();
  },
  methods: {
    async getDespesa() {
      try {
        const response = await this.$axios.get('/despesas');
        this.despesas = response.data.data;
      } catch (error) {
        // Log detailed error info to the console
        console.error('Failed to fetch expenses:', error.response?.data || error.message);
        // You can also show a user-friendly alert here
      }
    }
  }
};

Quick Checks Before Testing

  • Make sure Laravel's development server is running: php artisan serve
  • Verify your database credentials in .env are correct (DB_HOST, DB_DATABASE, DB_USERNAME, DB_PASSWORD)
  • If Vue is running on a different port (e.g., 8080), enable CORS in Laravel: install the barryvdh/laravel-cors package and configure it to allow your Vue app's origin.

内容的提问来源于stack exchange,提问作者GuilhermeSO

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 07:33:15