Back to Engineering Notes
Laravel ConceptsEngineering Note

6. Custom Input (Request) & API Resource (Response)

I use Custom Input (Form Request) and API Resources to clearly control both:

🧠 Custom Input (Request) & API Resource (Response)

I use Custom Input (Form Request) and API Resources to clearly control both:

incoming data
outgoing responses

👉 This keeps APIs clean, consistent, and maintainable


🎯 Simple Idea

Custom Input → controls what comes into the system
Resource → controls what goes out of the system

🧩 Custom Input (Request)

Used at the entry point.

Handles:

validation
authorization
input formatting

Example:

plain text
public function rules()
{
    return [
        'email' => 'required|email',
        'name' => 'required|string'
    ];
}

👉 Controller receives clean and validated data


🧩 Resource (Response)

Used at the exit point.

Handles:

formatting output
hiding unnecessary fields
structuring API response

Example:

plain text
public function toArray($request)
{
    return [
        'id' => $this->id,
        'name' => $this->name,
        'email' => $this->email,
    ];
}

👉 Client receives controlled and consistent data


🧠 Why I Use This

separates input and output concerns
avoids messy controllers
ensures consistent API structure
prevents exposing internal data

📌 Practical Rule

> validate at the boundary (Request), format at the boundary (Resource)


💬 Summary

I use:

Custom Input → to control incoming data
Resource → to control outgoing data

👉 This helps build APIs that are safe, clean, and easy to maintain 👍