Laravel 4: Fluent changes? - laravel

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

Related

Are there laravel method like in_array?

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.

what happend to getRelatedIds in Laravel 5.4

I was using this function in 5.3 and now when I try to use it in 5.4 like this:
$post->tags()->getRelatedIds();
I am getting errors that function does not exist, I checked the documentation for 5.4 and it's not there anymore.
Anyone knows why this usefull function was removed and what I can do to get all ids from related model?
In Laravel 5.4 and 5.5 the getRelatedIds is replaced by allRelatedIds.
$post->tags()->allRelatedIds();
I can't speak for reasons why it has been removed but if you know the primary key name ahead of time (i.e. all your tables have an id column) you can simply do
$post->tags()->select('id')->pluck('id');
if you want a more generic way you'd need to jump through some hoops
$related = $post->tags();
$post->tags()
->select($related->getQualifiedKeyName())
->pluck($related->getKeyName());

Default blade directives in Laravel/Lumen

Where can I find the default blade directives folder?
I was looking for the #forelse to take as example, but I couldn't find it.
Does anyone know where they are defined in Lumen/Laravel?
note: I'm using Lumen framework, but I think it's quite similar to Laravel in this question, so I'm tagging both.
EDIT:
Due to the imprecision, I'll explain better my intention.
Basically, I'm creating a directive exactly the same as #forelse, but with 2 or 3 further information.
For that reason, I came to ask about the location since I haven't found by myself.
They're defined in the Illuminate\View\Compilers namespace in the BladeCompiler.
See Illuminate/View/Compilers/BladeCompiler.php
Specifically you'll need the compileForelse method if that's the one you want as an example.

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

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