Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

forna91fi's avatar

Filter Model attribute

Hello everyone, I have a model A with an hasOne relationship to model B Model B have something like 110 attributes, and I would like to get only attributes with value 1.

I wolud like to avois 110 times ->wherenotNull('attribute1')...How can I do?

0 likes
5 replies
tykus's avatar

I don't understand your question; what do you mean "get only attributes with value 1"? Can you provide a basic example?

forna91fi's avatar

What I have now:

App\Models\Vendors\Siemens\SiemensLanguages {#1454 ▼ // resources\views/project/view.blade.php
  #connection: "mysql"
  #table: "siemens_languages"
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  +preventsLazyLoading: false
  #perPage: 15
  +exists: true
  +wasRecentlyCreated: false
  #escapeWhenCastingToString: false
  #attributes: array:109 [▼
    "it-IT" => 1
    "sq-AL" => null
    "af-ZA" => null
    "hy-AM" => null
    "eu-ES" => null
    "be-BY" => null
    "bg-BG" => null
    "cs-CZ" => null
    "ca-ES" => null
    "ky-KG" => null
    "zh-HK" => null
    "zh-MO" => null
    "zh-CN" => null
    "zh-SG" => null
    "zh-TW" => null
    "hr-HR" => null
    "ko-KR" => null
    "da-DK" => null
    "et-EE" => null
    "fo-FO" => null
    "fi-FI" => null
    "fr-BE" => null
    "fr-CA" => null
    "fr-FR" => null
    "fr-LU" => null
    "fr-MC" => null
    "fr-CH" => null
    "gl-ES" => null
    "ka-GE" => null
    "ja-JP" => null
    "el-GR" => null
    "hi-IN" => null
    "id-ID" => null
    "en-AU" => null
    "en-BZ" => null
    "en-CA" => null
    "en-PH" => null
    "en-JM" => null
    "en-GB" => 1
    "en-IE" => null
    "en-NZ" => null
    "en-ZA" => null
    "en-TT" => null
    "en-US" => null
    "en-ZW" => null
    "is-IS" => null
    "it-CH" => null
    "kk-KZ" => null
    "lv-LV" => null
    "lt-LT" => null
    "mk-MK" => null
    "ms-BN" => null
    "ms-MY" => null
    "mn-MN" => null
    "mr-IN" => null
    "nb-NO" => null
    "nn-NO" => null
    "nl-BE" => null
    "nl-NL" => null
    "pl-PL" => null
    "pt-BR" => null
    "pt-PT" => null
    "ro-RO" => null
    "ru-RU" => null
    "sa-IN" => null
    "sk-SK" => null
    "sl-SI" => null
    "es-AR" => null
    "es-BO" => null
    "es-CL" => null
    "es-CO" => null
    "es-CR" => null
    "es-EC" => null
    "es-SV" => null
    "es-GT" => null
    "es-HN" => null
    "es-MX" => null
    "es-NI" => null
    "es-PA" => null
    "es-PY" => null
    "es-PE" => null
    "es-PR" => null
    "es-DO" => null
    "es-ES" => null
    "es-UY" => null
    "es-VE" => null
    "sv-FI" => null
    "sv-SE" => null
    "sw-KE" => null
    "th-TH" => null
    "tt-RU" => null
    "de-AT" => null
    "de-DE" => null
    "de-LI" => null
    "de-CH" => null
    "de-LU" => null
    "tr-TR" => null
    "uk-UA" => null
    "hu-HU" => null
    "vi-VN" => null
    "en-029" => null
    "kok-IN" => null
    "uz-Cyrl-UZ" => null
    "uz-Latn-UZ" => null
    "sr-Cyrl-CS" => null
    "sr-Latn-CS" => null
    "az-Cyrl-AZ" => null
    "az-Latn-AZ" => null
    "languages_machine_id" => 10
  ]

What I would like to have:

App\Models\Vendors\Siemens\SiemensLanguages {#1454 ▼ // resources\views/project/view.blade.php
  #connection: "mysql"
  #table: "siemens_languages"
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  +preventsLazyLoading: false
  #perPage: 15
  +exists: true
  +wasRecentlyCreated: false
  #escapeWhenCastingToString: false
  #attributes: array:109 [▼
    "it-IT" => 1
    "en-GB" => 1
    "languages_machine_id" => 10
  ]
tykus's avatar

@forna91fi I don't understand why this is a problem? Why is it a problem to have attributes with null value? Are you returning this model from an API endpoint as JSON?

In any case, I can't think of any way to filter this in the SQL query, but you could (in PHP) filter the attributes array like this:

$model = $model->newInstance(array_filter($model->getAttributes()));

Please or to participate in this conversation.