【问题标题】:Laravel Relationship Not WorkingLaravel 关系不起作用
【发布时间】:2015-09-24 06:37:59
【问题描述】:

我正在尝试在两个 Eloquent 模型 User 和 Company 之间建立关系。 User 是新 Laravel 项目的标准模型,Company 是使用 Artisan 创建的模型。

我已经通过以下方式设置了关系:

用户类别:

<?php

namespace App;

use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;

class User extends Model implements AuthenticatableContract, CanResetPasswordContract
{
    use Authenticatable, CanResetPassword;

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'users';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['name', 'email', 'password'];

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = ['password', 'remember_token'];

    public function companies() {
        return $this->hasMany('App\Company');
    }
}

公司类别:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use App\User;

class Company extends Model
{

    protected $table = 'companies';

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


}

我正在尝试使用 DatabaseSeeder 类中的以下代码创建模型(它也在 tinker 中执行此操作):

$user = new \App\User();
$user->name = "Josh Pennington";
$user->email = 'xxx@xxx.xxx';
$user->password = bcrypt('xxx');
$user->save();

$company = new \App\Company();
$company->name = "Business Name";
$company->default_tax_rate = 6.5;
$company->owning_user = $user;
$company->save();

但是,我收到以下错误:

[Illuminate\Database\QueryException]                                                                                                                 
  SQLSTATE[42S22]: Column not found: 1054 Unknown column 'owning_user' in 'field list' (SQL: insert into `companies` (`name`, `default_tax_rate`, `ow  
  ning_user`, `updated_at`, `created_at`) values (Business Name, 6.5, {"name":"Josh Pennington","email":"xxx@xxx.xxx","updated_at":"2015-07-06 23:09:  
  11","created_at":"2015-07-06 23:09:11","id":1}, 2015-07-06 23:09:11, 2015-07-06 23:09:11)) 

如您所见,它认为 owning_user 是列名,而实际上它应该是 user_id 作为列名,并且它不应该在查询中给出对象的 JSON 版本。我犯了什么明显的错误?

【问题讨论】:

    标签: php laravel laravel-5


    【解决方案1】:

    您可以这样做并将外键提供给公司类的“user_id”属性:

    $company = new \App\Company();
    $company->name = "Business Name";
    $company->default_tax_rate = 6.5;
    $company->user_id = $user->id;
    $company->save();
    

    或者您可以使用公司类的雄辩的 owning_user 方法,将用户实例传递给它。

    $company = new \App\Company();
    $company->name = "Business Name";
    $company->default_tax_rate = 6.5;
    $company->owning_user()->associate($user);
    $company->save();
    

    【讨论】:

    • 谢谢。我误解了您如何在课堂上使用关联功能。仅用于获取相关模型,不用于创建关系
    猜你喜欢
    • 2017-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-18
    • 2016-07-26
    • 2020-03-27
    相关资源
    最近更新 更多