简体   繁体   English

用户和用户表之间的laravel关系

[英]laravel relationship between user and user_info tables

i'm newbie on laravel 5.1 so i have an easy problem. 我是laravel 5.1的新手,所以我有一个简单的问题。

i have user table and user_info table. 我有用户表和user_info表。 if i create a user, i want to create another record for user_info table and if i get user then i want to get user_info record but i don't do this. 如果我创建一个用户,我想为user_info表创建另一个记录,如果我获得用户,那么我想获得user_info记录,但我不这样做。

codes are below. 代码如下。

<?php
namespace App;

use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{

    protected $fillable = [
        'name', 'email', 'password',
    ];

    protected $hidden = [
        'password', 'remember_token',
    ];

    public function profile()
    {
        return $this->hasOne('Http\Models\userInfo');
    }
}

this is userInfo class 这是userInfo类

<?php

namespace App\Http\Models;

use Illuminate\Database\Eloquent\Model;

class userInfo extends Model
{
    protected $table = 'user_info';
}

this is welcome page 这是欢迎页面

@extends('master')

@section('content')
<div class="container">
    <div class="row">
        <div class="col-md-10 col-md-offset-1">
            <div class="panel panel-default">
                <div class="panel-heading">Welcome</div>

                <div class="panel-body">
                    <pre>{{ dd(Auth::user()->profile) }}</pre>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

i got this error 我得到这个错误

FatalErrorException in Model.php line 740:
Class 'Http\Models\userInfo' not found

where am I doing wrong? 我在哪里做错了?

It is because of the namespace mismatch. 这是因为名称空间不匹配。 Just replace: 只需替换:

   return $this->hasOne('Http\Models\userInfo');

by 通过

   return $this->hasOne('App\Http\Models\userInfo');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM