[英]onetoMany relationships not working or printing the wrong data. Laravel 5.3
所以,這很奇怪。 我一直在實現用戶和各種數據集之間的onMany關系。 第一個起作用了。 我設置了數據透視表,但沒有,數據在表的兩端都是正確的,但是當laravel調用數據時,結果甚至沒有關閉:
讓我們看一下show user data函數:
public function show($id)
{
try {
$loc = UserEdit::findorFail($id);
$array = UserEdit::findorFail($id)->toArray();
//$prefs = Ministry_Prefs_User::find($id);
return view('UserEdit', compact('array', 'loc'));
} catch(\Exception $e) {
return \Redirect::route('users.index')
->withMessage('This user does not exist');
}
}
在刀片中,我根據自己的喜好打印出標簽:
Preferences:<br>
{{ $array['Preferences'] }}<br>
@foreach ($loc->prefs_user as $tag)
{{ $tag->tag }}<br>
@endforeach<br><br><br>
第一個數組從我繼承的舊表中打印存儲在原始未修改用戶數據中的內容。 這樣一來,我就可以比較並確保從必須從該信息生成的數據透視表中獲取正確的數據。 原來那是個好主意,因為這是印刷內容:
1A-4,1-2,1-3,2-3,3-6,4-7,6-11,8-6,8-10,9-4,7A-4
1A-1
1A-1
1A-1
1A-3
1A-4
1A-5
1A-7
1-1
1-1
1-6
1A-8
除了字母順序,我看不到其他任何圖案嗎? 為什么會這樣?
下一點甚至更奇怪。 使用相同的代碼后,我建立的其他數據透視表完全沒有結果:
public function country() {
return $this->belongsToMany('App\Country', 'country_user', 'user_id', 'country_id');
}
public function prefs_user() {
return $this->belongsToMany('App\Prefs_User', 'tag_user', 'user_id', 'tag_id');
}
public function language() {
return $this->belongsToMany('App\language', 'language_user', 'user_id', 'language_id');
為什么會這樣? 這些模型如下所示:
Prefs_User
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Prefs_User extends Model
{
protected $table = 'prefs';
public function prefs_user() {
return $this->belongsToMany('App\UserEdit', 'tag_user', 'tag_id', 'user_id');
}
}
國家
namespace App;
use Illuminate\Database\Eloquent\Model;
class Country extends Model
{
protected $table = 'countries';
public function country_user() {
return $this->belongsToMany('App\UserEdit', 'country_user', 'country_id', 'user_id');
}
//protected $fillable = ['region', 'country'];
}
語言
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class language extends Model
{
protected $table = 'languages';
//protected $fillable = ['user_id','language_id'];
public function language() {
return $this->belongsToMany('App\UserEdit', 'language_user', 'language_id', 'user_id');
}
}
好。 因此,這是由Laravel的工作原理引起的問題。
經過一番嘗試和錯誤之后,我猜測Laravel在使用數據透視魔術方法時只會檢查id,而不會搜索任何其他數據。
因此,我編寫了一個函數來獲取首選項,將其分解為原始標簽,然后將該標簽與首選項列表進行比較,獲取與該標簽關聯的ID,然后將其與其他數據一起打印到數據透視表中。 為了進行測試和仔細檢查,最初包含了原始標簽,然后將其刪除。
public function move() {
//get the preferences from the users table (this is stored badly "1-7,3-4,4-6,6-6,6-10,8-5,8-9,8-10,8-13,9-3,9-9") this returns a collection
$prefs = DB::table('users')->select('id', 'Preferences')->where('Preferences', '!=', '')->get();
//iterate through each item in the collection
foreach ($prefs as $pref) {
//get the tags only
$tags = $pref->Preferences;
//break up the tags into a searchable array by exploding each preference at the comma
$tag = explode(',', $tags);
//for each tag in the array assign it to t
foreach ($tag as $t) {
//get the id from the preference table when the tag matches t
$new = DB::table('prefs')->select('id')->where('tag', $t)->first();
//make a new model instance to insert data
$taguser = new Tag(array(
'user_id' => $pref->id,
'tag_id' => $t,
'tags' => $new->id
));
//insert data
$taguser->save(); //save in table
//dd($taguser); <- Since we have a lot of items, dd will break the printout after the first, meaning you can check to see if your information is being handled properly
}
}
return view('refactor');
擺脫原始的標簽ID並僅用引用該標簽的普通ID替換它,就意味着laravel現在可以搜索正確的數據並返回有用的結果。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.