ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2019-06-11 18:16:18  阅读:283  来源: 互联网

标签:php laravel-5-2


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

>与用户表中存储的登录相关的用户信息.
>用户档案相关信息存储在档案信息中.
>存储在地址表中的用户地址
>配置相关信息存储在城市,州,国家等配置中

努力

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

users migration table

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

User Model

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

profile migration table

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

Profile Model

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

Configuration migration table

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

Configuration Model

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

Address Migration Table

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

Address Model

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来建立我的项目关系.但不幸的是,上面的代码并没有产生欲望输出.看一看:

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('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,laravel-5-2
来源: https://codeday.me/bug/20190611/1220283.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有