I'm trying to connect a Laravel project to an existing database.
I have followed the Eloquent Model Conventions; however, I am still hitting the following error:
Illuminate \ Database \ QueryException (2002) SQLSTATE[HY000] [2002] No such file or directory (SQL: select * from
rents
)
Here is my code:
web.php
Route::get('/', [
'uses' => 'RentsController@index'
]);
RentsController.php
<?php
namespace App\Http\Controllers;
use App\Rent;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class RentsController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$rents = Rent::all();
return view('welcome', ['rents' => $rents]);
}
...
}
Rent.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Rent extends Model {
protected $table = 'rents';
}
welcome.blade.php
<!doctype html>
<html>
<head>
</head>
<body>
<ul>
@foreach ($rents as $rent)
<li>{{ $rent->title }}</li>
@endforeach
</ul>
</body>
</html>
* One thing that may be the problem is that I am running the page locally (php artisan serve), while the database is real and online. Would that cause the issue? If so, any idea how to fix that? *
Any idea what could be the problem? I have the .env file setup correctly, as it works on another page. However, on this one, it does not seem to be able to find the 'rents' table.
Thanks!!