model - Laravel 5.0,无法重新声明类 App\models\Category

标签 model runtime-error laravel-5 redeclaration

我最近将我的项目从 laravel 4.2 升级到 laravel 5.0,并且遇到了几个错误。

我没有在 4.2 版本中定义任何命名空间,但正如建议的 here,

我已经开始在我的代码中定义命名空间。我不知道我面临的问题是否与此有关,但它发生在此过程的中间。

运行代码时出现以下错误:

exception 'Symfony\Component\Debug\Exception\FatalErrorException' with
message 'Cannot redeclare class App\models\Category' in  
/Users/yash/summers/lightsCameraDinner/lcd_updated/app/models/Category.php:19

这是我的 Category.php:
<?php namespace App\models;

use Eloquent;

class Category extends Eloquent {

  protected $table = 'categories';
  protected $guarded = array('id');

  // Defining 'Many to Many' Relationship with 'VendorProfile' Model
  public function clients() {
    return $this->belongsToMany('Client');
  }

  // Defining 'One to Many' Relationship with 'Job' Model
  public function jobs() {
    return $this->hasMany('Job');
  }
}

我在 SO 上搜索了类似的错误,但没有找到。

这是我的 Controller 中在“/”路由上调用的函数。
    public function getIndex() {
    $categories = Category::all();

    $messages = Message::groupBy('receiver_id')
                ->select(['receiver_id', \DB::raw("COUNT('receiver_id') AS total")])
                ->orderBy('total', 'DESC')
                ->get()
                ->toArray();

    $vendors_ids = array();
    foreach ($messages as $message) {
      $vendors_ids[] = $message['receiver_id'];
    }

    $clients = Client::where('profile_type', 'VendorProfile')
                      ->where('is_activated', 1)
                      ->whereIn('id', $vendors_ids)
                      ->limit(4)
                      ->get();

    if($clients->count() < 4) {
      $clients = Client::where('profile_type', 'VendorProfile')
                        ->where('is_activated', 1)
                        ->limit(4)
                        ->get();
    }   
    Log::info('getIndex function of PagesController');
    $this->layout = View::make('layouts.homepage');
    $this->layout->content = View::make('pages/index', ['categories' => $categories, 'clients' => $clients]);
    return $this->layout;
  }

如果您需要代码中的其他内容,请告诉我。一段时间以来,我一直试图找出解决方案。

最佳答案

这是因为您已经生成了一个 Controller ,然后将其拖到了一个子文件夹中。
您需要将 namespace 更改为正确的 namespace 或正确生成您的 Controller 。

php artisan make:controller Api/CategoryController  

或将命名空间更改为
namespace App\Http\Controllers\Api;

(如果 api 是 Controller 所在文件夹的名称)

关于model - Laravel 5.0,无法重新声明类 App\models\Category,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30504221/

相关文章:

asp.net-mvc - 如何在ASP.NET MVC模型中获取当前用户

sql - Microsoft Access 找不到字段 '|1'

java - 从字符串中删除除最后 n 个字符之外的所有字符

javascript - 带有ajax响应的数据表

php - Laravel 5 CMS 制作?

php - Laravel 5.7 中无法使用软删除功能进行详细删除

database - Django 与附加字段的多对多关系

javascript - 商店中的 IDPROPERTY FOR COMBINED ID's

java - 模型之间的交互 ​​MVC Java

debugging - 当底层作业成功完成时,Oozie 工作流在 Hive 作业上出错