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

SGUserFace's avatar

add userID to create

How can I add userID field in the create?

$product = Product::create(request(['name', 'price']));

0 likes
5 replies
pardeepkumar's avatar

I suggest you to try this

$user = User::create(['name'=>'Pardeep']);
$user->id
SGUserFace's avatar

I want to add $user->id to product table.

the problem is the $user->id doesn't exist in request

NOMGUY's avatar

How about this one:

$project= Project::create([
            'name' => request('name'),
            'price' => request('price'),
            'user_id' => auth()->id()
]);

try it

Snapey's avatar
Snapey
Best Answer
Level 122

If you have setup the relation products in the user model, you dont need to set the key at all, Eloquent will do it for you;

 $product = $request->user()->products()
    ->create(request(['name', 'price']));

Please or to participate in this conversation.