SajjadAli97
3 days ago

Laravel elquent existing table issue.

Posted 3 days ago by SajjadAli97

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
[email protected]
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', '[email protected]'),
            '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,
        ],

    ],

];

Please sign in or create an account to participate in this conversation.