【问题标题】:Eloquent relation setup, two foreign keys to same table laravel 5.2雄辩的关系设置,同一张表laravel 5.2的两个外键
【发布时间】:2016-03-02 05:37:40
【问题描述】:

我遇到了大问题,我在我的项目中维护雄辩的关系设置,我有以下关系:

  1. 与登录相关的用户信息存储在 users 表中。
  2. 存储在配置文件信息中的用户配置文件相关信息。
  3. 用户地址存储在地址表中
  4. 存储在城市、州、国家等配置中的配置相关信息

努力

这是迁移和模型及其关系:

用户迁移表:

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('email')->unique();
            $table->string('password', 60);
            $table->rememberToken();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('users');
    }
}

用户模型:

namespace App;

use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'email', 'password',
    ];

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

    public function profile()
    {
        return $this->hasOne('App\Profile','user_id');
    }
}

配置文件迁移表:

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateProfilesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('profiles', function (Blueprint $table) {
            $table->increments('profile_id');
            $table->integer('user_id')->unsigned();
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
            $table->string('lastname')->nullable();
            $table->string('firstname')->nullable();
            $table->string('gender')->nullable();
            $table->string('phonenumber', 20)->nullable();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('profiles');
    }
}

个人资料模型:

namespace App;

use Illuminate\Database\Eloquent\Model;

class Profile extends Model
{
     protected $fillable = [
        'firstname', 'lastname',
    ];

    public function user(){
        return $this->belongsTo('App\User');
    }
    public function address()
    {
        return $this->hasOne('App\Address','profile_id');
    }
}

配置迁移表:

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateConfigurationsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('configurations', function (Blueprint $table) {
            $table->increments('config_id');
            $table->string('configuration_name');
            $table->string('configuration_type');
            $table->string('parent_id');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('configurations');
    }
}

配置模型:

namespace App;

use Illuminate\Database\Eloquent\Model;

class Configuration extends Model
{
    public function children() {
        return $this->hasMany('App\Configuration','parent_id');
    }
    public function parent() {
        return $this->belongsTo('App\Configuration','parent_id');
    }

    public function city() {
        return $this->hasOne('App\Address', 'city');
    }

    public function state() {
      return $this->hasOne('App\Address', 'state');
    }

    public function country() {
        return $this->hasOne('App\Address', 'country');
    }
}

地址迁移表:

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateAddressesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('addresses', function (Blueprint $table) {
            $table->increments('address_id');
            $table->integer('profile_id')->unsigned();
            $table->foreign('profile_id')->references('profile_id')->on('profiles')->onDelete('cascade');
            $table->string('address')->nullable();
            $table->integer('city')->unsigned();
            $table->foreign('city')->references('config_id')->on('configurations')->onDelete('cascade');
            $table->string('pincode')->nullable();
            $table->integer('state')->unsigned();
            $table->foreign('state')->references('config_id')->on('configurations')->onDelete('cascade');
            $table->integer('country')->unsigned();
            $table->foreign('country')->references('config_id')->on('configurations')->onDelete('cascade');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('addresses');
    }
}

地址模型:

namespace App;

use Illuminate\Database\Eloquent\Model;

class Address extends Model
{
    public function profile(){
        return $this->belongsTo('App\Profile');
    }

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

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

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

我使用Eloquent relation setup, two foreign keys to same table 建立我的项目关系。但不幸的是,上面的代码不会产生期望的输出。看看:

如果我使用命令Auth::user()->profile->firstname,它将打印用户名。因此,如果我使用 Auth::user()->profile->address->city 它应该打印存储在配置表中的城市名称,而是打印名称它打印 id。

请给我建议解决方案。

谢谢,

【问题讨论】:

    标签: php laravel-5


    【解决方案1】:

    好吧,我为自己的问题找到了解决方案。感谢所有认为这个问题有价值的人。

    好吧,我们不需要更改用户或配置文件,我们只需要对配置和地址模型进行一些更改即可。

    改变

    class Address extends Model
    {
        public function profile(){
            return $this->belongsTo('App\Profile');
        }
    
        public function city() {
            return $this->belongsTo('App\Configuration');
        }
    
        public function state() {
          return $this->belongsTo('App\Configuration');
        }
    
        public function country() {
            return $this->belongsTo('App\Configuration');
        }
    }
    

    class Address extends Model
    {
        public function profile(){
            return $this->belongsTo('App\Profile');
        }
    
        public function cityConfiguration() {
            return $this->belongsTo('App\Configuration', 'city');
        }
    
        public function stateConfiguration() {
          return $this->belongsTo('App\Configuration', 'state');
        }
    
        public function countryConfiguration() {
            return $this->belongsTo('App\Configuration', 'country');
        }
    }
    

    和改变

    class Configuration extends Model
    {
        public function children() {
            return $this->hasMany('App\Configuration','parent_id');
        }
        public function parent() {
            return $this->belongsTo('App\Configuration','parent_id');
        }
    
        public function city() {
            return $this->hasOne('App\Address', 'city');
        }
    
        public function state() {
          return $this->hasOne('App\Address', 'state');
        }
    
        public function country() {
            return $this->hasOne('App\Address', 'country');
        }
    }
    

    收件人

    class Configuration extends Model
    {
        public function children() {
            return $this->hasMany('App\Configuration','parent_id');
        }
        public function parent() {
            return $this->belongsTo('App\Configuration','parent_id');
        }
    
        public function city() {
            return $this->hasOne('App\Address', 'city');
        }
    
        public function state() {
          return $this->hasOne('App\Address', 'state');
        }
    
        public function country() {
            return $this->hasOne('App\Address', 'country');
        }
    }
    

    就是这样。一切正常。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-03
      • 2020-06-30
      相关资源
      最近更新 更多