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

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 8 years ago.

Sign in to participate in this thread!


Your banner here too?

4unkur 4unkur Joined 15 Aug 2015


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

Your logo here?

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

© 2024 - All rights reserved.