Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Blindacme's avatar

Calling eloquent model from PHP file in public directory

I have a php file I need stored in my public directory but I need to load eloquent or DB so I can interact with my database. Tried the below but I get a Failed to open file error, can't be found.

$app = require __DIR__ . '../../../bootstrap/app.php';
use Illuminate\Database\Capsule\Manager as DB;
use Illuminate\Database\Eloquent\Model;
use \App\Models\LeadSource;

$lead_source_id = LeadSource::where('number', '=', '9104541234')->pluck('id');
print_r($lead_source_id);

Is there a way I can access the database from a PHP file in the public folder without doing mysqli and entering the DB credentials again?

0 likes
2 replies
Blindacme's avatar

Yes, if I try

$lead_source_id = DB::connection('mysql')->select('select * from lead_sources where number = ?', ['9105551234']);
print_r($lead_source_id);
die();

I get Uncaught Error: Call to a member function getConnection() on null

Please or to participate in this conversation.