Laravel未建立一对多关系

时间:2019-12-22 12:40:56

标签: php laravel eloquent laravel-6

我正在做一个学习项目。对Laravel来说很新因此,我有一个用户和一个公司资料CRUD。公司属于用户,用户可能有多个公司。因此,在我的用户模型中,我实现了

<?php

namespace App\Models;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use App\Company;

class User extends Authenticatable
{
    use Notifiable;

    protected $fillable = [
        'first_name', 'last_name', 'username', 'email', 'password',
    ];


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


    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

    //relation with company
    public function company(){
        return $this->hasMany('App\Company','id');
    }
}

在公司模式下,我做到了

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Company extends Model
{
    //table name
    protected $table='companies';
    //primary keys
    protected $primaryKey='id';

    //relation with User Model

    public function user(){
        return $this->belongsTo(App\Models\User::class);
}

我的公司资料管理员是


public function index()
    {
        //Showing companies under user

        $user_id = auth()->user()->id;
        $user = User::find($user_id);
        $companies=$user->company;

        return view('company.profile')->with('companies', $companies);
    }

但是在执行方面,似乎是

public function user(){
        return $this->belongsTo(App\Models\User::class);
}

公司模型中的此功能不起作用。我的意思是将一家公司分配给一个用户,但应该像一个用户中的许多公司一样。我做错了什么?

我的用户模型位置是App / Model / User.php,我在auth.php中声明了用户模型路径。我的Company.php模型位置是App / Company.php。请看看并尝试帮助此新手。非常感谢。

2 个答案:

答案 0 :(得分:0)

我不明白您的问题,但我的答案可能会为您提供帮助

用户属于公司

模型用户


public function company()
{
    return $this->belongsTo('App\Company');

}

可能是错误的代码


//relation with company
public function company(){
    return $this->hasMany('App\Company','id'); // not Id foreign_key as company_id
}

这是真的,但是您可以写得更好

public function index()
{
    $user_id = auth()->user()->id;
    $user = User::find($user_id)->with('company');
    //$companies=$user->company;

    return view('company.profile', compact('user');
}

答案 1 :(得分:0)

更改您的Company.php喜欢

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Company extends Model
{
    //table name
    protected $table='companies';
    //primary keys
    protected $primaryKey='id';

    //relation with User Model

    public function user(){
        return $this->belongsTo('App\User');
}

更改您的User.php喜欢

<?php

namespace App\Models;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use App\Company;

class User extends Authenticatable
{
    use Notifiable;

    protected $fillable = [
        'first_name', 'last_name', 'username', 'email', 'password',
    ];


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


    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

    //relation with company
    public function company(){
        return $this->hasMany('App\Company');
    }
}

在您的控制器中

public function index()
{
    $user_id = auth()->user()->id;
    $user = User::with('company')->find($user_id);
    return view('company.profile', compact('user');
}