framework icon indicating copy to clipboard operation
framework copied to clipboard

Naming/Namespace conflict ?

Open xbagir opened this issue 11 years ago • 1 comments

Why this code is causing the error?

../app/Models/Model.php

<?php namespace App\Models;

use Illuminate\Database\Eloquent\Model as Eloquent;

class Model extends Eloquent {

    protected $table  = 'models';

    public function brand()
    {
        return $this->belongsTo('App\Models\Brand', 'brandId');
    }
}

../app/Models/Brand.php

<?php namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Brand extends Model
{
    protected $table = 'brands';

    public function models()
    {
        return $this->hasMany('App\Models\Model', 'brandId');
    }

}

Controller:

<?php namespace App\Http\Controllers;

use App\Models\Model;

class IndexController extends Controller
{
    public function index()
    {
        $model = Model::find(1);
        dd($model->brand);
    }
}

Error: FatalErrorException in Brand.php line 3: Cannot use Illuminate\Database\Eloquent\Model as Model because the name is already in use

xbagir avatar Feb 25 '15 21:02 xbagir

You have a class called model, then you try to import something and name it mode. You can't do that.

GrahamCampbell avatar Feb 25 '15 21:02 GrahamCampbell