So, I want to store some products to the product
table. I managed that. But now I want to attach a user id
to each product, so it can correspond to a different user.
As for now, I have tried this, but it doesn't seem to work:
public function store(){
$product = Request::all();
$product = Product::create($product);
$user = User::whereId(Request::input('user'))->first();
$product->users()->attach($user);
return redirect()->route('products');
}
The relationship between users
and products
is this: a user_id
column in products
table is foreign key
and references to the id
on users
table