雄辩有许多人没有回复任何结果

时间:2017-07-16 00:01:14

标签: php laravel eloquent

我有一个关于Eloquent如何为以下内容生成SQL的noob问题。

DB:

  • 客户表

    • ID(公钥)
    • ......(一些通用专栏)
  • 群组表

    • ID(公钥)
    • FK_CUSTOMER_ID(外键)
    • ......(一些通用专栏)

我在客户模型中有以下代码:

public function groups()
{
    return $this->hasMany(Group::class, 'fk_customer_id');
}

我正在尝试让所有群组(在上面的群组功能中),以后我可以将群组缩小到特定的客户范围。

上面的代码生成以下SQL(结果为空结果集,通过查看SQL可以理解)。我不知道为什么where子句(参见下面的SQL)生成,没有多大意义。

select * from `group` where `group`.`fk_customer_id` is null and `group`.`fk_customer_id` is not null limit 1

我希望生成以下SQL:

select * from `group`

另外,如何生成以下SQL(如果我需要根据customer_id选择组,我相信我需要以某种方式添加一些where子句)

select * from `group` where `group`.`fk_customer_id`= SOME_VALUE

谢谢!

---客户模式

<?php

namespace App;

use App\Role;
use Illuminate\Support\Facades\Log;
use Illuminate\Database\Eloquent\Model;

class Customer extends Model
{
    /**
     * Get ALL groups for the customer.
     */
    public function groups()
    {
        return $this->hasMany(Group::class, 'fk_customer_id');
    }

    /**
     * Get ONE group for the customer.
     */
    public function group($groupId)
    {
        return $this->hasMany(Group::class, 'fk_customer_id')->where('id', $groupId);
    }

     /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'customer';
}

群组模型

<?php

namespace App;

use App\Group;
use App\Customer;
use Illuminate\Database\Eloquent\Model;

class Group extends Model
{
    /**
     * Get ONE customer for the group.
     */
    public function customer()
    {
        return $this->belongsTo(Customer::class, 'fk_customer_id');
    }


    /**
     * Get ONE directory configuration for the group.
     */
    public function directoryConfiguration()
    {
        return $this->belongsTo(DirectoryConfiguration::class, 'fk_directory_configuration_id');
    }


    /**
     * Get ONE user for the group.
     */
    public function user($userId)
    {
        return $this->hasMany(User::class, 'fk_role_id')->where('user_id', $userId);
    }

    /**
     * Get ALL user for the group.
     */
    public function users()
    {
        return $this->hasMany(User::class, 'fk_role_id');
    }

     /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'group';
}

0 个答案:

没有答案
相关问题