我可以在一个新表中插入多行,其中使用eloquent查询中不存在这些行

时间:2015-03-30 07:46:44

标签: php sql laravel laravel-4 eloquent

我有以下查询

   $query = Modelname::selectRaw('table_name')
        ->join('table_nameTwo', 'table_name.userTwo_id', '=', 'table_nameTwo.user_id')
       ->where('table_name.user_id', '=', 8)
        ->select('table_name.user_id', 'table_name.userTwo_id', 'table_nameTwo.product')
        ->get();

表名

id|user_id|userTwo_id
1 |8      |7
2 |2      |6
3 |8      |6
4 |8      |9
5 |8      |2

table_nameTwo

id|user_id|product
1 |2      |book
2 |2      |mouse
3 |7      |computer
4 |7      |shoes

上面的查询从上面的两个表中生成以下数组:

[{"user_id":8,"userTwo_id":2,"product":"book"},{"user_id":8,"userTwo_id":2,"product":"mouse"},{"user_id":8,"userTwo_id":7,"product":"computer"},{"user_id":8,"userTwo_id":7,"product":"shoes"}]

table_nameThree

   id|user_id|userTwo_id|product
   1 |8      |7         |shoes
   2 |8      |2         |mouse

如何使用eloquent将结果数组($ query)插入table_nameThree而不重复行? (Laravel 4.2)

0 个答案:

没有答案