I am working on an API that is pulling data from an xml file then decode it into json, how would the store method look like in the controller?
I have tried storing the json into my DB with code below, but i cant get to a point were the json gets saved into the DB
public function store(Request $request)
{
$fileContents = file_get_contents('/path/to/my.xml');
$simpleXml = simplexml_load_string($fileContents);
$json = json_encode($simpleXml, true);
Vacancy::create($json);
}
This is my Vacancy Model
class Vacancy extends Model
{
protected $fillable = ['requisition', 'position_title', 'experience'];
I understand that the index method would look like this
public function index()
{
$vacancies = Vacancy::all();
retu $this->respond([
'data' => $this->vacancyTransformer->transformCollection($vacancies->all())
]);
}
This is my VacancyTransformer class
class VacancyTransformer extends Transformer
{
public function transform($vacancy)
{
retu [
'requisition' => $vacancy['requisition'],
'position' => $vacancy['position_title'],
'workExperience' => $vacancy['experience'],
];
}
}
This is my Transformer class
abstract class Transformer {
public function transformCollection(array $items)
{
retu array_map([$this, 'transform'], $items);
}
public abstract function transform($item);
}
