I have a request data like this.
array:6 [
"teacherid" => "1"
"classid" => "7"
"schedule" => array:5 [
0 => "Monday - lesson 1"
1 => "Monday - lesson 2"
2 => "Monday - lesson 3"
3 => "Wednesday - lesson 3"
4 => "Wednesday - lesson 4"
]
"year" => "2021"
"smt" => "1"
"_token" => "tSFppSS2TWPQYbQofJD6pPufTLFNpKWAssZNGIHO"
]
and DB look like this
table schedules
I want to validate the input teacher, schedule and classroom like this:
if schedule, teacher and classroom are duplicate
or if schedule and teacher duplicate, but different classroom
I've tried with rule::unique as below but validation doesn't work.
how to make validation of point 1 and 2 above with rule::unique ?
$messages = ['schedule.*.unique' => 'Schedule :input already taken'];
$validator = \Validator::make(
$request->all(),
[
'schedule.*' => 'required',
Rule::unique('schedules')->where(function ($query) use ($request) {
return $query->where('schedule', $request->schedule);
->where('teacher_classes_id', $request->classid)
->where('teacher_subjects_id', $request->teasubid);
}),
],
$messages
);
Related
id
agent_id
currency
1
A0001
IDR
2
A0002
MYR
3
A0001
THB
example currently have a dataset as above,
is it have any way to using only 1 query builder to get the outcome like below
Output:
[
"agent_id" => "A0001",
"currency" => [
"IDR",
"THB",
],
],
[
"agent_id" => "A0002"
"currency" => ["MYR"]
]
Basically likes try to pluck the currency under same agent
Appreciate for all the suggestion.
Found a solution for this problem
$output = $output
->get()
->groupby('agent_id')
->map(function($rows){
return [
"agent_id" => $rows[0]['agent_id'],
"currency" => $rows->pluck('currency'),
];
});
I need to validate every rows from excel user upload in laravel which already converted to numeric array.
The validation i wanted is :
to make sure that the 1st field (0) is not blank,
then check whether the input is 'PRODUCTION-PROJECT' or not, if yes, then the 2nd field is required (1).
how to achieve this ?
My Controller Import class
$file = $request->file('file');
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
$reader->setLoadSheetsOnly(['Upload']);
$reader->setReadDataOnly(true);
$spreadsheet = $reader->load($file->getRealPath());
$sheet = $spreadsheet->getActiveSheet();
$array = $sheet->toArray();
The array looks like this :
array:8 [
0 => array:11 [
0 => "PRODUCTION-PROJECT"
1 => "Consumable equipment expenses"
2 => "2022-07"
3 => "2022-08"
4 => "Forstr"
5 => "DOMESTIC"
6 => "ABCDE"
7 => "IDR"
8 => 2000
9 => 1
10 => "Enim temporibus est quis."
],
1 => array:11 [
0 => "PRODUCTION-PROJECT"
1 => null
2 => "2022-08"
3 => "2022-08"
4 => "RX"
5 => "DOMESTIC"
6 => "FGHIJ"
7 => "USD"
8 => 2000
9 => 1
10 => null
],
];
The validation i've tried so far like so :
$validatedData = Validator::make($array, [
'*.0' => 'required',
'*.1' => Rule::requiredIf('*.0' === 'PRODUCTION-PROJECT')
];
and the validation didn't show any error
The params of Rule::requiredIf should be a callback function that you need to custom the rule and input.
It's better to change Rule::requiredIf('*.0' === 'PRODUCTION-PROJECT') to 'required_if:*.0,PRODUCTION-PROJECT"'
so the correct code is :
$validatedData = Validator::make($array, [
'*.0' => 'required',
'*.1' => 'required_if:*.0,PRODUCTION-PROJECT'
];
I need to pluck two columns name and score from my table corporate_objectives and put it in my graph chart. I'm having two different behavior and I can't seem to get my desired result.
1st code
$getNameAndScore = CorporateObjective::pluck('name');
foreach($getNameAndScore as $key => $item) {
$corporateObjective[] = [$item, '('.$key.'%)'];
}
Result:
"xAxis": [
[
"PEOPLE DEVELOPMENT",
"(0%)"
],
[
"OPTIMUM SYSTEMS AND PROCESSES",
"(1%)"
],
[
"CUSTOMER MANAGEMENT",
"(2%)"
],
[
"REVENUE GROWTH",
"(3%)"
]
],
2nd code
$getNameAndScore = CorporateObjective::pluck('name', 'score');
foreach($getNameAndScore as $key => $item) {
$corporateObjective[] = [$item, '('.$key.'%)'];
}
Result:
"xAxis": [
[
"REVENUE GROWTH",
"(25%)"
]
],
I'm getting all the correct name but the incorrect score in my first code. On my second code, I'm getting the correct name and score but all data is not being pulled out. I wanted to achieve the first code with all the correct score from the second code.
EDIT:
This is how my database looks like
id | name | score
1 PEOPLE DEVELOPMENT 25
2 OPTIMUM SYSTEMS AND PROCESSES 25
3 CUSTOMER MANAGEMENT 25
4 REVENUE GROWTH 25
Is there another way other than pluck? It seems like pluck merges / filters all data with the same value.
This is the correct output of your code. There is no problem here
$getNameAndScore = CorporateObjective::pluck('name', 'score');
foreach($getNameAndScore as $key => $item) {
$corporateObjective[] = [$item, '('.$key.'%)'];
}
How does work pluck here is description
If duplicate keys exist, the last matching element will be inserted into the plucked collection:
$collection = collect([
['brand' => 'Tesla', 'color' => 'red'],
['brand' => 'Pagani', 'color' => 'white'],
['brand' => 'Tesla', 'color' => 'black'],
['brand' => 'Pagani', 'color' => 'orange'],
]);
$plucked = $collection->pluck('color', 'brand');
$plucked->all();
// ['Tesla' => 'black', 'Pagani' => 'orange']
Details in here
So I just made an alternative way of doing it and it might help other people. If there is a more proper way or cleaner way of doing it, please feel free to correct my answer.
$getNameAndScore = CorporateObjective::pluck('name');
foreach($getNameAndScore as $item) {
$key = CorporateObjective::where('name', $item)->value('score');
$corporateObjective[] = [$item, '('.$key.'%)'];
}
return response()->json([
'xAxis' => $corporateObjective,
]);
Result
"xAxis": [
[
"PEOPLE DEVELOPMENT",
"(25%)"
],
[
"OPTIMUM SYSTEMS AND PROCESSES",
"(1%)" // I changed the value in the database and it works
],
[
"CUSTOMER MANAGEMENT",
"(22%)" // I changed the value in the database and it works
],
[
"REVENUE GROWTH",
"(25%)"
]
],
I have a retrieved restaurant list. when the restaurant menu is empty that restaurant showed in last order.. what i do.. can you help..
My Query is :
$restaurantList = $this->Restaurants->find('all', [
'conditions' => $conditions,
'contain' => [
'DeliveryLocations' => [
'conditions' => $areaLocationConditions,
],
'RestaurantMenus' => [
'conditions' => [
'RestaurantMenus.status' => 1,
'RestaurantMenus.delete_status' => 'N'
]
]
],
'limit' => 5,
'order' => 'Restaurants.id DESC'
])->hydrate(false)->toArray();
Simple solution:
by implementing CounterCache
https://book.cakephp.org/3.0/en/orm/behaviors/counter-cache.html and order by cache results.
More complex:
by using Case statements
https://book.cakephp.org/3.0/en/orm/query-builder.html#case-statements
select 'has_menus' if restuarant has menus then 1 else 0
order by that results
I have 2 systems which are seperate from each other. To let them communicate I have built an API. Both systems have common Models, one of which is a Project Model with all its relationships.
Within System A, to send a Project with its relationships I do the following.
$client = new GuzzleHttp\Client();
$jsonProject = json_encode(Project::with('projectType', 'projectType.projectTypeData',
'projectAssets', 'projectAssets.projectAssetsData')->find($project->id));
$req = $client->request('POST', 'https://someurl/postProject', [
'body' => json_encode($jsonProject),
'headers' => [
'Content-Type' => 'application/json',
'Content-Length' => strlen($jsonProject),
]
]);
Within System B I have the routes set up, and when the above is posted the following is triggered
public function store(Request $request)
{
$project = $request->all();
dd($project);
}
When I make the post request from System A I see something like this because of the dump in System B (I have removed a lot of the output to cut down on code).
array:17 [
"id" => 3
"projectName" => "Test Project"
"user_id" => 1
"contact" => "John Doe"
"project_type" => array:7 [
"id" => 3
"project_id" => 3
"project_type_data" => array:1 [
0 => array:8 [
"id" => 5
"projectType" => "Standard"
]
]
]
"project_assets" => array:7 [
"id" => 2
"project_id" => 3
"project_assets_data" => array:4 [
0 => array:8 [
"id" => 5
"label" => "Logo"
"altTag" => ""
"urlPath" => ""
"projectAssetsId" => 2
]
]
]
]
So everything seems to work fine. My question is this. System B now has a load of json data containing all the data required to make the models. If I was able to send a model via the api (not json) then I could easily create the model within System B. Because it is json data however, and I cant decode it because it is not a string, do I have to start looping it all in order to make my models?