Reputation: 645
I have the follwing problem, I have two models, Level and Class, each level has many classes inside it, I need to add all level classes at once , with the follwing format:
$levelclasses['0'][level_id]=1;
$levelclasses['0'][class][0][supervisor_id]=2;
$levelclasses['1'][level_id]=2;
$levelclasses['1'][class][0][supervisor_id]=2;
I added this successfully :
$levelNewClasses;
$ids;
$count=0;
foreach ($data['levelclasses'] as $levelClass) {
$level=\App\InstituteLevel::find($levelClass['level_id']);
$ids[$count]=$level->id;
$count++;
foreach ($levelClass['class'] as $classdata) {
$class= new \App\EClass($classdata);
$class->school_year_id=\App\SchoolsYear::first()->id;
$level->classes()->save($class);
$levelNewClasses[$level->id][$class->id]=$class->id;
}
}
but now I want to return with the response just the inserted classes, not all classes , if I do this :
$level->classes;
it will return all , so I stored the new classes id's in an array and now I try to filter the classes returned from this relation $level->classes based on theses id's, I tried this
foreach ($levels as $level){
$levelclasses=$level->classes->pluck('id');
$level->classes=$levelclasses->intersect($levelNewClasses[$level->id]);
}
but this doesn't work ! , Any suggestions?
Upvotes: 0
Views: 57
Reputation: 4435
Check following code. I think it would work.
$levelNewClasses;
$ids;
$count=0;
insertedClasses = array();//Array to store inserted classes
foreach ($data['levelclasses'] as $levelIndex=>$levelClass) {\\updated to include level index
$level=\App\InstituteLevel::find($levelClass['level_id']);
$ids[$count]=$level->id;
$count++;
foreach ($levelClass['class'] as $index=>$classdata) {
$class= new \App\EClass($classdata);
$class->school_year_id=\App\SchoolsYear::first()->id;
//Use following code to get all the inserted classes as array
$insertedClasses[$levelIndex][$index] = $level->classes()->save($class)->toArray();\\updated to use 2d array to store both level and class
$levelNewClasses[$level->id][$class->id]=$class->id;
}
}
Upvotes: 1
Reputation: 43
This is a bit of a hacky way to do it, but if your model has the created_at
or updated_at
timestamps (which Eloquent models do by default), you could see if it was created or updated in the last few seconds. For instance, to see if it was created in the last 10 seconds:
foreach($level->classes as $class) {
if($class->created_at->diffInSeconds(Carbon::now()) < 10) {
// do something...
}
}
In order to do this, you need to import Carbon. Put use Carbon\Carbon;
at the top of your class to do this.
Essentially, what the above code is doing is figuring out when each of the level's classes were created, comparing it in seconds to the current time, and entering that if
statement if the class was created less than 10 seconds before the current time (Carbon::now()
). Obviously, you don't necessarily have to do 10 seconds - find which time works best for you.
As I said, this is a bit of a hack, and may not work for all cases, but I think it will work for the case you're describing. Hope this helps!
Upvotes: 1