Peek
  • 👋Welcome to A PeekDebug
  • Getting Started
    • 🛠️Installation
    • ✨Features
    • Support
  • Laravel
    • Installation
    • 🛠️Configuration
    • 🔑Usage
    • 🔗Queries
Powered by GitBook
On this page
  • Using Peek With Laravel
  • See the caller of a function
  • Counting execution times
  • Measuring performance and memory usage
  • Display the class name of an object
  • Showing events
  • #Showing cache events
  • Handling models
  • Displaying mailables
  • Showing which views are rendered
  • Displaying environment variables
  • Showing HTTP client requests
  1. Laravel

Usage

PreviousConfigurationNextQueries

Last updated 4 months ago

Using Peek With Laravel

To display data in Peek, use the peek() function. It accepts anything—strings, arrays, objects, or even complex expressions. Simply pass your data to peek(), and it will appear in the app, making debugging and inspecting your code effortless and intuitive.

    
    peek("A string");

    peek(123);
    
    peek(true);
    
    peek([1, 'text']);

    peek($object);
    

peek accepts multiple arguments. Each argument will be displayed in the Peek app.

peek('as', 'many', 'arguments', 'as', 'you', 'like');

See the caller of a function

Sometimes you want to know where your code is being called. You can quickly determine that by using the caller function.

peek()->caller();

If you want to see the entire backtrace, use the trace (or backtrace).

peek()->trace();

Counting execution times

You can display a count of how many times a piece of code was called using count.

Here's an example:

foreach (range(1, 2) as $i) {
    peek()->count();

    foreach (range(1, 4) as $j) {
        peek()->count();
    }
}

This is how that looks like in Peek.

Optionally, you can pass a name to count. Ray will display a count of how many times a count with that name was executed.

Here's an example:

foreach (range(1, 4) as $i) {
    peek()->count('first');

    foreach (range(1, 2) as $j) {
        peek()->count('first');

        peek()->count('second');
    }
}

You may access the value of a named counter using the counterValue function.

foreach (range(1, 4) as $i) {
    peek()->count('first');

    if (peek()->counterValue('first') === 2) {
        echo "counter value is two!";
    }
}

This is how that looks like in Ray.

Measuring performance and memory usage

You can use the measure function to display runtime and memory usage. When measure is called again, the time between this and previous call is also displayed.

peek()->measure();

sleep(1);

peek()->measure();

sleep(2);

peek()->measure();

The measure call optionally accepts a callable. Peek will output the time needed to run the callable and the maximum memory used.

peek()->measure(function() {
    sleep(5);
});

Display the class name of an object

To quickly send the class name of an object to peek, use the className function.

peek()->className($anObject)

Showing events

You can display all events that are executed by calling showEvents (or events).

peek()->showEvents();

event(new TestEvent());

event(new TestEventWithParameter('my argument'));

To stop showing events, call stopShowingEvents.

peek()->showEvents();

event(new MyEvent()); // this event will be displayed

peek()->stopShowingEvents();

event(new MyOtherEvent()); // this event won't be displayed.

Alternatively, you can pass a callable to showEvents. Only the events fired inside that callable will be displayed in Peek.

event(new MyEvent()); // this event won't be displayed.

peek()->showEvents(function() {
    event(new MyEvent()); // this event will be displayed.
});

event(new MyEvent()); // this event won't be displayed.

Showing jobs

You can display all jobs that are executed by calling showJobs (or jobs).

peek()->showJobs();

dispatch(new TestJob('my-test-job', 'my-test-job2'));

To stop showing jobs, call stopShowingJobs.

peek()->showJobs();

dispatch(new TestJob()); // this job will be displayed

peek()->stopShowingJobs();

dispatch(new MyTestOtherJob()); // this job won't be displayed.

Alternatively, you can pass a callable to showJobs. Only the jobs dispatch inside that callable will be displayed in Peek.

event(new TestJob()); // this job won't be displayed.

peek()->showJobs(function() {
    dispatch(new TestJob()); // this job will be displayed.
});

event(new TestJob()); // this job won't be displayed.

You can display all cache events using showCache

peek()->showCache();

Cache::put('my-key', ['a' => 1]);

Cache::get('my-key');

Cache::get('another-key');

Handling models

Using the model function, you can display the attributes and relations of a model.

peek()->model($user);

The model function can also accept multiple models and even collections.

// all of these models will be displayed in Peek
peek()->model($user, $anotherUser, $yetAnotherUser);

// all models in the collection will be display
peek()->model(User::all());

// all models in all collections will be displayed
peek()->model(User::all(), OtherModel::all());

Displaying mailables

Mails that are sent to the log mailer are automatically shown in peek, you can also display the rendered version of a specific mailable in Ray by passing a mailable to the mailable function.

peek()->mailable(new TestMail());

Showing which views are rendered

You can display all views that are rendered by calling showViews.

peek()->showViews();

// typically you'll do this in a controller
view('welcome', ['name' => 'John Doe'])->render();

Displaying environment variables

You can use the env() method to display all environment variables as loaded from your .env file. You may optionally pass an array of variable names to exclusively display.

peek()->env();

peek()->env(['APP_NAME', 'DB_DATABASE', 'DB_HOSTNAME', 'DB_PORT'])

Showing HTTP client requests

You can display all HTTP client requests and responses using showHttpClientRequests

peek()->showHttpClientRequests();

Http::get('https://example.com/api/users');

To stop showing HTTP client events, call stopShowingHttpClientRequests.

Alternatively, you can pass a callable to showHttpClientRequests. Only the HTTP requests made inside that callable will be displayed in Peek.

Http::get('https://example.com'); // this request won't be displayed.

peek()->showHttpClientRequests(function() {
    Http::get('https://example.com'); // this request will be displayed.
});

Http::get('https://example.com'); // this request won't be displayed.

Showing cache events

🔑
#