2015-05-28 63 views
4

我最近將項目從laravel 4.2升級到了laravel 5.0,並且一直面臨着一些錯誤。Laravel 5.0,無法重新聲明class App models Category

我沒有在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'); 
    } 
} 

我搜索上這樣類似的錯誤,但沒有找到任何。

這是我的控制器在「/」路由上調用的函數。

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; 
    } 

讓我知道你是否需要代碼中的其他東西。我一直試圖找出解決方案相當長一段時間了。

+0

你有另一個'Category Class'在相同的命名空間嗎? – DavidDomain

+0

不,我不知道。 我之前在全局名稱空間中已經擁有了所有內容,因此當時我只能想到這個錯誤。其次,這個類別位於App \ models命名空間中,並且在此命名空間中沒有任何其他名爲model的類。 – Yashasvi

+0

錯誤指向Category.php:19,這就是文件結束的地方。那是什麼意思? – Yashasvi

回答

9

這是因爲您已經生成了一個控制器,然後將其拖到子文件夾中。 您需要將名稱空間更改爲正確的名稱空間或正確生成控制器。

php artisan make:controller Api/CategoryController 

或命名空間更改爲

namespace App\Http\Controllers\Api; 

(如果API是控制器在文件夾的名稱)

0

我知道這個問題是舊的,但我會回答什麼都有爲我工作。 我最近在git分支上測試Laravel 4.2到5.0升級。我在名爲Megaloquent的Model類中遇到了同樣的問題,它在Laravel 4.2中擴展了Eloquent,現在是Model。

因爲我試圖得到它的開頭沒有命名空間的工作,我添加了應用程序/ subdirecties在composer.json

"autoload": { 
    "classmap": [ 
     "database", 
     "app/Http/Controllers", 
     "app/Http/Controllers/Auth", 
     "app/Models", 
     "app/Libraries" 
    ], 
    "psr-4": { 
     "App\\": "app/" 
    } 
}, 

的類映射和很多的麻煩得到它的工作後,我決定在控制器和模型中使用命名空間,現在我發現它更加結構化和精簡。你應該從classmap中刪除app/Controllers-Models-Libraries,因爲psr-4已經加載了app/{子目錄}/classes中的所有類,並且classmap autoload使它發生了兩次。你會刪除它們

"autoload": { 
    "classmap": [ 
     "database" 
    ], 
    "psr-4": { 
     "App\\": "app/" 
    } 
}, 

這是Laravel內部配置,你不能真正控制,但除去它們有固定的錯誤後,我僅此得到。