dongwu5743 2016-02-18 14: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 14: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');
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