Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

172
Views
Laravel intersect wont get input

I have an intersect in my update method:

$inputs = $request->intersect('message','name','email','is_read');

If i send an update request where is_read=0 the intersect returns an empty array. Works fine with anything else (false, 1 etc)

Any tips?

Thanks!

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

ALERT Try to move to another implementation and stop using intersect() method, it will be removed from the future versions of Laravel: Link

IF you mean that the is_read key is missing from the final array (and not the the whole array is empty, see my comment), this is because of the implementation of the intersect() method.

The intersect method simply wraps the only() method of the Illuminate\Http\Request class and do an array_filter over the result.

This is the implementation:

/**
 * Intersect an array of items with the input data.
 *
 * @param  array|mixed  $keys
 * @return array
 */
public function intersect($keys)
{
    return array_filter($this->only(is_array($keys) ? $keys : func_get_args()));
}

In your case, we can decompose the code as this:

step1

$results = $request->only('message','name','email','is_read');

At this point, $results is

Array
(
    [message] => message
    [name] => name
    [email] => email
    [is_read] => 0
)

However, at step2

step2

$filteredResults = array_filter($results);

The result becomes

Array
(
    [message] => message
    [name] => name
    [email] => email
)

And this is because of how array_filter works. It expect an array as the first parameter, then an optional callback (used to filter the array) and a flag.

From php reference

What happens when no callback is provided (like in this case?)

If no callback is supplied, all entries of array equal to FALSE (see converting to boolean) will be removed.

If you look at the link converting to boolean you'll see that 0 (zero) is assumed to be FALSE and, for that reason, removed from the array.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!