Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Request Helper Methods #53646

Open
wants to merge 5 commits into
base: 11.x
Choose a base branch
from

Conversation

amjpdevp
Copy link

This pull request introduces four new helper methods to Laravel's Request class, which are designed to assist developers in debugging request data more efficiently. These methods are:

  • ddAll(): Dumps all request data and halts execution.
  • ddOnly(): Dumps only the specified keys of request data and halts execution.
  • ddExcept(): Dumps all request data except for the specified keys and halts execution.
  • ddKeys(): Dumps the keys of all request data and halts execution.

These methods leverage Laravel's built-in dump() and dd() functions to display request data in a readable format. The key difference is that these new methods make it easier for developers to focus on the exact pieces of data they care about during debugging.

Feature Explanation

1. ddAll()

public function ddAll() {
    $this->dump($this->all());
    exit(1);
}
  • Purpose: Dumps all input data (request data, query parameters, etc.) and stops script execution.
  • Usage: This method is useful when you want to quickly inspect all incoming request data without having to manually specify which pieces of data to dump.
  • Example:
$request->ddAll(); // Dumps all input data and stop the execution.` 

2. ddOnly()

public function ddOnly(...$keys) {
    $this->dump($this->only($keys));
    exit(1);
}
  • Purpose: Dumps only the specific request parameters passed to the method and stops execution.
  • Usage: This method allows you to focus on a subset of request data, making it easier to debug specific fields.
  • Example:
$request->ddOnly('name', 'email'); // Dumps only 'name' and 'email' from the request data.

3. ddExcept()

public function ddExcept(...$keys) {
    $this->dump($this->except($keys));
    exit(1);
}
  • Purpose: Dumps all the request data except for the specified keys and stops execution.
  • Usage: This method is useful when you want to inspect everything in the request data except for certain sensitive or irrelevant fields (e.g., passwords).
  • Example:
$request->ddExcept('password'); // Dumps all request data except for 'password'.

4. ddKeys()

public function ddKeys() {
    $this->dump($this->keys());
    exit(1);
}
  • Purpose: Dumps the keys of the request data and stops execution. This is useful for debugging and understanding the structure of the incoming request.
  • Usage: Use this when you only want to see the field names (keys) in the request.
  • Example:
$request->ddKeys(); // Dumps only the keys of the request data.

Why This is Useful

These new helper functions are useful for developers who frequently need to debug request data during the development process. They streamline the debugging workflow by providing quick and easy access to different subsets of request data.

  • Improved Debugging Efficiency: These functions reduce the need to manually specify which fields to dump or inspect, making it easier to debug specific parts of the request.
  • Cleaner Debugging: By using methods like ddOnly() and ddExcept(), developers can avoid cluttering their output with unnecessary fields, which is especially helpful when dealing with large datasets or sensitive information.
  • Consistency: These helper methods follow the established pattern in Laravel (e.g., dd(), dump()) and provide additional flexibility without introducing significant changes to the core framework.

Benefits to End Users

  • Faster Debugging: The methods allow developers to quickly inspect all or part of the request data without needing to write custom debugging code.
  • Less Clutter: By dumping only the keys or specified data, developers avoid being overwhelmed by irrelevant data during debugging.
  • Improved Developer Experience: Laravel continues to improve its developer experience by adding simple, intuitive helper functions that integrate well with existing functionality.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant