Can't Select in Laravel using Eloquent and SQL Server Database

Viewed 569

Greatings, I'm very new in laravel. Today I make a project with Laravel and SQL Server for the database. I can connect to that DB, and I can make the default migration using user migration. but when I select the table user and return it into json the result is blank. That's my select

    {
        //
        $dataUser = User::all();
        return response()->json($dataUser);
    }

and the result only [] on browser, I use Postman too, but the result was same. I have 2 record on DB.

and then wan't to do with different table, I'm use existing table, the table name is Purchases, the table have around 100 record. and that's my model for the table

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Purchases extends Model
{
    protected $table = 'Purchases';
    protected $primaryKey = 'PurchaseID';
    protected $guarded = ['PurchaseID'];
    const CREATED_AT = 'CreatedTime';
    const UPDATED_AT = 'LastModifiedTime';
}
?>

I use the same code in the purchaseController

namespace App\Http\Controllers;
use App\Purchases;

public function index()
    {
        //
        $dataPurchase = Purchases::all();
        return response()->json($dataPurchase);
    }

but when I load the browser that return error like this

Illuminate\Database\QueryException
SQLSTATE[42S02]: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Invalid object name 'purchases'. (SQL: select * from [purchases])

in my SSMS, my table is [dbo].[purchases] and the migration is [dbo].[users]

Please help me to resolve that. Thankyou

EDIT: I think I have a problem on my connection, because I only register the php PDO driver on apache, and change the laravel config. I can using migration, but when I use faker, the data that's showing on tinker not save on DB users

1 Answers

your code perfectly worked in my project. check your database connection in env or databse.php

Related