<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Sebastian</title>
    <description>The latest articles on Forem by Sebastian (@seb_sebsn).</description>
    <link>https://forem.com/seb_sebsn</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F614208%2F5e942c5b-a097-4ded-8947-19a1d7b5ce0a.png</url>
      <title>Forem: Sebastian</title>
      <link>https://forem.com/seb_sebsn</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/seb_sebsn"/>
    <language>en</language>
    <item>
      <title>The ultimate guide to php artisan tinker</title>
      <dc:creator>Sebastian</dc:creator>
      <pubDate>Wed, 05 May 2021 09:49:05 +0000</pubDate>
      <link>https://forem.com/beyondcode/the-ultimate-guide-to-php-artisan-tinker-3pdi</link>
      <guid>https://forem.com/beyondcode/the-ultimate-guide-to-php-artisan-tinker-3pdi</guid>
      <description>&lt;h2&gt;
  
  
  What is tinker?
&lt;/h2&gt;

&lt;p&gt;This post is dedicated to an underappreciated component of Laravel – the tinker command that you can run with &lt;code&gt;php artisan tinker&lt;/code&gt;. The command is built into every Laravel application and you can use it to run code within the context of your application. Let's explore what this means.&lt;/p&gt;

&lt;p&gt;Tinker is a REPL (read-eval-print loop) on top of &lt;a href="https://psysh.org/"&gt;PsySH&lt;/a&gt;. It takes your command line input, evaluates it and prints the output to the console. So instead of using your database management tool and writing an SQL query to get the amount of users in your database, you can simply run &lt;code&gt;App\User::count()&lt;/code&gt; and get &lt;code&gt;=&amp;gt; 10&lt;/code&gt; as the result.&lt;/p&gt;

&lt;h2&gt;
  
  
  How does it work?
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;php artisan tinker&lt;/code&gt; command bootstraps your application and waits for new commands when this finishes. It keeps the application state until you close leave the tinker command with &lt;code&gt;exit&lt;/code&gt;, &lt;code&gt;Ctrl+C&lt;/code&gt; or close the terminal. This means that you have to initialize a new tinker session every time you change your code to get the latest version – but it allows you to define new variables that are accessible until you end the tinker session.&lt;/p&gt;

&lt;h2&gt;
  
  
  When do you need it?
&lt;/h2&gt;

&lt;p&gt;All the time! We use tinker in development and on production for many scenarios. The most common use case is accessing the database via Laravel Eloquent Models. &lt;/p&gt;

&lt;h3&gt;
  
  
  Use Eloquent Models with tinker
&lt;/h3&gt;

