Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mpk123's avatar

store function not recognizing field

Very new and I can't figure out a very basic thing. How would I add the value of Auth::user()->id to a user_id field when it's not passed through the form?

 public function store()
    {
        Auth::user()->sfa->student_meals()->create(
            Request::validate([
                'date_served' => ['required'],
                'meal_type_id' => ['nullable', Rule::exists('meal_types', 'id')],
				'tally' => ['required'],
            ]),

        );

        return Redirect::route('studentMeals')->with('success', 'Meal added.');

0 likes
1 reply
SilenceBringer's avatar

@mkainaroi

create(
	array_merge(
        Request::validate([
            'date_served' => ['required'],
            'meal_type_id' => ['nullable', Rule::exists('meal_types', 'id')],
			'tally' => ['required'],
        ]),
		['user_id' => auth()->id()]
	)
)

Please or to participate in this conversation.