Support the ongoing development of Laravel.io →
Input Database Eloquent
Last updated 1 year ago.
0

Supposing that $jsons is an array of objects, and that each object has a description property in which you would search

$items = new Collection($jsons);
$results = $items->filter(function($item) use ($search) {
                  return strpos($item->description, $search);
              })->get();
Last updated 1 year ago.
0

longilineo said:

Supposing that $jsons is an array of objects, and that each object has a description property in which you would search

$items = new Collection($jsons); $results = $items->filter(function($item) use ($search) { return strpos($item->description, $search); })->get();

Wonderful solution, Thank you for your help. I'm going to give this one a try, Let you know.

Last updated 1 year ago.
0

longilineo, Can I do anything like this.


	$results  = $items->filter(function($item) use ($search)
		{
			return strpos(null, array(

 					$item->moduleCode,$search,
 					$item->staffmember,$search
 					

				));

			$data = array_merge($results);

		})->get();

Last updated 1 year ago.
0

If it's a question you're using strpos in a completely wrong manner: http://php.net/manual/it/function.strpos.php

Everything you're doing in the function after return will never execute.

filter method add an $item to $results only if returned condition is true.

If $search is equal to "bar", my example take all $items in which description is something like "every thing else bar every thing else".

If I understand you need all items in which moduleCode OR staffmember contain $search. So the code should be:

$items = new Collection($jsons);
$results = $items->filter(function($item) use ($search) {
              return strpos($item->moduleCode, $search) ||  
                       strpos($item->staffmember, $search) ;
          })->get();
Last updated 1 year ago.
0

Thank you for reply.

One more question.

Can I add tags and "like" to my query search function.

('title', 'LIKE', '%' . $search . '%')

Last updated 1 year ago.
0

Your datasource isn't a relational database so you can't make a query with sql syntax and you can't use Eloquent.

However with my code you are doing something like:

select * from results where moduleCode like "%$search%" or staffmember like "%$search%";

But you can't use this syntax.

Last updated 1 year ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

sevmardi sevmardi Joined 19 Jun 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.