gpt4 book ai didi

php - 加入 Laravel 5.1

转载 作者:行者123 更新时间:2023-11-29 03:27:39 25 4
gpt4 key购买 nike

我是 Laravel 的新手,我仍在使用这个框架开发我的项目。在我的项目中,我使用 SQL JOIN 但我不知道如何检索/获取连接表上的数据。有人可以帮我解决这个问题吗?非常感谢你的帮助。谢谢!这是一个简单的例子!

$users = DB::table('users')
->join('contacts', 'users.id', '=', 'contacts.user_id')
->join('orders', 'users.id', '=', 'orders.user_id')
->select('users.*', 'contacts.phone', 'orders.price')
->get();

最佳答案

$users = \DB::table('users')
->join('contacts', 'contacts.user_id', '=', 'users.id')
->join('orders', 'orders.user_id', '=', 'users.id')
->select('users.id as id', 'contacts.phone as phone', 'orders.price as price')
//Where condition here if exist
->get();

$users 现在将拥有值数组。如有必要,将其循环到数组中。例如:

forearch($users as $user)
{ $user->id;
$user->phone;
}

就像那样。

关于php - 加入 Laravel 5.1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34024664/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com