Consuming Api Post Request.
Hi everyone I need some assistance if you are available. I have a post request coming into my api and i need some help consuming it. All the controllers, models and relationships are working correctly. But I have never taken a post and converted it for 5 tables before.
I can add new feature record with DB::table('features')->updateOrInsert(); But I have no idea how to update or insert the tags or category to thier tables and attach or sync the pivot tables. Any help with this would be very appreciated.
Incoming post is added to the following:
3 Tables
- features
- categories
- tags
2 Pivot Tables
- feature_categoy
- feature_tag
Incoming post data fields (json):
published --|
code |
name features table
description --|
category - categories table
tags - tags table
I am currently using the following for the main table which is working great.
public function storePost(StoreFeatureRequest $request)
{
$data = $request->all();
This is how I identify if record exists but is a work in progress. if there is a better way please share it.
if (Feature::where('code', $request->input('code'))->exists())
{
$feature_id = Feature::where('code', $request->input('code'))->pluck('id');
$feature = Feature::where('id', $feature_id)->get();
}
try {
I have tried new Feature::updateOrCreate({}); but have been unable to get it to work.
DB::table('features')
->updateOrInsert(
I have been able to get this to work but not sure how to update the
['code' => $request->input('code')],
[
'published' => 'Yes',
'code' => $request->input('code'),
'name' => $request->input('name'),
'description' => $request->input('description'),
'updated_at' => now()
Need assistance here. how can I determine if this is update or insert so I can add created_at
// 'created_at' => is_null($feature->created_at) ? $feature->created_at : now()
]
);
return response()->json([
"message" => "recorded successfully",
"content" => $data
], 200);
}catch (HttpException $exception) {
$response = $exception->getMessage();
$status = $exception->getStatusCode();
}
}
Please or to participate in this conversation.