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 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算