Overview
EloquentTools automatically generates CRUD (Create, Read, Update, Delete, Query) tools for your Eloquent models, allowing LLMs to interact with your database through natural language.Quick Start
EloquentTools::crud()
Generates a complete set of CRUD tools for a given Eloquent model.Parameters
The fully qualified class name of the Eloquent model
Include the query tool for searching and filtering records
Include the get tool for retrieving a single record by ID
Include the create tool for creating new records
Include the update tool for updating existing records
Include the delete tool for deleting records
Array of relationships to eager load with queries
Returns
Returns an array of tool instances that can be passed to thetools() method.
Model Setup
Your Eloquent model must implement theHasMagic interface and provide a schema:
HasMagic Interface
Returns the JSON schema for the model.Parameters:
$creating(bool) - Whether the schema is for creating (true) or updating (false) records
- An object schema with
type,properties, and optionallyrequiredfields
Generated Tools
Query Tool
Searches and filters model records. Tool Name:query_{model_plural} (e.g., query_products)
Parameters:
Array of filter objects to applyEach filter has:
field(string) - The field to filter byvalue(string|array) - The value to filter byoperator(string) - Comparison operator:=,!=,<,<=,>,>=,like
Column to order results by
Sort direction:
asc or descMaximum number of records to return (1-100)
Number of records to skip
Get Tool
Retrieves a single record by ID. Tool Name:get_{model_plural} (e.g., get_products)
Parameters:
The ID of the record to retrieve. Type depends on your model’s key type (supports integer, string, and UUID)
Create Tool
Creates a new record. Tool Name:create_{model_plural} (e.g., create_products)
Parameters:
Object containing the model data according to the schema defined in
getMagicSchema(creating: true)Update Tool
Updates an existing record. Tool Name:update_{model_plural} (e.g., update_products)
Parameters:
The ID of the record to update
Object containing the fields to update according to the schema defined in
getMagicSchema(creating: false)Delete Tool
Deletes a record permanently. Tool Name:delete_{model_plural} (e.g., delete_products)
Parameters:
The ID of the record to delete
Selective Tools
You can choose which tools to generate:Eager Loading Relationships
Eager load relationships to avoid N+1 queries:Multiple Models
You can create tools for multiple models:Error Handling
Eloquent tools handle common errors:- Record not found: Returns a Magic error with code
not_found - Invalid arguments: Throws
InvalidArgumentException - Validation errors: Returns Laravel validation errors