AL-zami
AL-zami

Reputation: 9076

Class 'phone ' not found while implementing eloquent relationship

This is the first time i am trying to use eloquent relationship.I have a userModel and a phoneModel class.They represents users and phone table respectively. Here i am trying to access The phone number of a user when he/she logged in.

users table has the field (id,name,password) and phone table has the (field id,phone_no,user_id)

phone migration is below:

public function up()
{
    //
    Schema::create('phone',function(Blueprint $table){
       $table->increments('id');
       $table->string('phone_no',20);
       $table->integer('user_id')->unsigned();
       $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
    });
}

i applied hasOne and belongs to relationship on both models:

userModel.php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Contracts\Auth\Authenticatable;
class userModel extends Model implements Authenticatable
{
    //
    use \Illuminate\Auth\Authenticatable;
    protected $table = 'users';

    public function phone(){
             $this->hasOne('App\Models\phone');
    }

}

phoneModel.php:

namespace App;

use Illuminate\Database\Eloquent\Model;

    class phoneModel extends Model
    {
        //
        protected $table='phone';
        public function user()
        {
            return $this->belongsTo('users');
        }
    }

Now when i tried to get a phone number from logged user i get an error called class 'phone' not found

Here is the code inside show method of userController:

public function show($user)
{
    //
    $indicator=is_numeric($user)?'id':'name';
    $info=userModel::where($indicator,'=',$user)->get()->first();
    if($info){
       $phone = userModel::find($info->id)->phone;
       $data=array('info'=>$info,'phone'=>$phone);
       return View::make('user.show')->with($data);
    }else{
      $info=userModel::where($indicator,'=', Auth::user()->name)->get()->first();
      return View::make('user.show')->with('user',$info);
    }
}

Upvotes: 0

Views: 447

Answers (1)

Jeff
Jeff

Reputation: 25221

You named your phone class phoneModel but you added the relationship as $this->hasOne('App\Models\phone');. You also created those classes in the App namespace but referenced them as App\Models\class.

The standard practice is to name your model classes after the model and using uppercase letters. So your classes would be called User and Phone rather than userModel and phoneModel. And the database tables would be users and phones. If you use these standards, Laravel will take care of a lot of things automatically behind the scenes.

User class

namespace App;

class User extends Model implements Authenticatable
{
//
use \Illuminate\Auth\Authenticatable;

//Laravel will assume the User model is in the table `users` so you don't need to specify

public function phone(){
         $this->hasOne('App\Phone');
}

Phone Class

namespace App;

class Phone extends Model
{
    //Laravel will assume the Phone model is in the table `phones` so you don't need to specify
    public function user()
    {
        return $this->belongsTo('App\User');
    }

Upvotes: 3

Related Questions