I have a problem with the Laravel 5 Validation, I post data with the Angular method $http.post. I have allready created a Request file called, CreateProjectRequest.
The posted data looks like this.
Array(
[0] => Array
(
[project_id] =>
[title] => test
[tasks] => Array
(
[0] => Array
(
[vat_id] => 1
[title] => 'test
[quantity] =>
[rate] =>
[costs] =>
)
)
)
[1] => Array
(
[project_id] =>
[title] => test2
[tasks] => Array
(
[0] => Array
(
[vat_id] => 1
[title] => test
[quantity] =>
[rate] =>
[costs] =>
)
)
)
)
I need to validate title and tasks, how can I reach that?
I tried a few things but the I get each time a error. For example title is required while title was filled in.
// Empty rules array
$rules = [];
foreach ($this->request->all() as $keyGroup => $request)
{
$rules['title'] = 'required';
// Loop throug tasks
foreach ($request['tasks'] as $key => $task)
{
$rules['quantity'] = 'required|numeric';
}
}
return $rules;
Related
It is my first time working with an external API. I have already fixed that it gives me the right output. In a controller I have the following code:
public function index() {
$api = new Wefact();
$parameters = [
];
$api_response = $api->sendRequest('product', 'list', $parameters);
print_r($api_response);
}
This shows me the array on the page. But I do not know how to use this array to get it into a foreach in the blade. The output of the print_r is as follow:
Array
(
[controller] => product
[action] => list
[status] => success
[date] => 2022-05-05T04:20:03+02:00
[totalresults] => 2
[currentresults] => 2
[offset] => 0
[products] => Array
(
[0] => Array
(
[Identifier] => 1
[ProductCode] => P0001
[ProductName] => SIM ONLY 5GB
[ProductKeyPhrase] => SIM ONLY 5GB
[ProductDescription] =>
[NumberSuffix] =>
[PriceExcl] => 25
[TaxCode] => V21
[TaxPercentage] => 21
[PricePeriod] => m
[Modified] => 2022-05-05 03:49:57
)
[1] => Array
(
[Identifier] => 2
[ProductCode] => P0002
[ProductName] => SIM ONLY 10GB
[ProductKeyPhrase] => SIM ONLY 10GB
[ProductDescription] =>
[NumberSuffix] =>
[PriceExcl] => 35
[TaxCode] => V21
[TaxPercentage] => 21
[PricePeriod] => m
[Modified] => 2022-05-05 04:03:47
)
)
)
As you can see there are two products. I want to have these in a datatable with an foreach.
Since it is my first time, I really do not know how to do this.
Anyone that can help me out?
If you want to access the array of products it would be like this
return view('your_view', [
'products' => $api_response['products']
]);
I have two Arrays 1st Array give me a role form Roles table and second array give me the data using this role_id key.
When i pass the array to view how can i get Role that is from first foreach().
This is my
SettingController.php
public function index()
{
$users = User::with('roles')->get()->all();
$access_names = AccessName::all();
$modules = Module::all();
$entries = Entry::all();
$general_settings = Setting::all()->first();
$module_permissions = ModulePermission::get()->toArray();
$roles = Role::get();
$num_roles = count($roles);
$data = 'roles';
foreach ($roles as $role){
foreach ($module_permissions as $permissions){
if($role['id'] == $permissions['role_id']){
$roles_data[$role['name']][] = $permissions;
}
}
}
return view('backend.setting.index', compact(['general_settings', 'roles', 'users', 'access_names', 'roles_data','modules','entries']));
}
My output of this code is like this
Array
(
[Admin] => Array
(
[1] => Array
(
[id] => 179
[user_id] => 1
[role_id] => 1
[access_name_id] => 2
[module_permission_id] => 1
[entry_type] => own
[status] => 0
[created_at] => 2019-08-22 04:45:14
[updated_at] => 2019-08-22 04:45:14
)
)
[User] => Array
(
[0] => Array
(
[id] => 206
[user_id] => 2
[role_id] => 2
[access_name_id] => 1
[module_permission_id] => 1
[entry_type] => own
[status] => 1
[created_at] => 2019-08-22 05:09:40
[updated_at] => 2019-08-22 05:09:40
)
)
)
If i can access data from this then very well other wise i make a new array like this from i can access this by a new array that is data so it is easy to check that
I also tried this but this is not working properly
foreach ($roles as $role){
foreach ($module_permissions as $permissions){
if($role['id'] == $permissions['role_id']){
$roles_data[$role['name']][] = $permissions;
$array['data'] = $roles_data;
}
}
}
But If output come like this that is also very good
Array
(
[data]=>array
(
[Admin] => Array
(
[0] => Array
(
[id] => 179
[user_id] => 1
[role_id] => 1
[access_name_id] => 2
[module_permission_id] => 1
[entry_type] => own
[status] => 0
[created_at] => 2019-08-22 04:45:14
[updated_at] => 2019-08-22 04:45:14
)
)
[User] => Array
(
[0] => Array
(
[id] => 206
[user_id] => 2
[role_id] => 2
[access_name_id] => 1
[module_permission_id] => 1
[entry_type] => own
[status] => 1
[created_at] => 2019-08-22 05:09:40
[updated_at] => 2019-08-22 05:09:40
)
)
)
)
In view i want to do like this.
view.blade.php
if($role->name == 'data['Admin']'){
//then do this
}
elseif($role->name == 'data['User']'){
// do this
}
else{
// do this
}
And i also want to this dynamic what can i do to make dynamic i not use 'admin' and 'user' because if i have many roles it is difficult to handle.
Thanks.................
After printing my $my_values['outer_group']['fieldset'] I'm getting the following output as per the data:
Array
(
[fieldset] => Array
(
[1] => Array
(
[title] => Dummy_Value1
[inner_group] => Array
(
[fieldset] => Array
(
[1] => Array
(
[id] => 11
[title] => Dummy_Value11
)
[2] => Array
(
[id] => 12
[title] => Dummy_Value12
)
[3] => Array
(
[id] => 13
[title] => Dummy_Value13
)
[actions] => Array
(
[add] => Add InnerGroup
[remove] => Remove InnerGroup
)
)
)
)
[2] => Array
(
[title] => Dummy_Value2
[inner_group] => Array
(
[fieldset] => Array
(
[1] => Array
(
[id] => 21
[title] => Dummy_Value21
)
[actions] => Array
(
[add] => Add InnerGroup
)
)
)
)
[actions] => Array
(
[add] => Add OuterGroup
[remove] => Remove OuterGroup
)
)
)
My requirement is to re-index the output data, hence I've performed the following code to re-index the same:
<?php
if (isset($my_values['outer_group']) && !empty($my_values['outer_group'])) {
$outer_types = $my_values['outer_group']['fieldset'];
$inner = [];
foreach ($outer_types as $outer_key => $outer_value) {
if (is_numeric($outer_key)) {
if (isset($outer_value['inner_group']['fieldset'])) {
foreach ($outer_value['inner_group']['fieldset'] as $k => $v) {
if (is_numeric($k)) {
$inner[] = [
'id' => $v['id'],
'title' => !empty($v['title']) ? $token->replace($v['title']) : NULL,
];
}
}
}
$my_values['outer'][$outer_key] = [
'title' => !empty($outer_value['title']) ? $token->replace($outer_value['title']) : NULL,
'inner' => $inner,
];
}
}
}
As per the output its getting re-indexed but with some errors in data. I'm getting trouble while populating the [inner] data, following is the output for the same:
Array
(
[0] => Array
(
[title] => Dummy_Value1
[inner] => Array
(
[0] => Array
(
[id] => 11
[title] => Dummy_Value11
)
[1] => Array
(
[id] => 12
[title] => Dummy_Value12
)
[2] => Array
(
[id] => 13
[title] => Dummy_Value13
)
)
)
[1] => Array
(
[title] => Dummy_Value2
[inner] => Array
(
[0] => Array
(
[id] => 11
[title] => Dummy_Value11
)
[1] => Array
(
[id] => 12
[title] => Dummy_Value12
)
[2] => Array
(
[id] => 13
[title] => Dummy_Value13
)
[3] => Array
(
[id] => 21
[title] => Dummy_Value21
)
)
)
)
Whereas, it should be:
Array
(
[0] => Array
(
[title] => Dummy_Value1
[inner] => Array
(
[0] => Array
(
[id] => 11
[title] => Dummy_Value11
)
[1] => Array
(
[id] => 12
[title] => Dummy_Value12
)
[2] => Array
(
[id] => 13
[title] => Dummy_Value13
)
)
)
[1] => Array
(
[title] => Dummy_Value2
[inner] => Array
(
[0] => Array
(
[id] => 21
[title] => Dummy_Value21
)
)
)
)
$inner = []; needs to be within the foreach loop so that it is empty before building each internal element.
Untested - but as follows:
<?php
if (isset($my_values['outer_group']) && !empty($my_values['outer_group'])) {
$outer_types = $my_values['outer_group']['fieldset'];
foreach ($outer_types as $outer_key => $outer_value) {
$inner = [];
if (is_numeric($outer_key)) {
if (isset($outer_value['inner_group']['fieldset'])) {
foreach ($outer_value['inner_group']['fieldset'] as $k => $v) {
if (is_numeric($k)) {
$inner[] = [
'id' => $v['id'],
'title' => !empty($v['title']) ? $token->replace($v['title']) : NULL,
];
}
}
}
$my_values['outer'][$outer_key] = [
'title' => !empty($outer_value['title']) ? $token->replace($outer_value['title']) : NULL,
'inner' => $inner,
];
}
}
}
I am trying to output a username and user email outside of a foreach loop. I am trying to send an email to all WordPress users that are within a certain User Role.
Here is my code:
// Get users and their roles
$user_args = array(
'role__in' => 'new_role',
'orderby' => 'user_nicename',
'order' => 'ASC'
);
$users = get_users($user_args);
foreach ( $users as $user ) :
$user_name = $user->user_email;
$user_email = $user->display_name;
endforeach;
So I can then use them in other areas of the page, ie:.
echo 'Hello, ' . $user_name;
echo 'Send to: ' .$user_email;
I've printed out the $users array which contains the following:
Array
(
[0] => WP_User Object
(
[data] => stdClass Object
(
[ID] => 46
[user_login] => huw
[user_pass] => u7CqxbuQDvApUAF6tT.
[user_nicename] => huw
[user_email] => huw#xxxx.co.uk
[user_url] =>
[user_registered] => 2017-02-06 11:13:09
[user_activation_key] => 1486379590:$P$BkisA4T5j1S/ZjRageafNYHfsdin1S0
[user_status] => 0
[display_name] => Huw Daniel Rowlands
)
[ID] => 46
[caps] => Array
(
[sssg] => 1
[new_role] => 1
[site_member] => 1
[test_role] => 1
)
[cap_key] => jciw_capabilities
[roles] => Array
(
[0] => sssg
[1] => new_role
[2] => site_member
[3] => test_role
)
[allcaps] => Array
(
[read] => 1
[sssg] => 1
[new_role] => 1
[site_member] => 1
[test_role] => 1
)
[filter] =>
)
[1] => WP_User Object
(
[data] => stdClass Object
(
[ID] => 308
[user_login] => jeremy
[user_pass] => LLOKbkPOWQsBKUIk2qL1
[user_nicename] => magnus
[user_email] => jeremy#gmail.com
[user_url] =>
[user_registered] => 2017-05-03 19:24:42
[user_activation_key] => 1493839482:$P$BI/IYldCzsXZowLEiNfxiUkIwVdDKV0
[user_status] => 0
[display_name] => Jeremy
)
[ID] => 308
[caps] => Array
(
[new_role] => 1
[sssg] => 1
[site_member] => 1
)
[cap_key] => jciw_capabilities
[roles] => Array
(
[0] => new_role
[1] => sssg
[2] => site_member
)
[allcaps] => Array
(
[read] => 1
[new_role] => 1
[sssg] => 1
[site_member] => 1
)
[filter] =>
)
)
If you want to send an email to all users with a certain role, try something like this:
// Get users and their roles
$user_args = array(
'role__in' => 'new_role',
'orderby' => 'user_nicename',
'order' => 'ASC'
);
$users = get_users($user_args);
foreach ( $users as $user ) :
$user_email = $user->user_email;
$user_name = $user->display_name;
send_email($user_name, $user_email);
endforeach;
And then write your email function:
function send_email($name, $email) {
// Do email sending stuff here with $name & $email
}
Let's say I have an index action where I want to get a list of projects:
$this->Project->find('all', array('order' => 'Project.modified DESC', 'conditions' => array('Project.user_id' => 1)));
It works well and returns the following array:
Array ( [0] => Array ( [Project] => Array ( [id] => 2 [title] => test project ) ) [1] => Array ( [Project] => Array ( [id] => 1 [title] => first project ) ) )
How do I modify the find function, so it returns the array in the following format:
Array ( [projects] => Array ( [0] => Array ( [id] => 2 [title] => test project ) [1] => Array ( [id] => 1 [title] => first project ) ) )
Thank you!
You could use the Set::combine() utility method to do this. I've used it for similar means as so:
public function groupByMenu() {
return Set::combine (
$this->find (
'all',
array (
'conditions' => array ( 'NavItem.active' => 1 ),
'order' => 'NavMenuItem.display_order'
)
),
'{n}.NavItem.title',
'{n}',
'{n}.NavMenu.id'
);
}
The code above takes a set of navigation items and reorganizes them so that they're grouped by the menu(s) they are displayed within.
It's not really clear if it's the fact that the result is under 'Project' rather than 'projects', but if you don't like that it's under [0] I believe you could use PHPs array_shift:
$result = $this->Project->find('all', array('order' => 'Project.modified DESC', 'conditions' => array('Project.user_id' => 1)));
$result = array_shift($result);
The result will be:
Array ( [Project] => Array ( [id] => 2 [title] => test project ) ) [1] => Array ( [Project] => Array ( [id] => 1 [title] => first project ) )