@
@
@Twitt2018-05-26 22:46:40
Laravel
@Twitt, 2018-05-26 22:46:40

Am I populating the model correctly with values?

There is a form that the user fills out. on the back there is a request to the action, and I have this:

Descriptor::create(['user_id' => Auth::id(), 'message' => $all['message']]);

As for user_id, the question is, do I correctly enter the ID of the logged in user? I heard that it is better not to cling to such a methodology, and the functionality should not supposedly depend on Auth::id (), just how to write a unit test for this in the future?
I also assigned the user_id column to the fillable array in the model. Did you do it right?
PS It's not entirely clear how to write unit tests for the Auth::id() case

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
Yan-s, 2018-05-26
@Yan-s

You can make a method that accepts a user as input, and then executes

Descriptor::create(['user_id' => $user->id, 'message' => $all['message']]);
then this method will be more universal and it will be more convenient to test.
Although it is not clear what difficulties getting an authorized user causes during testing. In tests, you can both authorize the user and use the special method https://laravel.com/docs/5.6/http-tests#session-an...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question