Support the ongoing development of Laravel.io →
Input Eloquent Installation
Last updated 2 years ago.
0

You need not to use mutator to set the Auth:id() for new model instance. Checkout the Eloquent event to set Auth::id() as user_id during the creation of new model instance.

  • For Post model code sample will

    <?php namespace App\Providers; use App\User; use Illuminate\Support\ServiceProvider; class AppServiceProvider extends ServiceProvider { /** * Bootstrap any application services. * * @return void */ public function boot() { Post::creating(function ($post) { $post->user_id = Auth:id(); }); } /** * Register the service provider. * * @return void */ public function register() { // } }

Don't Forget to register the service provider

Last updated 9 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

4unkur 4unkur Joined 15 Aug 2015

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.