MVS
MVS

Reputation: 118

Unable to use existing tables for laravel Eloquent

So I have been trying to follow some online links for understanding how eloquent can be used in defining relationships in table.What I would actually want to try is to use my existing tables for creating models and then define relationships using eloquent. Now I am not using migrations, since I had already created my tables and have configured the connection and other settings(mysql) in the database.php as well as .env file.

Follwing is my Article modal:

<?php

namespace App;


use Illuminate\Database\Eloquent\Model;


class Article extends Model
{
    
 protected $table = 'articles';

}

Since I don't make use of any migrations, I have specified my table name(articles), where I have already inserted some data. Now just to check whether the modal works properly, I tried giving a route in web.php as follows:

<?php

Route::get('/', function () {
  
    echo Article::all();
});

But now when try running the same on the server, it gives me an error saying "Class Article not found".What seems to be the issue here?

Following is the database.php file:

<?php

return [

    
    'default' => env('DB_CONNECTION', 'mysql'),
    'connections' => [

        'sqlite' => [
            'driver' => 'sqlite',
            'database' => env('DB_DATABASE', database_path('database.sqlite')),
            'prefix' => '',
        ],

        'mysql' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'xxxx'),
            'username' => env('DB_USERNAME', 'xxxx'),
            'password' => env('DB_PASSWORD', 'xxxx'),
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'strict' => true,
            'engine' => null,
        ],

        'pgsql' => [
            'driver' => 'pgsql',
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '5432'),
            'database' => env('DB_DATABASE', 'xxxx'),
            'username' => env('DB_USERNAME', 'xxxx'),
            'password' => env('DB_PASSWORD', 'xxxx'),
            'charset' => 'utf8',
            'prefix' => '',
            'schema' => 'public',
            'sslmode' => 'prefer',
        ],

        'sqlsrv' => [
            'driver' => 'sqlsrv',
            'host' => env('DB_HOST', 'localhost'),
            'port' => env('DB_PORT', '1433'),
            'database' => env('DB_DATABASE', 'xxxx'),
            'username' => env('DB_USERNAME', 'xxxx'),
            'password' => env('DB_PASSWORD', 'xxxx'),
            'charset' => 'utf8',
            'prefix' => '',
        ],

    ],


    'migrations' => 'migrations',


    'redis' => [

        'client' => 'predis',

        'default' => [
            'host' => env('REDIS_HOST', '127.0.0.1'),
            'password' => env('REDIS_PASSWORD', 'xxxx'),
            'port' => env('REDIS_PORT', 6379),
            'database' => 0,
        ],

    ],

];

Upvotes: 0

Views: 280

Answers (1)

Polaris
Polaris

Reputation: 1249

The issue is that your Article class is in a different namespace than your routes file.

At the top of your routes file include this statement:

use App\Article;

You can now reference the Article class in your route closure as Article::method();

Or alternatively, precede the call to all() as so:

\App\Article:all();

But make sure you do one or the other, not both as it is unnecessary.

Upvotes: 1

Related Questions