How to hash last insert id and set into request then save into db in laravel.?

findorfail laravel
laravel query builder
laravel db::query
laravel subquery
laravel eloquent
laravel collection
laravel model attributes
laravel raw query with parameters

Below my request is perfectly fine. I just wonder is it best way to use save function two time?.its really bothering me I am looking for a best way to hashed last insert id and set into visitor_id to save into db. Remember before hashing visitor_id i am sending $request->visitor_id NULL.

Here is my request:

$visit->funnel_id = $request->funnel_id;

$visit->page_id = $request->page_id;
$visit->traffic_source_id = $request->traffic_source_id;
$visit->visitor_id = $request->visitor_id;
$visit->save();

$hashid = new Hashid();
$visit->visitor_id = $hashid->encode($visit->id);
$visit->save();



You can add 'nextId' function and include Illuminate\Support\Facades\DB facade in your Visitor model.

public function nextId(){
   $statement = DB::select("SHOW TABLE STATUS LIKE '".$this->getTable()."'");
   return $statement[0]->Auto_increment;
}

You can implement in your code like this :

$visit->funnel_id = $request->funnel_id;

$visit->page_id = $request->page_id;
$visit->traffic_source_id = $request->traffic_source_id;
$hashid = new Hashid();
$visit->visitor_id = $hashid->encode($visit->nextId());
$visit->save();

Database: Query Builder - Laravel, Below my request is perfectly fine I just wonder is it best way to use save function How to hash last insert id and set into request then save into db in laravel. getid () - This method has received ajax request for get the last inserted id. Below you can find 4 different method in Laravel by which we can get the value of last inserted id. Method 1 - insertGetId () - This method has use Laravel Database class for get the value of last inserted id.


 $visit->funnel_id = $request->funnel_id;

 $visit->page_id = $request->page_id; 
 $visit->traffic_source_id = $request->traffic_source_id; 
 $visit->visitor_id = $request->visitor_id; 
 $visit->save();

 $hashid = new Hashid(); 
 $hashid->visitor_id = $visit->id;
 $hashid->save();

Eloquent: Getting Started - Laravel, You may use the table method on the DB facade to begin a query. the given table, allowing you to chain more constraints onto the query and then finally get the To retrieve a single row by its id column value, use the find method: also use the following methods to insert a raw expression into various parts of your query. To execute an insert statement, you may use the insert method on the DB facade. Like select , this method takes the raw SQL query as its first argument and bindings as its second argument: DB::insert('insert into users (id, name) values (?, ?)', [1, 'Dayle']);


Try

$hashid = new Hashid();
$visit->visitor_id = $hashid->\Hash::make($visit->id);
$visit->save();

Or

$hashid = new Hashid();
$visit->visitor_id = $hashid->$visit->id;
$visit->save();

Laravel, get last insert id using Eloquent, All Eloquent models extend Illuminate\Database\Eloquent\Model class. Eloquent will also assume that each table has a primary key column named id . may also add constraints to queries, and then use the get method to retrieve the results: When we call the save method, a record will be inserted into the database. 2 How can I convert String into array of characters in java Oct 3 '17 2 How to hash last insert id and set into request then save into db in laravel.? Dec 14 '19


php Laravel, get last insert id using Eloquent?, And I want to return the last ID inserted but I don't know how to get it. where he just sends the Request straight into the database without Firstly create an object, Then set attributes value for that object, Then save the object record, and Hash::make(Input::get('password')), )); $lastInsertedId= $user->id;� A little explanation on how laravel does this behind the scene and why this is dangerous for large data set, get() method will fetch everything from your_table and generate a Collection and last() method will get the last item from that collection. So you will already have all of the data from the table loaded on your memory(bad).


How to get last inserted id in laravel PHP Framework, Here is how we can get last inserted id in Laravel 4 DB::getPdo()->lastInsertId (); use the insertGetId method to insert a record and then retrieve the ID: use App\Http\Requests\UserStoreRequest; class UserController extends Controller $Tut->save(); $LastInsertId = $Tut->id; return Response::json(array('success'� The following examples are equal to the examples from the previous page (PHP Insert Data Into MySQL), except that we have added one single line of code to retrieve the ID of the last inserted record. We also echo the last inserted ID:


Laravel Mass Insert, In PHP, you use mysqli_insert_id to get last inserted record id. In laravel, when you go with DB query builder then you can use insertGetId() that You can simply use create method to insert a new record into. import excel into DB and export data into csv and excel � How to set custom header authorization in PHP curl? The created_at and updated_at timestamps will automatically be set when the save method is called, so there is no need to set them manually. Updates. The save method may also be used to update models that already exist in the database. To update a model, you should retrieve it, set any attributes you wish to update, and then call the save method.