Laravel 4 将10月CMS中的多对一关系查询为联接

Laravel 4 将10月CMS中的多对一关系查询为联接,laravel-4,eloquent,inner-join,octobercms,Laravel 4,Eloquent,Inner Join,Octobercms,鉴于以下10月CMS插件型号: Schema::create('iaff106_cellphone_cellphones', function($table){ $table->engine = 'InnoDB'; $table->increments('id'); $table->integer('user_id')->unsigned()->nullable()->index(); $table->string('la

鉴于以下10月CMS插件型号:

Schema::create('iaff106_cellphone_cellphones', function($table){
    $table->engine = 'InnoDB';
    $table->increments('id');
    $table->integer('user_id')->unsigned()->nullable()->index();
    $table->string('label');
    $table->string('phone')->nullable()->index();
    $table->integer('provider_id')->nullable();            
    $table->boolean('is_txtable')->default(true);
    $table->boolean('is_published')->default(false);
    $table->timestamps();
    });


Schema::create('iaff106_cellphone_providers', function($table){
    $table->engine = 'InnoDB';
    $table->increments('id');
    $table->string('name');
    $table->string('slug')->index();
    $table->string('mail_domain')->nullable();
    $table->timestamps();
    }); 
供应商关系:

public $belongsToMany = ['IAFF106\CellPhone\Models\Cellphone', 'foreignKey' => 'provider_id'];
手机关系:

public $hasOne = ['IAFF106\CellPhone\Models\Provider'];
我想选择一部手机和相关服务提供商,以获取“电话”和“姓名”字段

在我的组件中,我尝试过:

public function onRun()
{
    $this->cellphone = $this->page['cellphone'] = $this->loadCell();
}

protected function loadCell()
{
    $slug = $this->propertyOrParam('idParam');
    $cellphone = Cellphone::isPublished()->where('id', '=', $slug)->first();
    $this->provider = $this->page['provider'] = $cellphone->provider;

    return $cellphone;
}
$this->手机上有我想要的电话数据,但我不知道如何访问提供商的“姓名”信息

如何从视图中的两个模型加载和访问数据

在我看来,我尝试过这一点:

<ul>
    <li>{{ cellphone.phone }}  {{ provider.name }} </li>
    <li>Try Again </li>
    <li>{{ cellphone.phone }}  {{ cellphone.provider.name }} </li>
</ul>
  • {{mobile.phone}{{provider.name}}
  • 再试一次
  • {{mobile.phone}{{mobile.provider.name}}

{{mobile.phone}}显示良好,但我无法获取提供商名称。

好的,很好,我找到了解决方案,这是您的型号中的一个问题

你需要把

public $belongsTo = [
        'provider' => ['IAFF106\CellPhone\Models\Provider', 'foreignKey' => 'provider_id']
    ];
而不是

public $hasOne = ['IAFF106\CellPhone\Models\Provider'];
下面是mobile.php的模型代码

<?php namespace IAFF106\CellPhone\Models;

use Model;

/**
 * CellPhone Model
 */
class CellPhone extends Model
{

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

    /**
     * @var array Guarded fields
     */
    protected $guarded = ['*'];

    /**
     * @var array Fillable fields
     */
    protected $fillable = [];

    /**
     * @var array Relations
     */

    public $belongsTo = [
        'provider' => ['IAFF106\CellPhone\Models\Provider', 'foreignKey' => 'provider_id']
    ];

    public function scopeIsPublished($query)
    {
        return $query
            ->whereNotNull('is_published')
            ->where('is_published', '=', 1);
    }

}

好的,我找到了解决方案,这是您的模型中的一个问题

你需要把

public $belongsTo = [
        'provider' => ['IAFF106\CellPhone\Models\Provider', 'foreignKey' => 'provider_id']
    ];
而不是

public $hasOne = ['IAFF106\CellPhone\Models\Provider'];
下面是mobile.php的模型代码

<?php namespace IAFF106\CellPhone\Models;

use Model;

/**
 * CellPhone Model
 */
class CellPhone extends Model
{

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

    /**
     * @var array Guarded fields
     */
    protected $guarded = ['*'];

    /**
     * @var array Fillable fields
     */
    protected $fillable = [];

    /**
     * @var array Relations
     */

    public $belongsTo = [
        'provider' => ['IAFF106\CellPhone\Models\Provider', 'foreignKey' => 'provider_id']
    ];

    public function scopeIsPublished($query)
    {
        return $query
            ->whereNotNull('is_published')
            ->where('is_published', '=', 1);
    }

}

好的,我找到了解决方案,这是您的模型中的一个问题

你需要把

public $belongsTo = [
        'provider' => ['IAFF106\CellPhone\Models\Provider', 'foreignKey' => 'provider_id']
    ];
而不是

public $hasOne = ['IAFF106\CellPhone\Models\Provider'];
下面是mobile.php的模型代码

<?php namespace IAFF106\CellPhone\Models;

use Model;

/**
 * CellPhone Model
 */
class CellPhone extends Model
{

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

    /**
     * @var array Guarded fields
     */
    protected $guarded = ['*'];

    /**
     * @var array Fillable fields
     */
    protected $fillable = [];

    /**
     * @var array Relations
     */

    public $belongsTo = [
        'provider' => ['IAFF106\CellPhone\Models\Provider', 'foreignKey' => 'provider_id']
    ];

    public function scopeIsPublished($query)
    {
        return $query
            ->whereNotNull('is_published')
            ->where('is_published', '=', 1);
    }

}

好的,我找到了解决方案,这是您的模型中的一个问题

你需要把

public $belongsTo = [
        'provider' => ['IAFF106\CellPhone\Models\Provider', 'foreignKey' => 'provider_id']
    ];
而不是

public $hasOne = ['IAFF106\CellPhone\Models\Provider'];
下面是mobile.php的模型代码

<?php namespace IAFF106\CellPhone\Models;

use Model;

/**
 * CellPhone Model
 */
class CellPhone extends Model
{

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

    /**
     * @var array Guarded fields
     */
    protected $guarded = ['*'];

    /**
     * @var array Fillable fields
     */
    protected $fillable = [];

    /**
     * @var array Relations
     */

    public $belongsTo = [
        'provider' => ['IAFF106\CellPhone\Models\Provider', 'foreignKey' => 'provider_id']
    ];

    public function scopeIsPublished($query)
    {
        return $query
            ->whereNotNull('is_published')
            ->where('is_published', '=', 1);
    }

}

感谢Anand Patel发现了关系中的主要问题

为了方便其他人,我在这里再次发布了简单但关键的代码片段。 我确实让它像这样工作

在手机型号中:

/**
 * @var array Guarded fields
 */
protected $guarded = [];


/**
 * @var array Relations
 */
public $belongsTo = [
    'user' =>       ['RainLab\User\Models\User', 
                    'foreignKey' => 'user_id'],
    'provider' =>   ['IAFF106\CellPhone\Models\Provider', 
                    'foreignKey' => 'provider_id']
    ];
在提供程序模型中:

public $hasMany = ['cellphone' =>   ['IAFF106\CellPhone\Models\Cellphone', 
                    'foreignKey' => 'provider_id']
    ];
在第一部分中,我做到了:

$cellphones = Cellphone::where('user_id', '=', $this->userid)->get();
我认为:

<ul>
{% for cellphone in cellphones  %}
    <li>
        {{ cellphone.label }} : <strong>{{ cellphone.phone }} </strong>  
        <sub>{{ cellphone.provider.name }}</sub>
    </li>
{% endfor %}
</ul>
    {%用于手机中的手机%}
  • {{mobile.label}:{{{mobile.phone}} {{mobile.provider.name}
  • {%endfor%}

我最初的主要问题是如何格式化关系数组。我需要为关系数组指定键。将我的第一篇文章与这篇文章进行比较,看看我的意思。

感谢Anand Patel找到了关系中的主要问题

为了方便其他人,我在这里再次发布了简单但关键的代码片段。 我确实让它像这样工作

在手机型号中:

/**
 * @var array Guarded fields
 */
protected $guarded = [];


/**
 * @var array Relations
 */
public $belongsTo = [
    'user' =>       ['RainLab\User\Models\User', 
                    'foreignKey' => 'user_id'],
    'provider' =>   ['IAFF106\CellPhone\Models\Provider', 
                    'foreignKey' => 'provider_id']
    ];
在提供程序模型中:

public $hasMany = ['cellphone' =>   ['IAFF106\CellPhone\Models\Cellphone', 
                    'foreignKey' => 'provider_id']
    ];
在第一部分中,我做到了:

$cellphones = Cellphone::where('user_id', '=', $this->userid)->get();
我认为:

<ul>
{% for cellphone in cellphones  %}
    <li>
        {{ cellphone.label }} : <strong>{{ cellphone.phone }} </strong>  
        <sub>{{ cellphone.provider.name }}</sub>
    </li>
{% endfor %}
</ul>
    {%用于手机中的手机%}
  • {{mobile.label}:{{{mobile.phone}} {{mobile.provider.name}
  • {%endfor%}

我最初的主要问题是如何格式化关系数组。我需要为关系数组指定键。将我的第一篇文章与这篇文章进行比较,看看我的意思。

感谢Anand Patel找到了关系中的主要问题

为了方便其他人,我在这里再次发布了简单但关键的代码片段。 我确实让它像这样工作

在手机型号中:

/**
 * @var array Guarded fields
 */
protected $guarded = [];


/**
 * @var array Relations
 */
public $belongsTo = [
    'user' =>       ['RainLab\User\Models\User', 
                    'foreignKey' => 'user_id'],
    'provider' =>   ['IAFF106\CellPhone\Models\Provider', 
                    'foreignKey' => 'provider_id']
    ];
在提供程序模型中:

public $hasMany = ['cellphone' =>   ['IAFF106\CellPhone\Models\Cellphone', 
                    'foreignKey' => 'provider_id']
    ];
在第一部分中,我做到了:

$cellphones = Cellphone::where('user_id', '=', $this->userid)->get();
我认为:

<ul>
{% for cellphone in cellphones  %}
    <li>
        {{ cellphone.label }} : <strong>{{ cellphone.phone }} </strong>  
        <sub>{{ cellphone.provider.name }}</sub>
    </li>
{% endfor %}
</ul>
    {%用于手机中的手机%}
  • {{mobile.label}:{{{mobile.phone}} {{mobile.provider.name}
  • {%endfor%}

我最初的主要问题是如何格式化关系数组。我需要为关系数组指定键。将我的第一篇文章与这篇文章进行比较,看看我的意思。

感谢Anand Patel找到了关系中的主要问题

为了方便其他人,我在这里再次发布了简单但关键的代码片段。 我确实让它像这样工作

在手机型号中:

/**
 * @var array Guarded fields
 */
protected $guarded = [];


/**
 * @var array Relations
 */
public $belongsTo = [
    'user' =>       ['RainLab\User\Models\User', 
                    'foreignKey' => 'user_id'],
    'provider' =>   ['IAFF106\CellPhone\Models\Provider', 
                    'foreignKey' => 'provider_id']
    ];
在提供程序模型中:

public $hasMany = ['cellphone' =>   ['IAFF106\CellPhone\Models\Cellphone', 
                    'foreignKey' => 'provider_id']
    ];
在第一部分中,我做到了:

$cellphones = Cellphone::where('user_id', '=', $this->userid)->get();
我认为:

<ul>
{% for cellphone in cellphones  %}
    <li>
        {{ cellphone.label }} : <strong>{{ cellphone.phone }} </strong>  
        <sub>{{ cellphone.provider.name }}</sub>
    </li>
{% endfor %}
</ul>
    {%用于手机中的手机%}
  • {{mobile.label}:{{{mobile.phone}} {{mobile.provider.name}
  • {%endfor%}

我最初的主要问题是如何格式化关系数组。我需要为关系数组指定键。比较我的第一篇文章和这篇文章,看看我的意思。

您想在哪里访问提供商名称?在PHP或视图中的意思?我编辑了我的帖子以添加我试图实现的视图。您想在哪里访问提供者名称?在PHP或视图中的意思?我编辑了我的帖子以添加我试图实现的视图。您想在哪里访问提供者名称?在PHP或视图中的意思?我编辑了我的帖子以添加我试图实现的视图。您想在哪里访问提供者名称?在PHP或视图中的意思?我编辑了我的帖子,以添加我试图实现的视图。