php - Laravel How to insert data to 2 tables which has foreign key to each other
Get the solution ↓↓↓Solution:
you can do it this way
$imagenes=[];
$stock= new Stocks();
$data = $stock->storeColors()->attach($request->color_name, array('icon' => request('icon')));
$color = Color::where('stock_id',$data->id)->first();
if ($request->hasFile('imagen')) {
foreach ($request->imagen as $image) {
$imagenes[]=
new Images([
'url' => $image->url
]);
$request->file('imagen')->move('imagenes/','imagenName.jpg');
}
}
$color ->images()->saveMany($imagenes);
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: property [id] does not exist on this collection instance.
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.