Skip to content
Advertisement

PHP Laravel – Get array of models by search or return empty array

Very new to PHP and Laravel so this is probably a noob question.

public function listForReportEvent(Request $request)
{
    $sites = $request->filled('name') ? $this->searchSiteByName($request->query('name')) : '';

    if (is_array($sites)) {
        return $sites->map(function (Site $site) {
            return [
                'text' => $site->study->ref_study_id . '-' . $site->site_number . ': ' . $site->name,
                'value' => $site->id,
            ];
        });
    } elseif (is_object($sites)) {
        return null;
    }
}

I know the $sites variable has content in it when I dd($sites); but when it comes to actually returning a value, I get nothing.

Any thoughts what I am doing wrong?

EDIT: The other function referenced above is here (shouldn’t really need it though).

private function searchSiteByName(string $name): Collection
{
    return Site::query()
        ->where('name', 'LIKE', "%$name%")
        ->orWhere('site_number', 'LIKE', "%$name%")
        ->get();
}

dd($sites) results:

IlluminateDatabaseEloquentCollection {#1797
  #items: array:9 [
    0 => AppModelsSite {#1796
      #casts: array:11 [
        "name" => "string"
        "site_number" => "string"
        "description" => "string"
        "subject_planned_cnt" => "integer"
        "subject_enrolled_cnt" => "integer"
        "created_at" => "datetime"
        "updated_at" => "datetime"
        "created_by_id" => "integer"
        "updated_by_id" => "integer"
        "deleted_at" => "datetime"
        "deleted_by_id" => "integer"
      ]
      #appends: []
      #fillable: array:9 [
        0 => "study_id"
        1 => "site_number"
        2 => "name"
        3 => "subject_planned_cnt"
        4 => "subject_enrolled_cnt"
        5 => "patient_recruitment_status"
        6 => "closed"
        7 => "state_id"
        8 => "site_status_id"
      ]
      #CREATED_AT: "created_at"
      #UPDATE_AT: "updated_at"
      #dates: array:1 [
        0 => "deleted_at"
      ]
      -format: "M/j/Y - g:iA"
      #connection: "mysql"
      #table: "sites"
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      +preventsLazyLoading: false
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #attributes: array:16 [
        "id" => 1
        "name" => "1001"
        "site_number" => "1001"
        "subject_planned_cnt" => 0
        "subject_enrolled_cnt" => 0
        "patient_recruitment_status" => null
        "closed" => 0
        "study_id" => 1
        "created_at" => null
        "updated_at" => null
        "created_by_id" => null
        "updated_by_id" => null
        "deleted_at" => null
        "deleted_by_id" => null
        "state_id" => 53
        "site_status_id" => 1
      ]
      #original: array:16 [
        "id" => 1
        "name" => "1001"
        "site_number" => "1001"
        "subject_planned_cnt" => 0
        "subject_enrolled_cnt" => 0
        "patient_recruitment_status" => null
        "closed" => 0
        "study_id" => 1
        "created_at" => null
        "updated_at" => null
        "created_by_id" => null
        "updated_by_id" => null
        "deleted_at" => null
        "deleted_by_id" => null
        "state_id" => 53
        "site_status_id" => 1
      ]
      #changes: []
      #classCastCache: []
      #dateFormat: null
      #dispatchesEvents: []
      #observables: []
      #relations: []
      #touches: []
      +timestamps: true
      #hidden: []
      #visible: []
      #guarded: array:1 [
        0 => "*"
      ]
      #forceDeleting: false
      #userstamping: true
    }
    1 => AppModelsSite {#1795
      #casts: array:11 [
        "name" => "string"
        "site_number" => "string"
        "description" => "string"
        "subject_planned_cnt" => "integer"
        "subject_enrolled_cnt" => "integer"
        "created_at" => "datetime"
        "updated_at" => "datetime"
        "created_by_id" => "integer"
        "updated_by_id" => "integer"
        "deleted_at" => "datetime"
        "deleted_by_id" => "integer"
      ]
      #appends: []
      #fillable: array:9 [
        0 => "study_id"
        1 => "site_number"
        2 => "name"
        3 => "subject_planned_cnt"
        4 => "subject_enrolled_cnt"
        5 => "patient_recruitment_status"
        6 => "closed"
        7 => "state_id"
        8 => "site_status_id"
      ]
      #CREATED_AT: "created_at"
      #UPDATE_AT: "updated_at"
      #dates: array:1 [
        0 => "deleted_at"
      ]
      -format: "M/j/Y - g:iA"
      #connection: "mysql"
      #table: "sites"
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      +preventsLazyLoading: false
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #attributes: array:16 [
        "id" => 43
        "name" => "1001"
        "site_number" => "1001"
        "subject_planned_cnt" => 0
        "subject_enrolled_cnt" => 0
        "patient_recruitment_status" => null
        "closed" => 0
        "study_id" => 2
        "created_at" => null
        "updated_at" => null
        "created_by_id" => null
        "updated_by_id" => null
        "deleted_at" => null
        "deleted_by_id" => null
        "state_id" => 12
        "site_status_id" => 3
      ]
      #original: array:16 [
        "id" => 43
        "name" => "1001"
        "site_number" => "1001"
        "subject_planned_cnt" => 0
        "subject_enrolled_cnt" => 0
        "patient_recruitment_status" => null
        "closed" => 0
        "study_id" => 2
        "created_at" => null
        "updated_at" => null
        "created_by_id" => null
        "updated_by_id" => null
        "deleted_at" => null
        "deleted_by_id" => null
        "state_id" => 12
        "site_status_id" => 3
      ]

Advertisement

Answer

$sites is a Collection when $request->filled('name') is true.

When that’s the case, is_array($sites) will always return false since it is not an array but a Collection.

That’s why you fall into the return null part.

What you could check instead of is_array($sites):

public function listForReportEvent(Request $request)
{
    $sites = $request->filled('name') ? $this->searchSiteByName($request->query('name')) : '';


// if $sites isn't '' and isn't an empty collection, then map...
// else return null

    if ($sites && !$sites->isEmpty()) {
        return $sites->map(function (Site $site) {
            return [
                'text' => $site->study->ref_study_id . '-' . $site->site_number . ': ' . $site->name,
                'value' => $site->id,
            ];
        });
    } else {
        return null;
    }
}
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement