Jump to content

PHP Laravel Undefined property help needed.

Joveice
Go to solution Solved by leodaniel,

I don't know your error, but if $value->service is a collection I would use

$value->service->each(function ($item, $key) use($request,$ok) {
    if( in_array($item,$request->services) ){
        $ok = true;
        
        // return false to break the loop
        return false;
    }
});

 

foreach ($value->service as $service) {
    if (in_array($service, $request->service)) {
        $ok = true;
    }
}

"Undefined property: stdClass::$service" line 254 (the foreach line)

foreach ($value->service as $service) {
    echo $service;
}
die();

"service1service2service3service4"

foreach ($value->service as $service) {
    echo gettype($service);
}
die();

"stringstringstringstring"

What's going on here?

 

($value->service is a collection and $request->service is a array)

 

I have literary no clue whats going on here, I'd love if someone spots what I'm doing wrong.

Back-end developer, electronics "hacker"

Link to comment
Share on other sites

Link to post
Share on other sites

I don't know your error, but if $value->service is a collection I would use

$value->service->each(function ($item, $key) use($request,$ok) {
    if( in_array($item,$request->services) ){
        $ok = true;
        
        // return false to break the loop
        return false;
    }
});

 

Business Management Student @ University St. Gallen (Switzerland)

HomeServer: i7 4930k - GTX 1070ti - ASUS Rampage IV Gene - 32Gb Ram

Laptop: MacBook Pro Retina 15" 2018

Operating Systems (Virtualised using VMware): Windows Pro 10, Cent OS 7

Occupation: Software Engineer

Link to comment
Share on other sites

Link to post
Share on other sites

You can also use intersect

$intersection = $value->service->intersect($request->service);

if( $intersection->notEmpty() ){
    $ok = true;
}

 

Business Management Student @ University St. Gallen (Switzerland)

HomeServer: i7 4930k - GTX 1070ti - ASUS Rampage IV Gene - 32Gb Ram

Laptop: MacBook Pro Retina 15" 2018

Operating Systems (Virtualised using VMware): Windows Pro 10, Cent OS 7

Occupation: Software Engineer

Link to comment
Share on other sites

Link to post
Share on other sites

Thanks leodaniel, still not sure how I managed to get that error, but anyways it works now with your solution.

Back-end developer, electronics "hacker"

Link to comment
Share on other sites

Link to post
Share on other sites

You can't call a in_array() method on a Laravel collection. you can search in it with the given methods listed on the laravel documentation.

If you really need to call the in_array() method for some reason you can do. 

foreach ($value->service->toArray() as $service) {
    if (in_array($service, $request->service)) {
        $ok = true;
    }
}

But it defeats the purpose of a collection a bit.

Quote or mention me if not feel ignored 

Link to comment
Share on other sites

Link to post
Share on other sites

3 hours ago, Cruorzy said:

You can't call a in_array() method on a Laravel collection. you can search in it with the given methods listed on the laravel documentation.

If you really need to call the in_array() method for some reason you can do. 

From what I can tell, there should be no problem. Laravel Collection implements ArrayAccess and the variable $service is a string.

So calling the in array function should work fine.

 

But I completely agree: 

3 hours ago, Cruorzy said:

But it defeats the purpose of a collection a bit.

 

Business Management Student @ University St. Gallen (Switzerland)

HomeServer: i7 4930k - GTX 1070ti - ASUS Rampage IV Gene - 32Gb Ram

Laptop: MacBook Pro Retina 15" 2018

Operating Systems (Virtualised using VMware): Windows Pro 10, Cent OS 7

Occupation: Software Engineer

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×