Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 使用Laravel比较不同表中的两个字段_Mysql_Laravel_Join - Fatal编程技术网

Mysql 使用Laravel比较不同表中的两个字段

Mysql 使用Laravel比较不同表中的两个字段,mysql,laravel,join,Mysql,Laravel,Join,我有两个表,员工和客户,我给出了下面的模式 Customers('id' , 'username', 'location'); Employees('id' , 'EmployeeID' , 'CustomerID', 'location'); 目前,我可以使用查询检索客户详细信息,如下面的查询,请注意,这是当用户登录到系统时,因此是Auth:: $customerQuery1 = DB::table('customer') ->where('id', '!='

我有两个表,员工和客户,我给出了下面的模式

Customers('id' , 'username', 'location');

Employees('id' , 'EmployeeID' , 'CustomerID', 'location');
目前,我可以使用查询检索客户详细信息,如下面的查询,请注意,这是当用户登录到系统时,因此是Auth::

$customerQuery1 = DB::table('customer')
            ->where('id', '!=', Auth::id())  
            ->where('item', '=' , Auth::customer()->recommendation)  
            ->get();
每个员工都有许多客户,我希望其他客户看到其他客户项目,因此我附加了
CustomerID
字段,它是一个外键,与customer表中的
id
字段相关

我尝试了下面的方法,但是我想我可能需要一个连接查询,但是我不确定

$query2 =  DB::table('Customer','Employee')
        ->select('username')
        ->where(['EmployeeID' => Auth::id(), 'CustomerID' => 'id']) 
        ->get(); 

$query2 =  DB::table('Customer')
        ->select('username')
        ->join('Employee', 'Customer.id', '=', 'Employee.CustomerID')             
        ->where(['EmployeeID' => Auth::id(), 'CustomerID' => 'id']) 
        ->get(); 
然后,我将这些值返回到我的刀片文件,如下所示

 return view ('pages.dashboard')
 ->with('query1',$query1)
然后我在我的刀片文件中使用php缩进来返回用户数据

 @foreach ($query1 as $Userfound)
 {{ $Userfound->username}}</p> 

@endforeach
@foreach($query1作为$Userfound)
{{$Userfound->username}}

@endforeach
实际查询需要简单的英语

所以我需要选择一个客户,其中CustomerID==id


注意:id来自customers表,CustomerID存储在Employees表中。

您可以使用Laravel创建模型,例如:

Employee.php

public function customers()
{
    return $this->hasMany('App\Customer');
}
Customer.php

public function employee()
{
    return $this->belongsTo('App\Employee');
}
您可以这样访问:

$customer = Customer::where('id',Auth::user()->id)->firstOrFail();

以及查看员工的客户:

$employee->customers()->get();
或查看
$customer
雇主的其他客户:

$customer->employee()->customers()->get();

那么,您希望通过查询实现什么呢?预期的结果是什么?您可以尝试使用laravel关系,但为此,您需要为表创建模型文件。如需更多参考,您可以查看下面的链接我可以自己测试结果,我将$query或变量返回到我的刀片文件@主持人。我将更新我的帖子,向您展示“我希望其他客户看到其他客户项目”是什么意思?我有点像一个以下系统,我希望其他客户看到其他客户的详细信息。@party ring使用关系时,您需要使用
->get()
来完成雄辩的生成器,或者使用
->customer
(不带
()
),将按预期返回所有内容。@这是我收到的错误消息SQLSTATE[42S02]:找不到基表或视图:1146表“musicdatabase.follower\u user”不存在(SQL:选择
follower
*,
follower\u user
作为
pivor\u user\u id
follower\u user
follower\u id
作为
follower\u id
follower
内部连接wer_idwhere
follower_user
user_id
=1)我将给出一个示例,说明我希望在我的帖子中实际输出的内容
$customer->employee()->customers()->get();