Store top categories from an API request into the database
I have an API request like this:
$postInformation = (new postInformation)->handle(['field' => 'slug', 'value' => $this->slug]);
A dump of this response, besides other things, show some categories that are coming from this API request. And they are on this format where there is a 'categories' key with the top categories:
I created a table 'post_top_categories' and a model PostTopCategory and I want to get the top categories from the API response above and store them into the 'post_top_categories' table. But I'm not understanding how to properly achieve this. Do you know how it can be achieved it? Thanks
@SilenceBringer Thanks, I already tried different ways of fetch the data from that structure and transform them in a way to store them in the database but always getting issues. Also with your example its showing :
@SilenceBringer Also do you know how to create an index so that is possible to have only one record with the same combination of post_id and category_name?
The migration is like this at the moment:
public function up()
{
Schema::create('post_categories', function (Blueprint $table) {
$table->id();
$table->foreignId('post_id')
->nullable()
->constrained('posts');
$table->unsignedSmallInteger('category_id')->nullable();
$table->string('category_name')->nullable();
}