Easily list data and export it as CSV file directly from your PHP application
The Table Mode of Tinkerwell allows you to see the results of your code in a table and export it to a CSV file. This is extra useful if you pair it with complex database queries for reports that you don’t generate that often or just on demand – no fiddling around with SQL but simply using the ORM of your application.
For the sake of simplicity, we’re using Laravel to explain how you can export data as CSV file and to show how it works but your can head to the other examples for Symfony and WordPress if you use them more often. So don’t skip the Laravel examples if you aren’t using the framework but go through them for a better understanding of the Table Mode of Tinkerwell.
Tinkerwell makes exporting data as CSV file easy and works with any PHP application.
Laravel Eloquent is a powerful ORM that has a fluent syntax. In this example, we query our Beyond Code database to get a list of all users who bought at least one video course and list them with their course so that we know which email address has access to a specific course.
Laravel takes care out the relation between the User and Course models automatically and makes it easy for us to select the elements from the collections via the mapping method.
User::with("courses") ->whereHas("courses") ->limit(10) ->get() ->map(function (User $user) { return $user->courses->map(function (App\Course $course) use ($user) { return [ "email" => $user->email, "course" => $course->title ]; }); }) ->flatten(1);
Tinkerwell allows you to get a lists of all posts of your WordPress site and other things easily.
In this example, we’re using the wp_get_recent_posts()
method to get an array of the recents
posts and display it in the Table View. You can also use methods like get_posts()
but they
require a transformation into a plain array to get displayed properly.
The Table View is only able to display arrays that have named keys like ID
and post_title
in the
example but can’t convert WordPress objects (i.e. WP_Post
) to a list view automatically.
array_map(function($item) { return [ $item['ID'], $item['post_date'], $item['post_title'], ];}, wp_get_recent_posts());
Symfony uses Doctrine as ORM to fetch database records and it works similar to the Laravel example above. You can get the EntityManager for your model – in our example we want to fetch some users and display them and their email addresses in the Table View. You can easily use this as a base example and extend it for your more complex application.
Tinkerwell exposes the $container
and $kernel
variables to the editor to make interacting with
your application easier.
$users = $container ->get('doctrine.orm.entity_manager') ->getRepository(App\Entity\User::class) ->findAll(); array_map(function ($user) { return [ 'full_name' => $user->getFullName(), 'email' => $user->getEmail(), ];}, $users);
As you can see, the Table View mode is helpful when running custom reports against your application and don’t want to fiddle around with MySQL queries or adding code to your codebase for a task that only happens once.
If you find yourself in a situation where you need to export data as CSV file, you can use the Table Mode for this task – and save the code as a snippet for later use. This is quicker than writing a feature for your application, especially if you don’t need it that often or only use it with changing parameters in a development context.
“The ability to SSH directly into a server and run diagnostics directly from Tinkerwell is insanely amazing! Can't imagine doing development without it at this point.”Jake Bennett
Director of Technology and Innovation
“I open Tinkerwell together with my IDE to have an instant feedback loop when prototyping an implementation. I can't imagine a life without it now.”Kamau Wanyee
FullStack Developer
The must-have companion to your favorite IDE. Quickly iterate on PHP code within the context of your web application.
Buy now Learn more