0
我有這樣的功能在這裏:爲什麼WHERE子句不接受變量? Laravel 5.3
public function move() {
$prefs = DB::table('users')->select('id', 'Preferences')->where('Preferences', '!=', '')->get();
foreach ($prefs as $pref) {
$tags = $pref->Preferences;
$tag = explode(',', $tags);
foreach ($tag as $t) {
$new = DB::table('prefs')->select('id', 'tag')->where('tag', '=', $t)->get();
$taguser = new Tag(array(
'user_id' => $pref->id,
'tag_id' => $t,
'tagsid' => $new->id
));
$taguser->save(); //save in table
}
}
return view('refactor');
}
它基本上只是抓住從沒有正確地設置了一箇舊的數據庫表中的數據,分割了的數據,將其與喜好的列表進行比較另一個表並獲取該數據ID,然後將標記,用戶標識和首選項ID打印到數據透視表中。
或者這就是它應該做的。現在它告訴我來自prefs表的id是未定義的。那最後一位'tagsid' => $new->id
< =顯然那不存在。
但它應該。我做錯了什麼?
這裏是一塊錯誤:
Undefined property: Illuminate\Support\Collection::$id
at HandleExceptions->handleError('8', 'Undefined property: Illuminate\Support\Collection::$id', 'C:\xampp\htdocs\Missionseek2\app\Http\Controllers\RefactorController.php', '29', array('prefs' => object(Collection), 'pref' => object(stdClass), 'tags' => '1-3,1-5,1-4,2-3,3-2,3-9,3-3,3-4,3A-5,3A-8,5-2,8-4,8-8,8-13,9-5,7A-2,7A-4,7A-10', 'tag' => array('1-3', '1-5', '1-4', '2-3', '3-2', '3-9', '3-3', '3-4', '3A-5', '3A-8', '5-2', '8-4', '8-8', '8-13', '9-5', '7A-2', '7A-4', '7A-10'), 't' => '1-3', 'new' => object(Collection))) in RefactorController.php line 29