&lt;p&gt;Tinker is very convenient if you want to create test data in development – simply create two new test users with their factory.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;User::factory&lt;span class="o"&gt;()&lt;/span&gt;-&amp;gt;count&lt;span class="o"&gt;(&lt;/span&gt;2&lt;span class="o"&gt;)&lt;/span&gt;-&amp;gt;create&lt;span class="o"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command uses the model factory for users and creates two new users. Your output of this command should look similar to this – with different usernames and emails.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; Illuminate&lt;span class="se"&gt;\D&lt;/span&gt;atabase&lt;span class="se"&gt;\E&lt;/span&gt;loquent&lt;span class="se"&gt;\C&lt;/span&gt;ollection &lt;span class="o"&gt;{&lt;/span&gt;&lt;span class="c"&gt;#4612&lt;/span&gt;
     all: &lt;span class="o"&gt;[&lt;/span&gt;
       App&lt;span class="se"&gt;\M&lt;/span&gt;odels&lt;span class="se"&gt;\U&lt;/span&gt;ser &lt;span class="o"&gt;{&lt;/span&gt;&lt;span class="c"&gt;#4616&lt;/span&gt;
         gender: &lt;span class="s2"&gt;"male"&lt;/span&gt;,
         firstname: &lt;span class="s2"&gt;"Orie"&lt;/span&gt;,
         lastname: &lt;span class="s2"&gt;"Rath"&lt;/span&gt;,
         email: &lt;span class="s2"&gt;"jocelyn.schuster@example.org"&lt;/span&gt;,
       &lt;span class="o"&gt;}&lt;/span&gt;,
       App&lt;span class="se"&gt;\M&lt;/span&gt;odels&lt;span class="se"&gt;\U&lt;/span&gt;ser &lt;span class="o"&gt;{&lt;/span&gt;&lt;span class="c"&gt;#4620&lt;/span&gt;
         gender: &lt;span class="s2"&gt;"female"&lt;/span&gt;,
         firstname: &lt;span class="s2"&gt;"Karlie"&lt;/span&gt;,
         lastname: &lt;span class="s2"&gt;"Ruecker"&lt;/span&gt;,
         email: &lt;span class="s2"&gt;"trycia.koelpin@example.org"&lt;/span&gt;,
       &lt;span class="o"&gt;}&lt;/span&gt;,
     &lt;span class="o"&gt;]&lt;/span&gt;,
   &lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's change the last name of the second user with php artisan tinker (type and confirm every line separately)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; User::where&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'email'&lt;/span&gt;, &lt;span class="s1"&gt;'trycia.koelpin@example.org'&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;-&amp;gt;first&lt;span class="o"&gt;()&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nv"&gt;$user&lt;/span&gt;-&amp;gt;lastname &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'Smith'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nv"&gt;$user&lt;/span&gt;-&amp;gt;save&lt;span class="o"&gt;()&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If this works, tinker returns &lt;code&gt;=&amp;gt; true&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;If you don't want to run multiple commands and the model supports mass assignments, you can run this in a single line.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;User::where&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'email'&lt;/span&gt;, &lt;span class="s1"&gt;'trycia.koelpin@example.org'&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;-&amp;gt;first&lt;span class="o"&gt;()&lt;/span&gt;-&amp;gt;update&lt;span class="o"&gt;([&lt;/span&gt;&lt;span class="s1"&gt;'lastname'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'Carlson'&lt;/span&gt;&lt;span class="o"&gt;])&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are no limits what you can do with tinker as long as you are able to squeeze your code into one line or can execute the code line by line. If you want to push this to the next level, you can use &lt;a href="https://tinkerwell.app"&gt;Tinkerwell&lt;/a&gt; to write multiple lines of code and run them at once. This is very handy if you work with Laravel Collections or use loops – or simply like well-formatted code.&lt;/p&gt;

&lt;p&gt;With tinker, you have access to all PHP functions and helpers that Laravel provides. &lt;/p&gt;

&lt;h3&gt;
  
  
  Generate UUIDs
&lt;/h3&gt;

&lt;p&gt;Instead of googling for an internet service that lets your generate UUIDs, you can use tinker and generate one with the &lt;a href="https://laravel.com/docs/8.x/helpers#method-str-uuid"&gt;Laravel Str helper&lt;/a&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;Str::uuid&lt;span class="o"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This instantly generates an UUID for you.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; Ramsey&lt;span class="se"&gt;\U&lt;/span&gt;uid&lt;span class="se"&gt;\L&lt;/span&gt;azy&lt;span class="se"&gt;\L&lt;/span&gt;azyUuidFromString &lt;span class="o"&gt;{&lt;/span&gt;&lt;span class="c"&gt;#4632&lt;/span&gt;
     uuid: &lt;span class="s2"&gt;"a1d56de1-455d-4275-8812-c3e28d45428e"&lt;/span&gt;,
   &lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Create a slug from a headline
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;Str::slug&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'The ultimate guide to php artisan tinker'&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;"the-ultimate-guide-to-php-artisan-tinker"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Get the date of the first day of the week in 3 months
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;now&lt;span class="o"&gt;()&lt;/span&gt;-&amp;gt;addMonths&lt;span class="o"&gt;(&lt;/span&gt;3&lt;span class="o"&gt;)&lt;/span&gt;-&amp;gt;startOfWeek&lt;span class="o"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; Illuminate&lt;span class="se"&gt;\S&lt;/span&gt;upport&lt;span class="se"&gt;\C&lt;/span&gt;arbon @1627862400 &lt;span class="o"&gt;{&lt;/span&gt;&lt;span class="c"&gt;#5507&lt;/span&gt;
     &lt;span class="nb"&gt;date&lt;/span&gt;: 2021-08-02 00:00:00.0 UTC &lt;span class="o"&gt;(&lt;/span&gt;+00:00&lt;span class="o"&gt;)&lt;/span&gt;,
   &lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Base64 encode and decode
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;base64_encode&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'The ultimate guide to php artisan tinker'&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
base64_decode&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'VGhlIHVsdGltYXRlIGd1aWRlIHRvIHBocCBhcnRpc2FuIHRpbmtlcg=='&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;"VGhlIHVsdGltYXRlIGd1aWRlIHRvIHBocCBhcnRpc2FuIHRpbmtlcg=="&lt;/span&gt;
&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;"The ultimate guide to php artisan tinker"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Ok, you get it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Dispatch jobs
&lt;/h3&gt;

&lt;p&gt;You can dispatch jobs with tinker and add data to this job too. This is a real time-saver when you develop a new job and there are multiple steps involved to trigger the job. Imagine that you have a job that fulfills an order in an ecommerce system. You can either place many orders to test the job or simply dispatch it via tinker. Remember to restart your tinker session if you change the code of the job.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$order&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; new Order&lt;span class="o"&gt;([&lt;/span&gt;&lt;span class="s1"&gt;'invoice_id'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; 12345, &lt;span class="s1"&gt;'item'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'Tinker Fangirl Mug'&lt;/span&gt;&lt;span class="o"&gt;])&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
dispatch&lt;span class="o"&gt;(&lt;/span&gt;new OrderPlacedJob&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$order&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Send Notifications
&lt;/h3&gt;

&lt;p&gt;Similar to Jobs, you can also send Laravel Notifications &amp;amp; Mailables via tinker. You are in the context of your application and can send them with a simple call.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="o"&gt;(&lt;/span&gt;new User&lt;span class="o"&gt;)&lt;/span&gt;-&amp;gt;notify&lt;span class="o"&gt;(&lt;/span&gt;new InvoicePaid&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$invoice&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Check your current Laravel version
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;composer.json&lt;/code&gt; file of your application contains the major and minor version of your Laravel application. If you want to know which Laravel version you use exactly, you can get this via &lt;code&gt;app()-&amp;gt;version()&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;app&lt;span class="o"&gt;()&lt;/span&gt;-&amp;gt;version&lt;span class="o"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;"8.24.0"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Tinkerwell – php artisan tinker on steroids
&lt;/h2&gt;

&lt;p&gt;Tinker is a powerful tool for all Laravel developers but you can push this to a next level. &lt;a href="https://tinkerwell.app"&gt;Tinkerwell&lt;/a&gt; is a code editor for macOS, Windows and Linux that is entirely dedicated to tinker. It comes with multiline support, SSH connections and allows you to tinker with multiple applications at the same time. &lt;/p&gt;

&lt;p&gt;With Tinkerwell, there is no need to reload a tinker session, as Tinkerwell evaluates the current state of your code and does not keep the application state. &lt;/p&gt;

&lt;h3&gt;
  
  
  Using Collections
&lt;/h3&gt;

&lt;p&gt;The multiline support makes it easy to use Laravel Collections or foreach loops in tinker. To continue with an example, you can update multiple users at the same time by using collections on the Eloquent result.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;App\User::whereNull('confirmed_at')
  -&amp;gt;get()
  -&amp;gt;each(function ($user) {
    $user-&amp;gt;update([
      'confirmed_at' =&amp;gt; $user-&amp;gt;created_at-&amp;gt;addDay(),
    ]);
  });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Testing APIs
&lt;/h3&gt;

&lt;p&gt;When working with APIs, the &lt;code&gt;Http&lt;/code&gt; facade of Laravel provides a fluent interface to send &lt;code&gt;GET&lt;/code&gt; and &lt;code&gt;POST&lt;/code&gt; requests to an API. You can use this to access the API and see how the data of this API looks or send data to the API before you implement the API directly in your application.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$apiKey = 'Your-Forge-API-Key';

$response = Http::withHeaders([
    'accept' =&amp;gt; 'application/json'
  ])
  -&amp;gt;withToken($apiKey)
  -&amp;gt;get('https://forge.laravel.com/api/v1/servers')
  -&amp;gt;json();

collect($response['servers'])-&amp;gt;pluck('name');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;=&amp;gt; Illuminate\Support\Collection {#1046
     all: [
       "HELO-cloud",
       "bc-dev",
       "bc-prod-01",
       "bc-prod-02",
       "bc-prod-03",
       "bc-website",
       "expose",
       "expose-eu-1",
       ...
     ],
   }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://tinkerwell.app"&gt;Tinkerwell&lt;/a&gt; is a mighty desktop application for every Laravel developer and more than 7,000 developers are already using Tinkerwell, including the Laravel team itself.&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>tinker</category>
      <category>tooling</category>
      <category>php</category>
    </item>
    <item>
      <title>How to get the raw SQL query from the Laravel Query Builder</title>
      <dc:creator>Sebastian</dc:creator>
      <pubDate>Tue, 04 May 2021 08:10:25 +0000</pubDate>
      <link>https://forem.com/beyondcode/how-to-get-the-raw-sql-query-from-the-laravel-query-builder-5hcj</link>
      <guid>https://forem.com/beyondcode/how-to-get-the-raw-sql-query-from-the-laravel-query-builder-5hcj</guid>
      <description>&lt;p&gt;Sometimes, you ask yourself how you can get the Laravel query builder to output its raw SQL query as a string. Luckily, there are multiple ways how to get this raw query.&lt;/p&gt;

&lt;h2&gt;
  
  
  Using Laravel Eloquent methods
&lt;/h2&gt;

&lt;p&gt;The first method to get the query of an Eloquent call is by using the &lt;code&gt;toSql()&lt;/code&gt; method. This method returns the query without running it – good if you don't want to alter data and only get the query – but this method doesn't show the whole query if your query is more complex or if there are sub-queries.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
App\User::query()
    -&amp;gt;where('created_at', '&amp;lt;', now()-&amp;gt;subYear())
    -&amp;gt;with('assignedApps', 'courses')
    -&amp;gt;orderBy('email', 'asc')
    -&amp;gt;limit(5)
    -&amp;gt;toSql();

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;select * from `users` where `created_at` &amp;lt; ? order by `email` asc limit 5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Using the Laravel Query Log
&lt;/h2&gt;

&lt;p&gt;The second method is the Laravel query log that collects all queries within a request. You can enable this log, run your query and dump the output.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DB::enableQueryLog();

App\User::query()
    -&amp;gt;where('created_at', '&amp;lt;', now()-&amp;gt;subYear())
    -&amp;gt;with('assignedApps', 'courses')
    -&amp;gt;orderBy('email', 'asc')
    -&amp;gt;limit(5)
    -&amp;gt;get();

dd(DB::getQueryLog());
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This code leads to the output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;array:3 [▼
  0 =&amp;gt; array:3 [▼
    "query" =&amp;gt; "select * from `users` where `created_at` &amp;lt; ? order by `email` asc limit 5"
    "bindings" =&amp;gt; array:1 [▼
      0 =&amp;gt; Illuminate\Support\Carbon @1588525477 {#1595 ▶}
    ]
    "time" =&amp;gt; 7.97
  ]
  1 =&amp;gt; array:3 [▼
    "query" =&amp;gt; "select `apps`.*, `user_apps`.`user_id` as `pivot_user_id`, `user_apps`.`app_id` as `pivot_app_id`, `user_apps`.`created_at` as `pivot_created_at`, `user_apps`.` ▶"
    "bindings" =&amp;gt; []
    "time" =&amp;gt; 2.81
  ]
  2 =&amp;gt; array:3 [▼
    "query" =&amp;gt; "select `courses`.*, `user_courses`.`user_id` as `pivot_user_id`, `user_courses`.`course_id` as `pivot_course_id`, `user_courses`.`created_at` as `pivot_created_ ▶"
    "bindings" =&amp;gt; []
    "time" =&amp;gt; 0.54
  ]
]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This gives you detailed information about the executed query and their execution time. If the query has data bindings, the query logs lists them and makes it easy to check your data,  &lt;/p&gt;

&lt;h2&gt;
  
  
  Digging deeper
&lt;/h2&gt;

&lt;p&gt;While you can get the raw SQL query with the methods above, there are situations where you debug the runtime or memory allocation of a query and want to know why it takes longer than expected. &lt;/p&gt;

&lt;h3&gt;
  
  
  Using Tinkerwell
&lt;/h3&gt;

&lt;p&gt;If you write your query in &lt;a href="https://tinkerwell.app"&gt;Tinkerwell&lt;/a&gt; anyway, you can highlight the Eloquent query and press &lt;code&gt;Cmd+Shift+R&lt;/code&gt; (or &lt;code&gt;Ctrl+Shift+R&lt;/code&gt; if you are on Windows) and Tinkerwell profiles the query directly in the editor. You see all executed queries, the time that the query run, the memory consumption and even the peak for your memory. This is super useful if you debug server crashes or want to know how much memory your application server needs.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://beyondco.de/img-blog/tinkerwell/raw-query-output.png?1"&gt;&lt;br&gt;
    &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--GLG6Mkee--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://beyondco.de/img-blog/tinkerwell/raw-query-output.png%3F1"&gt;&lt;br&gt;
&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There are multiple ways how to get the raw SQL query for an Eloquent call – we are using Tinkerwell daily and so this is our natural way to debug queries.&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>eloquent</category>
      <category>mysql</category>
      <category>query</category>
    </item>
  </channel>
</rss>
