That is a single element? The type is App\Models\testInfo which is the model..
Mar 15, 2022
29
Level 3
Find() returns collection instead of single element
I made some changes and starting watching the lessons that you recommended (didn't change the table names neither the column names in order to much laravel "rules") . My issue is caused because find($id) returns a collection instead of one element.
public function update(Request $req, $id){
$input = $req->all();
$testInfo = testInfo::find($id);
$testInfo->NAME = $input['Name'];;
$testInfo->TIME = $input['Time'];;
$testInfo->OBSERVER_ID = $req->observer_id;
dd($id,testInfo::find($id),$testInfo->NAME = $input['Name'],$testInfo->TIME = $input['Time'],$testInfo->OBSERVER_ID = $req->observer_id, $testInfo->LOCATION_ID = $req->LOCATION_ID);
$testInfo->LOCATION_ID = $req->LOCATION_ID;
$testInfo->save();
}
The result of the dd() is
"4"
App\Models\testInfo {#284 ▼
#table: "infotest"
+timestamps: false
#fillable: array:5 [▶]
#connection: "mysql"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
+preventsLazyLoading: false
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#escapeWhenCastingToString: false
#attributes: array:5 [▶]
#original: array:5 [▶]
#changes: []
#casts: []
#classCastCache: []
#attributeCastCache: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
#hidden: []
#visible: []
#guarded: array:1 [▶]
}
"GEORGE"
"18:30"
"1"
"2"
How can I prevent find from creating a collection and return one element?
Please or to participate in this conversation.