这是我的设想。用户有三种类型(角色)。客户,服务提供者,行政。
客户添加新订单 服务提供者-执行增加的订单 管理员-确保客户端添加的订单是正确的,找到服务提供者并将其交给服务提供商。
当客户添加新订单时,客户端和管理员之间必须有招标系统。添加时的订单价格可能是50美元。客户可以出价40元,然后管理员可以同意此或出价另一个价格。然后,客户可以选择接受或尝试另一个价格,等等。这就是主意。当价格谈判后,同样的过程必须在服务提供商和管理之间进行。
因此,我需要一个表,在那里我存储客户和管理,管理和服务提供商之间的投标,我还需要存储投标历史,而不是最新的出价。
用户表
+----+--------+---------------------------------+-------------+
| id | name | userable_type | userable_id |
+----+--------+---------------------------------+-------------+
| 1 | George | App\Http\Models\Client | 2 |
| 2 | John | App\Http\Models\ServiceProvider | 3 |
| 3 | Ben | App\Http\Models\Admin | 4 |
+----+--------+---------------------------------+-------------+Client表/Admin表/服务提供程序表现在看起来都一样
+----+--+
| id | |
+----+--+
| 1 | |
| 2 | |
| 3 | |
+----+--+现在,如果我想做投标表,我不想为服务提供商,管理和客户分别制作新的表。我宁愿让它成为唯一的。
让我们以这个例子为例:
+----+----------+---------+-------+
| id | order_id | user_id | price |
+----+----------+---------+-------+
| 1 | 2 | 1 | 50 |
| 1 | 2 | 1 | 60 |
| 1 | 2 | 3 | 70 |
+----+----------+---------+-------+现在我如何知道哪个出价是管理,哪个是客户,哪个是服务提供商?基本上,我想要的是得到所有的客户出价和行政出价的order_id 2,也是供应商出价和order_id 2的管理出价。我尽力了,但还是搞不清楚。也许你可以建议我使用更好的版本。
发布于 2019-02-21 13:07:00
你可以通过加入得到你的出价
$bids = DB::table('bids')
->join('users', 'users.id', '=', 'bids.user_id')
->select('bids.*', 'users.userable_type')
->where('bids.order_id',$order_id)
->whereIn('users.userable_type', ['App\Http\Models\Client','App\Http\Models\Admin'])
->get();发布于 2019-02-21 12:45:43
我不确定你是否想要单独的名单或得到所有的出价,然后检查,所以我做了两个。它们都使用您提供的示例表。
$bids = Bid::where('order_id', 2)->get(); //Get all bids where order_id=2
foreach($bids as $bid){ //Loop through fetched bids
if($bid->userable_type=="App\Http\Models\Client"){ //If user is 'Client'
//User is client
}
}
$bids = Bid::where('order_id', 2)->where('userable_type', "App\Http\Models\Client")->get(); //List with only bids from Clients让我知道你的想法。
https://stackoverflow.com/questions/54806929
复制相似问题