In codeigniter 4 there is a Db helper or query builder when i print_r($db->getLastQuery()) it returns whole methods and other information or when echo $db->getLastQuery() it returns last query.
How the same function returns diffrent output at same time ??
Probably because getLastQuery returns object which implements magic method __toString, and with echo variable is casted to string, while print_r just prints raw object.
Related
According to the documentation, I can cast JSONB column as array with Laravel. It's great but I would like to understand how does it work under the hood. Because I don't know if it calls json_decode once or many time in this code (assuming metadata is a JSONB column):
echo $user->metadata['field_a'];
echo $user->metadata['field_b'];
echo $user->metadata['field_c'];
Does Laravel calls json_decode once or three times?
If so should I replace my code with this?
$meta = $user->metadata;
echo $meta['field_a'];
echo $meta['field_b'];
echo $meta['field_c'];
Side note: It's not a question about micro-optimization. Sometimes I do multiple call to JSONB in big loops, so maybe I need to rewrite all.
Laravel will do all casts once when the data comes from the database. All eloquent database functions will use the fill function to set the properties of a model. And this function will set the attributes using the setAttribute function.
The setAttribute function will check for casts and set the properties of the object accordingly. So the json is decoded once and then set as an object or array to the property.
Json strings are casted here in the setAttribute function.
What is the difference between these methods:
find()
findOrFail()
first()
firstOrFail()
get()
list()
toArray()
I've been using them and each one gives a different result and sometimes I need to add toArray() at the end of get() because my function is expecting an array. Won't the other methods produce arrays as well?
find($id) takes an id and returns a single model. If no matching model exist, it returns null.
findOrFail($id) takes an id and returns a single model. If no matching model exist, it throws an error1.
first() returns the first record found in the database. If no matching model exist, it returns null.
firstOrFail() returns the first record found in the database. If no matching model exist, it throws an error1.
get() returns a collection of models matching the query.
pluck($column) returns a collection of just the values in the given column. In previous versions of Laravel this method was called lists.
toArray() converts the model/collection into a simple PHP array.
Note: a collection is a beefed up array. It functions similarly to an array, but has a lot of added functionality, as you can see in the docs.
Unfortunately, PHP doesn't let you use a collection object everywhere you can use an array. For example, using a collection in a foreach loop is ok, put passing it to array_map is not. Similarly, if you type-hint an argument as array, PHP won't let you pass it a collection. Starting in PHP 7.1, there is the iterable typehint, which can be used to accept both arrays and collections.
If you ever want to get a plain array from a collection, call its all() method.
1 The error thrown by the findOrFail and firstOrFail methods is a ModelNotFoundException. If you don't catch this exception yourself, Laravel will respond with a 404, which is what you want most of the time.
Probably things changed but the findorFail method can take 2 arguments: $id and $columns mixed/array params respectively. Passing a second arg is not required. That said, this would work:
$post = Post::findOrFail([1,2], ['title', 'subtitle']);
If one of the $ids fails, the ModelNotFoundException with message 'No query results for model ... ' will be thrown.
Let's assume we have this function
<?php
function run($callback)
{
$callback($some, $params);
}
How to call it?
run($someObject->callback);
or rather
run([$someObject, 'callback']);
The first one seems better to me especially because of code suggestion but in documentation is used array notation.
Why is first one worse than second one?
The array notation is better because the arrow notation doesn't work. Functions in PHP aren't first class objects which can be passed around. Whenever you "pass a function", you must pass it by name; i.e. only its name. To pass an object method, you need to pass the object and the method name using the callable pseudo type notation.
when using the dump utility of symfony, there is a nice reference number that identifies the object such as object: Doctrine\ORM\PersistentCollection {#3491, is there any way that one can get this value(3491) without without using the dump function. That is some_function($object) and then get the same number that symfony dump function would return.
Thanks
I believe it is the spl_object_hash() value.
What is the difference between these methods:
find()
findOrFail()
first()
firstOrFail()
get()
list()
toArray()
I've been using them and each one gives a different result and sometimes I need to add toArray() at the end of get() because my function is expecting an array. Won't the other methods produce arrays as well?
find($id) takes an id and returns a single model. If no matching model exist, it returns null.
findOrFail($id) takes an id and returns a single model. If no matching model exist, it throws an error1.
first() returns the first record found in the database. If no matching model exist, it returns null.
firstOrFail() returns the first record found in the database. If no matching model exist, it throws an error1.
get() returns a collection of models matching the query.
pluck($column) returns a collection of just the values in the given column. In previous versions of Laravel this method was called lists.
toArray() converts the model/collection into a simple PHP array.
Note: a collection is a beefed up array. It functions similarly to an array, but has a lot of added functionality, as you can see in the docs.
Unfortunately, PHP doesn't let you use a collection object everywhere you can use an array. For example, using a collection in a foreach loop is ok, put passing it to array_map is not. Similarly, if you type-hint an argument as array, PHP won't let you pass it a collection. Starting in PHP 7.1, there is the iterable typehint, which can be used to accept both arrays and collections.
If you ever want to get a plain array from a collection, call its all() method.
1 The error thrown by the findOrFail and firstOrFail methods is a ModelNotFoundException. If you don't catch this exception yourself, Laravel will respond with a 404, which is what you want most of the time.
Probably things changed but the findorFail method can take 2 arguments: $id and $columns mixed/array params respectively. Passing a second arg is not required. That said, this would work:
$post = Post::findOrFail([1,2], ['title', 'subtitle']);
If one of the $ids fails, the ModelNotFoundException with message 'No query results for model ... ' will be thrown.