Are there laravel method like in_array? - laravel

I try to use methods in https://laravel.com/docs/9.x/helpers instead of old php functions I used for many
year. I did not find any laravel method like in_array.
I tried to use Str::contains , but failed it. Seems different ?
So any replacement of in_array ?
Thanks in advance!

Using in_array seems perfectly acceptable and if it gets the job done for you and is not causing issues then stick with it.
That aside, there is the Arr::has() helper, or alternatively, you could convert your array to a collection and then use the contains() method, however that just uses in_array behind the scenes anyway.

Related

Difference between ->withIndex($index) and ->with('index',$index) in Laravel

I just came accros a variable defined as ->with('index',$index) and I was wandering, is there a difference between this format and this one I usually use :
->withIndex($index)?
If yes, which one should be promote ?
The only difference is that with() can accept an array with variables, for example ->with(['index' => $index]); and by ->withParamName(); you pass only one variable. Behind the scenes Laravel calls a magic method and transforms the withIndex($index) to with('index',$index), so you can use either. Just make sure you don't use a reserved method name like withHeaders, withCookie, withInput, etc.

Paginate causes crashing in Laravel 4

I am learning about Laravel 4 and I'm trying its pagination. I created a simple query to test the pagination, yet it always end up hanging. If I use get(), it works fine, but when I replace get() with paginate(), it hangs. Here is my code:
DB::table("samp_tbl")
->select("id","activity")
->whereNull("deleted_at")
->orderBy("id","desc")
->paginate(5);
Could someone tell me what's wrong with my code?
In case anyone else comes across this issue it's because you are using orderBy. In the Note: area on this page http://laravel.com/docs/4.2/pagination#usage it explains that laravel has issues with groupBy. However I also would assume this would go for orderBy as well. Writing a custom query would be recommended in your case.
create a model for your database and it will work fine

equivalent function for $this->_ci_cached_vars in CI Helpers

I'm creating a custom helper in codeigniter. There is an instance where I check if certain parameter is passed to view.
In view, i can get all the passed variables by using this function:
$this->_ci_cached_vars
but it returns blank when used in the custom helper.
Is there any equivalent function of this that can be used in the helper?
Thanks in advance.
_ci_cached_vars is a property of the Loader class. So something like this should work (untested):
$CI =& get_instance();
$vars = $CI->load->_ci_cached_vars;
You can use $GLOBALS['CI']->load->get_var('your_key_here') tested in CI 2.1.2
I'm not sure if older versions of CodeIgniter support this, but in the v3 release the Loader class has a public method get_vars() that allows you to read the value of _ci_cached_vars.
Although this question is very old it's the first hit on Google that I found while searching for this problem. I hope this post helps someone out who follows the same path on Google as I did! :)

Laravel 4: Fluent changes?

I read that Laravel 4 is not going to change much on the surface, saying that much of the old code would be compatible.
I try to use Fluent's insert_get_id like it says in the docs, but the function doesn't exist.
Am I doing it wrong? If not, are there more changes in Fluent and / or Eloquent?
Laravel 4 has been changed to become PSR-1 complaint and as such is now camelCased instead of snake_cased. Try changing to insertGetId().
I believe functions went from underscores to camelCase... try insertGetId

Relational entity too big to debug [duplicate]

I have around 40 entities and many bidirectional relationships.
Whenever i use var_dump($user) or any entity my browser gets loaded with too much data of arrays and variables then it just crashed.
i want to whats the problem.
The data is being inserted fine. Can i cause issue in production.
Replace var_dump() with the debug method dump() provided by Doctrine Common.
\Doctrine\Common\Util\Debug::dump($user);
It works for single objects and Doctrine collections and should prevent browser displaying issues you are having.
well formatted :
echo '<pre>';
\Doctrine\Common\Util\Debug::dump($user, $recurciveLevelToDisplay);
echo '</pre>';
Simple and easy example.
var_dump(serialize($Object));
Symfony < 2.6
You can use \Doctrine\Common\Util\Debug::dump($variable, $depth); it displays doctrine output without the proxy information.
Symfony > 2.6
If you are using symfony 2.6 or more, I strongly advice you to use dump().
It shows a well formated and colored output, and you can dynamically expend/hide rows.
The problem is that in a bidirectional relationship both entities have a link to each other, so while displaying entity1 var_dump will also have to print all properties of entity2, which include entity1 itself giving you a loop.
The get_object_vars() improve the visualization too.
echo "<pre>";
\Doctrine\Common\Util\Debug::dump(get_object_vars($user));
With Symfony 2.6 you can now just use dump($var) in your controller and {{ dump(var) }} in twig.
Make sure to add this to your AppKernal.php file, in the array('dev', 'test') section.
$bundles[] = new Symfony\Bundle\DebugBundle\DebugBundle();
use dump($user) and you can see perfect result in Symfony Profiler! good luck
Just use
echo serialize($user);

Resources