You should explain what you want to achieve, it is very unclear.
Oct 5, 2015
12
Level 1
Eloquent adds data not in model
I have two data sets where I'm comparing the same Model, but for some reason Eloquent is leaving adding in the row that is used for pivoting. Obviously, when I compare it to the original, it fails since the one being called through the hasMany relationship has one extra row. Please help! Many thanks!
the "Collection" model's method:
public function nouns()
{
return $this->hasManyThrough('App\Models\CategoryNouns',
'App\Models\CollectionNouns',
'collection_id',
'id');
}
dd of the object:
CategoryNouns {#411 ▼
+timestamps: false
#guarded: array:1 [▶]
#table: "category_nouns"
#connection: null
#primaryKey: "id"
#perPage: 15
+incrementing: true
#attributes: array:8 [▼
"id" => "9"
"noun" => "lose animal"
"weight" => "1"
"category_id" => "12"
"created_at" => "0000-00-00 00:00:00"
"updated_at" => "0000-00-00 00:00:00"
"deleted_at" => null
"collection_id" => "44"
]
#original: array:8 [▼
"id" => "9"
"noun" => "lose animal"
"weight" => "1"
"category_id" => "12"
"created_at" => "0000-00-00 00:00:00"
"updated_at" => "0000-00-00 00:00:00"
"deleted_at" => null
"collection_id" => "44"
]
#relations: []
#hidden: []
#visible: []
#appends: []
#fillable: []
#dates: []
#dateFormat: null
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: true
+wasRecentlyCreated: false
#forceDeleting: false
}
dd of the original Model being compared:
CategoryNouns {#456 ▼
+timestamps: false
#guarded: array:1 [▶]
#table: "category_nouns"
#connection: null
#primaryKey: "id"
#perPage: 15
+incrementing: true
#attributes: array:7 [▼
"id" => "64"
"noun" => "testing wrong category system"
"weight" => "100"
"category_id" => "1"
"created_at" => "0000-00-00 00:00:00"
"updated_at" => "0000-00-00 00:00:00"
"deleted_at" => null
]
#original: array:7 [▼
"id" => "64"
"noun" => "testing wrong category system"
"weight" => "100"
"category_id" => "1"
"created_at" => "0000-00-00 00:00:00"
"updated_at" => "0000-00-00 00:00:00"
"deleted_at" => null
]
#relations: []
#hidden: []
#visible: []
#appends: []
#fillable: []
#dates: []
#dateFormat: null
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: true
+wasRecentlyCreated: false
#forceDeleting: false
}
And now, for the original Model:
class CategoryNouns extends Model {
use SoftDeletes;
public $timestamps = false;
protected $guarded = ['id'];
protected $table ='category_nouns';
/* input requirements */
public static $rules = [
'noun' => 'required|unique:category_nouns',
'weight' => 'required|integer',
'category_id' => 'required|integer'
];
public function category()
{
return $this->hasOne('App\Models\Category');
}
}
And now the code used to compare:
/* now we check for any prior corrections directed to our associations */
$collections = [];
//@TODO: check by how much does it match the prior collection
for($i=0; $i<count($allCatNouns->values()); $i++) {
$nounsGroup = $allCatNouns->values()->get($i); //make a collection out of the group
if($col = Collection::where('old_category_id', $nounsGroup[0]->category_id)->get()) {
$id = $nounsGroup[0]->category_id; // take the cat id with us
foreach($col as $nounObj) {
//@TODO ain't necessarily only in one collection
$collections[$id] = $nounObj->nouns->values();
}
}
}
if(!empty($collections)) {
// should be a replica of $allCatnouns, if this issue has been submitted before and corrected by staff
// $collections = collect($collections)->sortBy('category_id');
for($i=0; $i<count($allCatNouns->values()); $i++) {
$nounsGroup = $allCatNouns->values()->get($i); //make a collection out of the group
// if it's category id is in Collections, check the noun groups to see if they match
if(isset($collections[$nounsGroup[0]->category_id])) {
// this -V- is the category subcollection
if($collections[$nounsGroup[0]->category_id][0] == $nounsGroup[0]) {
dd(TRUE);
}
}
}
}
Please or to participate in this conversation.