gpt4 book ai didi

php - Eloquent 关系设置,同一个表 laravel 5.2 的两个外键

转载 作者:可可西里 更新时间:2023-10-31 22:10:16 25 4
gpt4 key购买 nike

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

  1. 存储在用户表中的与登录相关的用户信息。
  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。

请给我建议解决方案。

谢谢,

最佳答案

嗯,我找到了我自己问题的解决方案。感谢所有认为这个问题有值(value)的人。

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

改变

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');
}
}

就是这样。一切正常。

关于php - Eloquent 关系设置,同一个表 laravel 5.2 的两个外键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35739293/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com