TransWikia.com

Как проверить, при переборе languages(collect) что такой то language относится к профилю в котором перебирается?

Stack Overflow на русском Asked on November 10, 2021

В blade.php перебираю все языки, и мне нужно узнать относится этот язык к данному профилю или нет, если да то отмечаю галкой, если нет пропускаю

все языки текущего профиля :

 $profile_languages = AppProfile::with('languages.language')->find($dataTypeContent->id);
@foreach(AppLanguage::all() as $language) 
  @if($language->ЕСТЬ В $profile_languages)
   <input class="language_level languages" data-language_id="{{$language->id}}" data-delete="true" type="checkbox" name="language" value="" checked>
@else
     <input class="language_level languages" data-language_id="{{$language->id}}" data-delete="false" type="checkbox" name="language" value="">
@endif
@endforeach
dump()
AppProfile::with('languages.language')->find($dataTypeContent->id);
    AppProfile {#2311 ▼
      #dates: array:2 [▶]
      #connection: "mysql"
      #table: "profiles"
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #attributes: array:45 [▶]
      #original: array:45 [▶]
      #changes: []
      #casts: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: array:1 [▼
        "languages" => IlluminateDatabaseEloquentCollection {#2309 ▼
          #items: array:1 [▼
            0 => AppLanguageLevel {#2315 ▼
              #connection: "mysql"
              #table: "language_levels"
              #primaryKey: "id"
              #keyType: "int"
              +incrementing: true
              #with: []
              #withCount: []
              #perPage: 15
              +exists: true
              +wasRecentlyCreated: false
              #attributes: array:6 [▶]
              #original: array:6 [▶]
              #changes: []
              #casts: []
              #dates: []
              #dateFormat: null
              #appends: []
              #dispatchesEvents: []
              #observables: []
              #relations: array:1 [▼
                "language" => AppLanguage {#2322 ▼
                  #connection: "mysql"
                  #table: "languages"
                  #primaryKey: "id"
                  #keyType: "int"
                  +incrementing: true
                  #with: []
                  #withCount: []
                  #perPage: 15
                  +exists: true
                  +wasRecentlyCreated: false
                  #attributes: array:7 [▼
                    "id" => 3
                    "title" => "Французский"
                    "show_for_mens" => "0"
                    "show_for_girls" => "0"
                    "active" => "0"
                    "created_at" => "2020-07-21 13:59:19"
                    "updated_at" => "2020-07-21 13:59:19"
                  ]
                  #original: array:7 [▶]
                  #changes: []
                  #casts: []
                  #dates: []
                  #dateFormat: null
                  #appends: []
                  #dispatchesEvents: []
                  #observables: []
                  #relations: []
                  #touches: []
                  +timestamps: true
                  #hidden: []
                  #visible: []
                  #fillable: []
                  #guarded: array:1 [▶]
                }
              ]
              #touches: []
              +timestamps: true
              #hidden: []
              #visible: []
              #fillable: []
              #guarded: array:1 [▶]
            }
          ]
        }
      ]
      #touches: []
      +timestamps: true
      #hidden: []
      #visible: []
      #fillable: []
      #guarded: array:1 [▶]
      #forceDeleting: false
    }

//dump() AppLanguage

IlluminateDatabaseEloquentCollection {#2301 ▼
  #items: array:3 [▼
    0 => AppLanguage {#2298 ▼
      #connection: "mysql"
      #table: "languages"
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #attributes: array:7 [▼
        "id" => 1
        "title" => "немецкий"
        "show_for_mens" => "0"
        "show_for_girls" => "0"
        "active" => "0"
        "created_at" => "2020-07-18 18:41:42"
        "updated_at" => "2020-07-18 18:41:42"
      ]
      #original: array:7 [▶]
      #changes: []
      #casts: []
      #dates: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: []
      #touches: []
      +timestamps: true
      #hidden: []
      #visible: []
      #fillable: []
      #guarded: array:1 [▶]
    }
    1 => AppLanguage {#2299 ▼
      #connection: "mysql"
      #table: "languages"
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #attributes: array:7 [▼
        "id" => 2
        "title" => "Русский"
        "show_for_mens" => "0"
        "show_for_girls" => "0"
        "active" => "0"
        "created_at" => "2020-07-18 18:41:49"
        "updated_at" => "2020-07-18 18:41:49"
      ]
      #original: array:7 [▶]
      #changes: []
      #casts: []
      #dates: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: []
      #touches: []
      +timestamps: true
      #hidden: []
      #visible: []
      #fillable: []
      #guarded: array:1 [▶]
    }
    2 => AppLanguage {#2303 ▼
      #connection: "mysql"
      #table: "languages"
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #attributes: array:7 [▼
        "id" => 3
        "title" => "Французский"
        "show_for_mens" => "0"
        "show_for_girls" => "0"
        "active" => "0"
        "created_at" => "2020-07-21 13:59:19"
        "updated_at" => "2020-07-21 13:59:19"
      ]
      #original: array:7 [▶]
      #changes: []
      #casts: []
      #dates: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: []
      #touches: []
      +timestamps: true
      #hidden: []
      #visible: []
      #fillable: []
      #guarded: array:1 [▶]
    }
  ]
}

One Answer

Вывод в blade(один из вариантов):

  1. Собрать все id языков, которые знает пользователь(можно юзать и методы коллекции, например pluck()):
$profile_languages = [];

foreach($profile->languages as $language) {
  $profile_languages[] = $language->language->id;
}
  1. При выводе языков проверять, есть ли такой id языка в массиве языков профиля
@foreach(AppLanguage::all() as $language)
  @if(in_array($language->id, $profile_languages)
    <input class="language_level languages" data-language_id="{{$language->id}}" data-delete="true" type="checkbox" name="language" value="" checked>
  @else
    <input class="language_level languages" data-language_id="{{$language->id}}" data-delete="false" type="checkbox" name="language" value="">
  @endif
@endforeach

Answered by Не быть рабом на Руси on November 10, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP