laravel 5.4 angular 1.6.8 GET 500 (внутренняя ошибка сервера)

Я учусь на угловой. Это мой первый тестовый проект. Я пытаюсь сохранить данные в базе данных && получить и показать данные из базы данных, используя laravel 5.4 и angular 1.6.8. Когда я сохраняю данные, используя $http.post метод это работает. Но когда я пытаюсь получить данные из базы данных и показать их, я получаю эту ошибку:

ПОЛУЧИТЕ http://test.com/demos/angular_lara/api/v1/employees 500 (Внутренняя ошибка сервера)

Кроме того, когда я проверил storage/logs/laravel.log файл, я нашел это:

local.ERROR: exception 'BadMethodCallException' with message 'Method [orderBy] does not exist.' in /home/demos/public_html/demos/angular_lara/vendor/laravel/framework/src/Illuminate/Routing/Controller.php:82 

Но я не могу найти ничего плохого в этом вызове метода.

Вот мой модуль:

app.js:

var app = angular.module('employeeRecords',[])
    .constant('API_URL', 'http://test.com/demos/angular_lara/api/v1/');
app.config(function($interpolateProvider) {
    $interpolateProvider.startSymbol('<%');
    $interpolateProvider.endSymbol('%>');
});
app.config(['$qProvider', function ($qProvider) {
    $qProvider.errorOnUnhandledRejections(false);
}]);

Вот мой угловой контроллер:

employees.js

app.controller('employeesController', function($scope, $http, API_URL) {

    $http.get(API_URL + "employees")
        .then(function(response) {
            $scope.employees = response.data;
            console.log( $scope.employees);
        });
    //show modal form
    $scope.toggle = function(modalstate, id) {
        $scope.modalstate = modalstate;
        // console.log('add');
        switch (modalstate) {
            case 'add':
                $scope.form_title = "Add New Employee";
                break;
            case 'edit':
                $scope.form_title = "Employee Detail";
                $scope.id = id;
                $http.get(API_URL + 'employees/' + id)
                    .then(function(response) {
                        console.log(response.data);
                        $scope.employee = response.data;
                    });
                break;
            default:
                break;
        }
        console.log(id);
        $('#myModal').modal('show');
    }
    //save new record / update existing record
    $scope.save = function(modalstate, id) {
        var url = API_URL + "employees";
        //append employee id to the URL if the form is in edit mode
        if (modalstate === 'edit'){
            url += "/" + id;
        }
        $http({
            method: 'POST',
            url: url,
            data: $.param($scope.employee),
            headers: {'Content-Type': 'application/x-www-form-urlencoded'}
        }).then(function(response) {
            console.log(response.data);
            console.log(111);
            //location.reload();
        });
    } 
});

Вот мой файл маршрута:

web.php

<?php
Route::get('/', function () {
    return view('index');
});
Route::get('/api/v1/employees/{id?}', 'Employees@index');
Route::post('/api/v1/employees', 'Employees@store');

Контроллер Laravel: Employees.php

<?php

namespace App\Http\Controllers;
use App\Employee;
use Illuminate\Http\Request;

class Employees extends Controller
{
    public function index($id = null)
    {
        if($id == null){
            return Employees::orderBy('id', 'asc')->get();
        }else{
            return $this->show($id);
        }
    }

    public function show($id)
    {
        return Employee::find($id);
    }

    public function store(Request $request)
    {
        $employee = new Employee();
        $employee->name = $request['name'];
        $employee->email = $request['email'];
        $employee->contact_number = $request['contact_number'];
        $employee->position = $request['position'];
        $employee->save();
        return 'Record inserted successfully with id '. $employee->id;

    }
}

Я погуглил это некоторое время, но все еще не могу найти решение. Может кто-нибудь сказать мне, как я могу преодолеть эту ошибку?

0 ответов

Другие вопросы по тегам