SajjadAli97's avatar

Laravel elquent existing table issue.

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 my .env file:

APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:RVOuXWd9zPWrrdIEIHoezPBrfQqHPTihq4WUgXk3xjU=
APP_DEBUG=true
APP_LOG_LEVEL=debug
APP_URL=http://localhost

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=test
DB_USERNAME=root
DB_PASSWORD=shetty@123
BROADCAST_DRIVER=log
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null

PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=

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', 'test'),
            'username' => env('DB_USERNAME', 'root'),
            'password' => env('DB_PASSWORD', 'shetty@123'),
            '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', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'charset' => 'utf8',
            'prefix' => '',
            'schema' => 'public',
            'sslmode' => 'prefer',
        ],

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

    ],


    'migrations' => 'migrations',


    'redis' => [

        'client' => 'predis',

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

    ],

];
0 likes
1 reply
tisuchi's avatar
tisuchi
Best Answer
Level 70

@sajjadali97

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

At the top of your routes file includes 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.

7 likes

Please or to participate in this conversation.