Я пытаюсь использовать отношение ownToMany , у меня никогда не было проблем с ним, но по какой-то причине он теперь возвращается пустым.
Но, если я проверю tinker , я получу массив с данными, возвращенными
Я настроил свое отношение в модели User следующим образом:
// linking with table tag
public function tags(){
return $this->belongsToMany('App\Tag','tag_user','user_id','tag_id');
}
Модель тега:
public function user(){
return $this->belongsToMany('App\User','tag_user','tag_id','user_id');
}
PostController
public function create()
{
// bringing user details to show his preferneces
$user_id = Auth::user()->user_id;
$user = User::find($user_id)->first();
//post_types
$types= PostType::pluck('name', 'post_type_id');
return view('user.create_post')->with('user', $user)->with('types',$types);
}
В поле зрения:
@foreach ($user->tags as $tag)
<label class = "btn btncategory">
<input type = "checkbox"
name = "tag_selected[]"
value = "{{$tag->tag_id}}" >
{{$tag->tag_name}}
</label>
@endforeach
Миграция
метка
Schema::create('tags', function (Blueprint $table) {
$table->increments('tag_id');
$table->string('tag_name')->unique();
$table->integer('type');
$table->integer('used_time')->default('0');
$table->timestamps();
});
Schema::create('tag_user', function(Blueprint $table){
$table->integer('user_id');
$table->integer('tag_id');
$table->primary(['user_id', 'tag_id']);
});
Пользователи
Schema::create('users', function (Blueprint $table) {
$table->increments('user_id');
$table->string('user_name')->unique();
$table->string('fname')->nullable();
$table->string('lname')->nullable();
... etc etc