Laravel 5.5 Eloquent not putting quote marks around a search string

Viewed 2324

I am trying to pass a value from the URL to a controller to get data with the string from the URL, here is the route that passes the data to the controller this is working fine, and the correct values are making it to the controller

Route::middleware('auth:api')->get('test/{name}', 'EmployeeController@getAllEmployees');

I have tried $name, "$name" , '$name' , '%' . $name . '%' , '%$name%'

class EmployeeController extends Controller
{
    public function getAllEmployees($name)
    {
        $model = Employee::where('Name', 'LIKE',  $name)->get();
        return $model->tojson();
    }
}

Here is the error, as you can see the value doesn't have any quote marks around it which is what sqlsrv is tripping up with when running the query

"SQLSTATE[42S02]: [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Invalid object name 'Tabel_Name'. (SQL: select * from [Tabel_Name] where [Name] LIKE test)",

what am i doing wrong?

Laravel 5.5 sqlsrv php 7.0

2 Answers

You shouldn't care about quotes because error comes from table and not from missing quotes (Laravel uses PDO to bind query parameters). The problem here is probably missing table in database.

So make sure you have already run:

php artisan migrate

or if you haven't created such table, you should create new migration and then run this command to create table in database

Most probably your table name is in fault. Laravel uses a convention if your table name is posts then your model name should be Post other you will have to define table name in the model itself.What is your Model name and table name?

Related