5

Following code is working if I'm using MySQL. When i switch to MS SQLServer i get error. I have already enabled the pdo sqlsrv driver. My database has a table called products, But it is showing invalid object name.

Product.php (Model)

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    protected $connection = 'sqlsrv';
}
?>

database.php

'sqlsrv' => [
        'driver'   => 'sqlsrv',
        'host'     => env('DB_EXT_HOST', 'localhost'),
        'database' => env('DB_EXT_DATABASE', 'forge'),
        'username' => env('DB_EXT_USERNAME', 'forge'),
        'password' => env('DB_EXT_PASSWORD', ''),
        'charset'  => 'utf8',
        'prefix'   => '',
    ]

Controller:

<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Product;
use App\Http\Requests;

class APIController extends Controller
{
    public function index(Request $request)
    {
        $products = Product::paginate(5);
        return response(array(
            'error' => false,
            'products' =>$products->toArray(),
        ),200);
    }
}
?>

Output:

SQLSTATE[42S02]: [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Invalid object name 'products'. (SQL: select count(*) as aggregate from [products])

4
  • What schema does the products table have? You may need to specify that as well (e.g [dbo].[products]) if the schema in question is not the database default. Commented Dec 22, 2016 at 9:19
  • It is database default only. dbo.products Commented Dec 22, 2016 at 9:28
  • 1
    Run that query manually from your code and reference the table as [forge].[dbo].[products] and see if you get the same error. SELECT COUNT(*) FROM [forge].[dbo].[products] Commented Dec 22, 2016 at 10:46
  • Are you sure Laravel is connecting to the same database that you're looking at? Do a dd((new Product)->getConnection()->getName(), config('database.connections.sqlsrv')) to verify the connection name and the connection details are correct. Commented Dec 23, 2016 at 7:24

2 Answers 2

2

I know I'm very late in party but this could help to other user.

From the documentation:

plural name of the class will be used as the table name unless another name is explicitly specified

So in your case, table name products is different then model product. just add below line into your model:

protected $table = 'products';
Sign up to request clarification or add additional context in comments.

Comments

0

If adding the correct schema doesn't work, it could be a permission issue for your user. I'm guessing you are accessing the database with the "forge" user - does that user have select permission on that table?

You may have to run this to be certain:

GRANT SELECT ON dbo.products TO forge

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.