Php 如何在laravel 8中为创建人和更新人自动插入数据字段

Php 如何在laravel 8中为创建人和更新人自动插入数据字段,php,mysql,laravel,Php,Mysql,Laravel,我是新手,需要帮助 我的帖子数据库中有创建人和更新人字段,添加新帖子后如何自动填充这些字段 这是我的数据库: Schema::create('posts', function (Blueprint $table) { $table->id(); $table->string('post_title', 60); $table->string('post_slug')->unique(); $table->

我是新手,需要帮助

我的帖子
数据库中有
创建人
更新人
字段,添加新帖子后如何自动填充这些字段

这是我的
数据库

Schema::create('posts', function (Blueprint $table) {
        $table->id();
        $table->string('post_title', 60);
        $table->string('post_slug')->unique();
        $table->integer('post_seq');
        $table->string('post_thumbnail');
        $table->string('post_excerpt', 500);
        $table->text('post_description');
        $table->string('post_meta_title');
        $table->string('post_meta_description');
        $table->string('post_meta_keyword');
        $table->boolean('is_active')->default(false);
        $table->timestamps();
        $table->string('created_by')->nullable();
        $table->string('updated_by')->nullable();
    });
这是我的
PostController

public function store(Request $request)
{
    $request->validate([
        'post_title' => 'required|string|max:60',
        'post_slug' => 'required|string|unique:posts,post_slug',
        'post_thumbnail' => 'required',
        'post_excerpt' => 'required|string|max:500',
        'post_seq' => 'required',
        'post_description' => 'required|string',
        'post_meta_title' => 'required|string|max:60',
        'post_meta_description' => 'required|string|max:250',
        'post_meta_keyword' => 'required|string',
        'is_active' => 'required'
    ]);

    Post::create([
        'post_title' => $request->post_title,
        'post_slug' => $request->post_slug,
        'post_seq' => $request->post_seq,
        'post_thumbnail' => parse_url($request->post_thumbnail)['path'],
        'post_excerpt' => $request->post_excerpt,
        'post_description' => $request->post_description,
        'post_meta_title' => $request->post_meta_title,
        'post_meta_description' => $request->post_meta_description,
        'post_meta_keyword' => $request->post_meta_keyword,
        'created_by' => User::where('name')->get(),
        'is_active' => $request->boolean('is_active'),
        'user_id' => $request->user_id
    ]);
    Alert::success('Add Post', 'Added Post Success');
    // dd($request->all());
    return redirect()->route('posts.index');
}
这是我的
post
模型

class Post extends Model
{
    

use HasFactory;

protected $table = 'posts';    



protected $fillable = ['post_title', 'post_slug', 'post_thumbnail', 'post_seq', 'post_excerpt', 'post_description', 'post_meta_title', 'post_meta_description', 'post_meta_keyword', 'is-active', 'created_by'];

    public function scopeSearch($query, $title)
    {
        return $query->where('post_title', 'LIKE', "%{$title}%");
    }
}
请,我需要帮助。

试试这个:

$model = new Model();
$model->created_at = Carbon::now();
$model->save(['timestamps' => false]);

您可以使用保存或创建事件,但要注意批量插入和批量更新。好的,谢谢。谢谢。这很有用。。