dongwu5743 2016-02-18 06:44
浏览 11
已采纳

在实施雄辩的关系时找不到“电话”类

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

展开全部

  • 写回答

1条回答 默认 最新

  • dsbgltg159136540 2016-02-18 06:53
    关注

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

    展开全部

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部