While using the get()
function, most of us always encounter errors. In this shot, let’s look at how to avoid such errors if we eventually encounter them.
The get()
function is used to fetch data from the database after querying with one or two query builders. The get()
will output:
Illuminate\Support\Collection
This class contains the result from the database.
When we chain the get()
method to a null instance.
When we try to access data returned from our view or anywhere without looping it.
use Illuminate\Support\Facades\DB;
$users = DB::table('users')->get();
foreach ($users as $user) {
echo $user->name.',';
}
From the example above, if we remove the foreach()
loop and try to echo
the $user->name
, we would end up getting only the last value of the data from the Collection
instance class. Still, we loop through the $users
variable, and we would be able to get each user in the table. I hope we learned something new.
John, Tom, Harry, Paul, Josh