【问题标题】:Error 500 when I try to store data from Vue.JS by Laravel当我尝试通过 Laravel 存储来自 Vue.JS 的数据时出现错误 500
【发布时间】:2019-10-19 00:56:05
【问题描述】:

我的代码有问题。当我尝试使用 Vue.JS 和 Laravel 存储数据时,我在控制台中收到错误 500。我坚持了 6 个小时。

控制器 - DashboardAdvantagesController.php:

  public function store(Request $request)
  {
      $request->validate([
          'icon'=> 'required',
          'title'=> 'required'
      ]);

      $advantage = $request->HomepageAdvantages()->create([
          'icon'=> $request->icon,
          'title'=> $request->title
      ]);

      return response()->json([
         'advantage'=> $advantage,
         'message'=> 'task has been created!'
      ]);

  }

型号 - 主页优势


class HomepageAdvantages extends Model
{
  protected $fillable = [
      'icon', 'title', 'text',
  ];
}

API 路由

Route::post('/advantages/store', 'DashboardAdvantagesController@store');

Vue 组件

  createAdvantage(){

   console.log(this.advantage.icon);
   console.log(this.advantage.title);


   axios.post('http://127.0.0.1:8000/api/advantages/store', {icon: this.advantage.icon, 
    title: this.advantage.title})

     .then(response=>{
     this.advantages.push(response.data.advantage);

                })

              .catch(error=>{

                console.log(error);

                });
                console.log(response.data.advantage);
            },

我的错误:

加载资源失败:服务器响应状态为 500 (内部服务器错误)

:8000/api/advantages/store:1 Failed to load resource: the server responded with a status of 500 (Internal Server Error)
2app.js:267 POST http://127.0.0.1:8000/api/advantages/store 500 (Internal Server Error)
dispatchXhrRequest @ app.js:267
xhrAdapter @ app.js:118
dispatchRequest @ app.js:706
Promise.then (async)
request @ app.js:513
Axios.<computed> @ app.js:533
wrap @ app.js:966
createTask @ app.js:1773
invokeWithErrorHandling @ app.js:9448
invoker @ app.js:9773
original._wrapper @ app.js:15126

【问题讨论】:

  • 遇到 500 内部服务器错误时,您总是要做的第一件事就是检查服务器的错误日志!

标签: javascript php laravel vue.js


【解决方案1】:
$request->HomepageAdvantages()

我不知道你是怎么想出这个的,因为只有当你在Request 类上注册了一个宏时它才会起作用。如果是这样,请忽略此评论。

否则,您需要使用HomepageAdvantages::create() 显式调用模型。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-01
    • 1970-01-01
    • 2013-05-31
    相关资源
    最近更新 更多