Laravel 多表连接:如何连接两个包含所有列的表,但一列除外

Posted

技术标签:

【中文标题】Laravel 多表连接:如何连接两个包含所有列的表,但一列除外【英文标题】:Laravel multiple table join: how to join two table with all columns and except one column 【发布时间】:2020-08-14 13:33:57 【问题描述】:

我有两个超过 100 列的表。 我想加入两个表,除了一列之外的所有列。


$agents = agent::join('cities','cities.id','city_id)->select('agents.*','cities.name as city')->get();


。 . 现在加入所有代理表,但我不想要 egents.user_id

【问题讨论】:

你能解释一下你想要什么 我有两个表并加入。但不想在我的联接中从一张表中提取一列 表有一个 user_id,我不想在 join 中发送 user-id 为什么要标记 mysql、SQL Server 和 SQLite?它们都是不同的 RDBMS? 是的,但都是关于数据库的 【参考方案1】:

在模型中使用 make hidden 数组很好,使用 makeHidden 函数将您的列从序列化中隐藏起来:

$agents = agents::where('your query')->get();
$agents ->makeHidden(['user_id']);
return response()->json($agents);

希望你喜欢

【讨论】:

【参考方案2】:

我可以知道表格的内容吗? 比如例子?

如果是 laravel,我会喜欢:

DB::table('users')
->select('users.*','profiles.photo', 'profiles.one_by_one')
->join('profiles','profiles.id','=','users.id')
->where(['something' => 'something', 'otherThing' => 'otherThing'])
->get();

您可以取消选择您不想加入的列。

你可以试试这个

DB::table('users')
->select('users.*','profiles.*')

->except('users.id')
OR
->exclude('users.id')

->join('profiles','profiles.id','=','users.id')
->where(['something' => 'something', 'otherThing' => 'otherThing'])
->get();

请测试一下

在您的模型 laravel 中,将此添加到您不想看到的模型中的某个列。

protected $hidden = array('column_you_dont_want_to_see');

【讨论】:

从这个例子中我想要除了(例如)电话列之外的所有用户表 如何将它添加到您的模型中? ``` 受保护的 $hidden = array('column_you_dont_want_to_see'); ``` 在 laravel 中,如果我使用这个模型,我们在任何地方都看不到这个

以上是关于Laravel 多表连接:如何连接两个包含所有列的表,但一列除外的主要内容,如果未能解决你的问题,请参考以下文章

oracle 多表连接查询

mysql的多表连接

多表合并且去掉重复项

数据库原理-数据多表查询

Oracle 多表查询

Shell-Script用于连接目录中具有两个键列的所有CSV文件