how to parse field with same name using SAXParser - saxparser

i have following xml file:
<package>
<reader>
<id>r1007</id>
<name>Robert</name>
<email>robert#yahoo.com</email>
</reader>
<writer>
<id>w1920</id>
<name>Young</name>
<email>young#hotmail.com</email>
</writer>
</package>
both 'reader' and 'writer' have fields as 'id', 'name' and 'email', how can i differentiate them using SAXParser since SAXParser only tell 'startElement' and 'endElement'. thanks.

You have to keep a stack of the parents of the element you are in.

Related

Unpacking a nested XML containing a list with Laravel's orchestra parser

I want to parse a list of packages that contains a list of pictures. However I am able to get the first element but not the full array. The parser I'm using is Laravel's Orchestra.
The XML to be unpacked:
<Packages>
<Package>
<Pictures>
<P>20205520574081..jpg</P>
<P>20205520574701.jpg</P>
<P>20205520575172.jpg</P>
<P>20205520575483.jpg</P>
<P>20205520575955.jpg</P>
<P>20205520576426.jpg</P>
<P>20205520576897.jpg</P>
<P>20205520577368.jpg</P>
<P>20205520577989.jpg</P>
</Pictures>
</Package>
<Package>
<Pictures>
<P>20205520574081..jpg</P>
<P>20205520574701.jpg</P>
<P>20205520575172.jpg</P>
<P>20205520575483.jpg</P>
<P>20205520575955.jpg</P>
<P>20205520576426.jpg</P>
<P>20205520576897.jpg</P>
<P>20205520577368.jpg</P>
<P>20205520577989.jpg</P>
</Pictures>
</Package>
My extraction code:
$OfferSummary = $xml->parse([
'Images' => ['uses' => 'Packages.Package.Picture[P]'],
]);
My result:
["Images"]=>
array(1) {
[0]=>
array(1) {
["P"]=>
string(19) "20205520574081..jpg"
}
....
}
Please note that only the first element of the pictures tag is returned, and I need the entire array of elements.
Thanks!
I had a similar problem. My XML had a list in which every sub-node had a name attribute. In my case I found the answer here: https://github.com/orchestral/parser/blob/master/tests/Feature/DocumentTest.php#L52
In my case the solution was:
$parser = $xml->parse([
'products' => ['uses' => 'products.product[attributes.attribute_group.value(::name=#)]'],
]);

Using groupBy in Laravel resource

I want to pass a collection with a groupBy in laravel resource, But the problem is when i use that with collection method I can not modify the json and it throws a error:
PostResource:
public function toArray($request)
{
return [
'comments' => CommentResource::collection($this->comments->groupBy('star')),
];
}
Property [star] does not exist on this collection
But as soon as I remove the groupBy method from the collection it works. So how to get and modify the resource when it is grouped by with a specific key ?
"You could possibly group the collection once it has been created."
CommentResource::collection($this->comments)->collection->groupBy('star')
credits goes to devcircus with his answer on github
i use it and work successfully
CommentResource::collection($this->comments)->collection->groupBy('star')

Call to undefined method Illuminate\Database\Query\Builder::withTrashed() with form validation

What's wrong with my validation code ?
request()->validate([
'name'=>['required', Rule::unique('users', 'name')->where(function($query) {
$query->withTrashed();
})],
'email'=>['required', Rule::unique('users')->where(function($query) {
$query->withTrashed();
})],
'password'=>'required|min:6|max:20|confirmed',
'g-recaptcha-response' => 'required|captcha',
]);
In my User model I declared use Illuminate\Database\Eloquent\SoftDeletes; and use SoftDeletes trait in the class.
But when I want to validate the data I have this error :
Call to undefined method
Illuminate\Database\Query\Builder::withTrashed()
Thank's for help.
The unique validation rule does work on a table and not a model. The rule itself has no knowledge about the User model, it only knows about the users database table.
For this reason, the validation engine does use an Illuminate\Database\Query\Builder instance and not an Illuminate\Database\Eloquent\Builder instance. Unfortunately, the normal query builder cannot work with scopes which prevents you from using withTrashed().
This isn't really an issue though because the global soft deleteable filter isn't even applied when using the unique validation rule. In other words, you don't have to do anything - deleted elements will already be considered anyway.

Unique record but only for a given parent in Laravel 5.5

I have 2 database models, both with a field for URL so you can access them. In the frontend I'd do something like this to access it:
/{{ $parent->url }}/{{ $child->url }}
Validating that the parent URL is easy, I just ensure it's required and unique for that table. But the child one is a bit tricker. I need the URL to be unique but only if the records share the same parent. Currently my validation rule for creating a new record is:
'url' => 'required|alpha_dash|unique:child_table,url',
and updated with:
'url' => 'required|alpha_dash|unique:child_table,url,' . $id,
But that means EVERY URL must be unique and I'd rather not enforce that rule. I know the parent record ID so is there a way to say URL must be unique but only for records with this parent ID?
To be absolutely clear, this isn't allowed:
/parent1/child1
/parent1/child1
But this is:
/parent1/child1
/parent2/child1
Something like this can help you achieve that:
Rule::unique('child_table', 'url')->where(function ($query) use ($parentId) {
return $query->where('parrent_id', $parentId);
})
It ended up ever so slightly more complicated if anyone ever needs this:
'url' => ['required', 'alpha_dash', Rule::unique('child_table', 'url')->where(function($query) use ($parentId) {
return $query->where('parentId', '=', $parentId);
})],

Update Table Using Laravel Model

I've got a table for a sports team. The record shows the team selection and some other information. I want to update the record with the team selection. My model is thus:
class Selection extends Model {
protected $table = "selection";
protected $fillable = [
'loose',
'hooker',
'tight',
'secrow1',
'secrow2',
'blindflank',
'openflank',
'eight',
'scrum',
'fly',
'leftwing',
'rightwing',
'fullback',
'sub1',
'sub2',
'sub3',
'sub4',
'sub5'
];
}
So I have a form which gives all the data for the positions and gives the id for the record in the DB. In my controller, I've got:
public function storeFirstTeam()
{
$input = Request::all();
Selection::update($input->id,$input);
return redirect('first-team');
}
But I get the following error:
Non-static method Illuminate\Database\Eloquent\Model::update() should not be called statically, assuming $this from incompatible context
Can anyone point out my silly error?
Please check the code below and this would solve your problem:
Selection::whereId($id)->update($request->all());
The error message tells you everything you know: you’re trying to call a method statically (using the double colons) that isn’t meant to be.
The update() method is meant to be called on a model instance, so first you need to retrieve one:
$selection = Selection::find($id);
You can then can the update() method on that:
$selection->update($request->all());
You should write it like given example below:
Selection::where('id', $input['id'])->update($input);
// Or use this using dynamic where
Selection::whereId($input['id'])->update($input);
Alternatively, you may write it like this as well:
Selection::find($input['id'])->fill($input)->save();
You can also simply update the fields manually:
Selection::whereId($id)->update($request->all());
it is possible to update with primary key but in my case I dont have id field in the detail table. To do it just run a query like this:
DB::table("shop_menu_detail")
->where(['name' => 'old name', 'language' => 'english'])
->update(['name' => 'new name']);
where is used as it is.

Resources