dongshi4078 2017-07-16 00:01
浏览 23

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

I have a noob question about how Eloquent generates SQL for the following.

DB:

  • Customer Table:

    • ID (Public Key)
    • ... (Some general columns)
  • Group Table:

    • ID (Public Key)
    • FK_CUSTOMER_ID (Foreign Key)
    • ... (Some general columns)

I have the following code in Customer Model:

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

I am trying to get all groups (in the groups function above), that I can narrow down the groups later, to a particular customer.

The above code generates the following SQL (which results an empty result set, which is understandable by looking at the SQL). I have no idea, why the where clause (see the SQL below) gets generated, does not makes much sense.

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

I would like the following SQL to be generated :

select * from `group`

also, how to get the following SQL generated (If I need to select groups based on customer_id, I believe I'll need to add some where clause somehow)

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

Thanks!

--- Customer Model

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

Group Model


<?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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
    • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
    • ¥15 CSAPPattacklab
    • ¥15 一直显示正在等待HID—ISP
    • ¥15 Python turtle 画图
    • ¥15 关于大棚监测的pcb板设计
    • ¥15 stm32开发clion时遇到的编译问题
    • ¥15 lna设计 源简并电感型共源放大器
    • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
    • ¥15 Vue3地图和异步函数使用