Start networking and exchanging professional insights

Register now or log in to join your professional community.

Follow

How to use Insert Statement in Laravel?

user-image
Question added by Julfkar Moh Umar , Sr. Software Engineer , Aakash Edutech Private Limited
Date Posted: 2017/09/28
Murtaza Khokhawala
by Murtaza Khokhawala , I.T. Head of Department , Royal Group of Companies

Create an Instance of Model Class then Assign the values to the respected fields and the save

                $data = new Staff();                 $data->sfullname = $request['fullname'];                 $data->susername = $request['username'];                 $data->spassword = openssl_encrypt($request['password'], $ciphering, $encryption_key, $options, $encryption_iv);                 $data->scontact = $request['phone'];                 $data->semail = $request['email'];                 $data->sdoj = $request['doj'];                 $data->sdob = $request['dob'];                 $data->sdesignation = $request['designation'];                 $data->scompany = $request['companyname'];                 $data->sreporting = $request['reporting'];                 $data->ssecurity = $request['security'] ? '7' : '2';                 $data->smultiple = $request['multiplesite'] ? '1' : '0';                 $data->active = "1";                 $data->sgrouplevel = "0";                 $data->sgroupowner = "Self";                 $data->save();

 

Lanka Senanayaka
by Lanka Senanayaka , Senior Software Engineer , Ensiz former Fullstack Labs

For laravel 5

First define fillables in model

 

App\\ModelName::create(["field1"=>"value1","field2"=>"value2"]);

AHMAD ATHAMNEH
by AHMAD ATHAMNEH , Senior Back-end Engineer & Angular developer & DBA , Edzance

\\DB::table('users')->insert([

'id'=>1,

'name'=>'Web technology',

'city_id'=>10

]);

Waleed Ikhlaq
by Waleed Ikhlaq , Full Stack Web Developer , iWebMonster

two ways to insert statement or data in laravel:

 

1- Use create method

2- Use save method

 

For more you can study on it in laravel docs.

Arun Raj G S
by Arun Raj G S , CRM Administrator & PHP Developer , ME Group

Use Save() , If you want to update also

Julfkar  Moh Umar
by Julfkar Moh Umar , Sr. Software Engineer , Aakash Edutech Private Limited

DB::insert('insert into users (id, name, city_id) values (?, ?)', [1, 'Web technology',10]);

More Questions Like This