mleontenko
6 months ago
638
3
Laravel

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;
$post->save();

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

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.