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])
productstable have? You may need to specify that as well (e.g[dbo].[products]) if the schema in question is not the database default.SELECT COUNT(*) FROM [forge].[dbo].[products]dd((new Product)->getConnection()->getName(), config('database.connections.sqlsrv'))to verify the connection name and the connection details are correct.