Create a Insert… Select statement in Laravel

前端 未结 6 1192
迷失自我
迷失自我 2020-12-11 14:41

I\'m needing to convert this query for Laravel and I\'m having issues trying to create an Insert... Select statement using Laravel\'s Eloquont ORM, or Queries. I\'m not sure

6条回答
  •  孤街浪徒
    2020-12-11 15:17

    First, you'll need to create a model for Demand, so then you can use:

    $demand = new Demand;
    $demand->Login = $login;
    $demand->Name = $name;
    [...]
    $demand->save(); // <~ this is your "insert" statement
    $id = $demand->id;
    

    Then for your select statement you can do something similar to these options:

    $demand = Demand::find($id); // will be similar to: SELECT * FROM Demand WHERE `id` = '$id';
    $demand = Demand::where('id', $id)->get(); //same as above
    $demand = Demand::where('id', $id)->get(array('Login', 'Name')); // will be similar to: SELECT `Login`, `Name` FROM Demand WHERE `id` = '$id';
    

    There is a lot more information in the manual here and here

提交回复
热议问题