I'm coming from an ASP.net MVC5 background so I am used to being able to do something like the following.
[HttpPost]
public JsonResult UpdateStuff(SomeObject aThing, List<AnotherObject> listOfThings)
{
// Do stuff
}
I would tend to post to the above using AngularJS. Effectively the end point is casting the passed JSON data for me and I then do my validation/creating/updating.
It's handy because I can pass various objects in various structures (e.g. single objects or arrays/lists) and the code is nice and readable because you can see the expected input.
Is similar possible in Laravel 5.6?
Currently I'm doing the following (not sure if it's right and/or recommended)...
public function something() {
$jsonAThing = request('aThing');
$jsonListOfThings = request('listOfThings');
$aThing = new SomeObject();
$aThing->property = $jsonAThing['property'];
$aThing->anotherProperty = $jsonAThing['anotherProperty'];
// Some other thing processing
foreach($jsonListOfThings as $listItem){
$anotherThing = new AnotherThing();
$anothingThing->property = $listItem['property'];
// etc etc
}
}
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire