6 months ago

Logging ID while storing new record to database

Posted 6 months ago by mleontenko

I have created logs table and Log model. When I insert something to database (e.g. a new post), I want to log some basic info.

For example: User creates a post. In Posts Controller:

// Create Post
$post = new Post;
$post->title = $request->input('title');
$post->body = $request->input('body');
$post->user_id = auth()->user()->id;
$post->cover_image = $fileNameToStore;

// Log the creation
$log = new Log;
$log->user_id= Auth::id();
$log->post_ id= ???;
$log->date = time();

return redirect('/posts')->with('success', 'Post Created');

The problem is that I don't have Post ID at this stage, so I can't log it. How can I solve this?

Please sign in or create an account to participate in this conversation.