php – Eloquent关系设置,同一个表laravel 5.2的两个外键
发布时间:2020-12-14 19:44:33 所属栏目:大数据 来源:网络整理
导读:我遇到了大问题,我在我的项目中保持雄辩的关系设置,我有以下关系: 与用户表中存储的登录相关的用户信息. 用户档案相关信息存储在档案信息中. 存储在地址表中的用户地址 配置相关信息存储在城市,州,国家等配置中 努力 这是迁移和模型及其关系: users migrat
我遇到了大问题,我在我的项目中保持雄辩的关系设置,我有以下关系:
>与用户表中存储的登录相关的用户信息. 努力 这是迁移和模型及其关系: users migration table use IlluminateDatabaseSchemaBlueprint; use IlluminateDatabaseMigrationsMigration; 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'); } } User Model namespace App; use IlluminateFoundationAuthUser 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('AppProfile','user_id'); } } profile migration table use IlluminateDatabaseSchemaBlueprint; use IlluminateDatabaseMigrationsMigration; 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'); } } Profile Model namespace App; use IlluminateDatabaseEloquentModel; class Profile extends Model { protected $fillable = [ 'firstname','lastname',]; public function user(){ return $this->belongsTo('AppUser'); } public function address() { return $this->hasOne('AppAddress','profile_id'); } } Configuration migration table use IlluminateDatabaseSchemaBlueprint; use IlluminateDatabaseMigrationsMigration; 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'); } } Configuration Model namespace App; use IlluminateDatabaseEloquentModel; class Configuration extends Model { public function children() { return $this->hasMany('AppConfiguration','parent_id'); } public function parent() { return $this->belongsTo('AppConfiguration','parent_id'); } public function city() { return $this->hasOne('AppAddress','city'); } public function state() { return $this->hasOne('AppAddress','state'); } public function country() { return $this->hasOne('AppAddress','country'); } } Address Migration Table use IlluminateDatabaseSchemaBlueprint; use IlluminateDatabaseMigrationsMigration; 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'); } } Address Model namespace App; use IlluminateDatabaseEloquentModel; class Address extends Model { public function profile(){ return $this->belongsTo('AppProfile'); } public function city() { return $this->belongsTo('AppConfiguration'); } public function state() { return $this->belongsTo('AppConfiguration'); } public function country() { return $this->belongsTo('AppConfiguration'); } } 我用Eloquent relation setup,two foreign keys to same table来建立我的项目关系.但不幸的是,上面的代码并没有产生欲望输出.看一看: If i use below command: Auth::user()->profile->firstname it will print user first name So if i use Auth::user()->profile->address->city it should print city name stored in configuration table,but instead print name it print the id. 请建议我的解决方案. 谢谢, 解决方法
好吧,我为自己的问题找到了解决方案.感谢所有认为这个问题有价值的人.
好吧,我们不需要更改用户或配置文件,我们只需要在配置和地址模型中进行一些更改. 更改 class Address extends Model { public function profile(){ return $this->belongsTo('AppProfile'); } public function city() { return $this->belongsTo('AppConfiguration'); } public function state() { return $this->belongsTo('AppConfiguration'); } public function country() { return $this->belongsTo('AppConfiguration'); } } 至 class Address extends Model { public function profile(){ return $this->belongsTo('AppProfile'); } public function cityConfiguration() { return $this->belongsTo('AppConfiguration','city'); } public function stateConfiguration() { return $this->belongsTo('AppConfiguration','state'); } public function countryConfiguration() { return $this->belongsTo('AppConfiguration','country'); } } 并改变 class Configuration extends Model { public function children() { return $this->hasMany('AppConfiguration','country'); } } 至 class Configuration extends Model { public function children() { return $this->hasMany('AppConfiguration','country'); } } 就是这样.一切都很好. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |