Laravel 5.5 Eloquent not putting quote marks around a search string - php

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

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

Lumen changes table name in query

I'm new to Lumen (ver. 8.3.4) and I got a strange issue during my tests.
In my DB I have the table "Pippo"; to query it I created the model App\Models\Pippo and the controller App\Http\Controllers\PippoController.php, that includes the aforementioned model.
To route the requests, in web.php I added the line:
$router->post('getdomain', 'PippoController#getdomain');
Now, in 'getdomain' function I've a simple
$var = Pippo::all();
but when I try to call it, I get the following error:
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'authserver.pippos' doesn't exist (SQL: select * from pippos)
I searched and researched multiple times in the code, but I don't understand why Lumen adds the 's' character to the table name.
Any suggestion?
you can put in modal protected $table = 'pippo'; To avoid this error

Laravel Eloquent model with sqlsrv is changing the table names

I'm new to laravel and have a problem with the model created with php artisan make:model [Name]
In my case i connect to the sqlsrv. The connection is established.
my route looks like this:
Route::get('/tasks', function () {
// $tasks = DB::table('WebShops')->get(); //This is working
$tasks = App\WebShops::all(); //This is not working
dd($tasks);
});
my datatable:
Webshops
For this i created a model to connect to the database. php artisan make:model Webshops.
When i access the db with the command $tasks = App\WebShops::all(); i'm getting an error:
SQLSTATE[42S02]: [Microsoft][ODBC Driver 11 for SQL Server][SQL
Server]Invalid Object Name "web_shops". (SQL: select * from
[web_shops]).
The problem is that ma table name is different. The query is searching where the table is web_shops and my table name is Webshop. I don't understand it.
the name in the db not write. you can't write 2 words behind each other like that als db name allows plural.
to avoid such problem you when you make model you can write like that
php artisan make:model Wenshop -c -m -r
when you write like that Laravel make for you Model+ Migration+ Controller with the right names

How to create a SQL VIEW when using Eloquent outside of Laravel

When using Eloquent outside of Laravel and using the Capsule\Manager class, I can't see how to create a SQL view, only a SQL table. When using Eloquent with Laravel, you can do something like:
DB::statement( "CREATE VIEW foo AS SELECT id, name FROM . . .
But the Capsule\Manager class doesn't have a statement() method or any way to execute raw SQL (like the statement() method does) that I can see.
The Capsule\Manager class seems to let you do everything else you can normally do with Eloquent, surely there's a way to create a view?
If you need to execute raw SQL you can use Manager::connection() to get the connection object which has statement and raw methods. Try this:
$db = \Illuminate\Database\Capsule\Manager::connection();
$db->statement($db->raw('CREATE VIEW `foo` AS SELECT * FROM `bar`'));

CodeIgniter DataMapper table name override not working

I have just set up DataMapper with CodeIgniter and written the models as required.
However I seem to have missed something, as when I run a query, DataMapper cannot see the table name override and thinks there is no table at all.
Funny thing is, if I remove the override, it works as it should (albeit with an error saying the auto-generated tabled doesnt exist).
Model Code:
class Activity extends Datamapper {
var $table = 'activity_log';
var $has_one = [
'user'
];
}
Controller Code:
$activity = new Activity(1);
Error:
Error Number: 1064
You have an error in your SQL syntax;
check the manual that corresponds to your MySQL server version for the right
syntax to use near 'WHERE `.`id` = 1' at line 2
SELECT * WHERE `.`id` = 1
As I mentioned above, if I remove var $tables = 'activity_log'; then it will defer back to looking for table 'activities' as it should.
Rules
DataMapper models must be named the singular version of the object
name, with an uppercase first letter. So for a user object, the
DataMapper model would be named User. The model should have a
corresponding table in the database named as the lowercase, pluralised
version of the object name. So for a DataMapper model named User, the
table would be named users. For a DataMapper model named Country, the
table would be named countries.
In most cases, the difference between the singular and plural version
of an object name is just a matter of adding the letter s on the end.
Your model should be named as Activity_log.
Docs.

Laravel database connection: Selecting from database name in snake case

I'm starting to learn Laravel. I've run through the example instructions from the site successfully and now I'm trying a second run through and I'm running into an issue.
I'm trying to connect to a database called zipCodes and has one table called zipCodeDetails.
In my Laravel project I have a model containing the following code:
<?php
class ZipCodeDetails extends Eloquent {}
And in my routes.php file I have the following code:
Route::get('zipCodes', function (){
$zipCodes = ZipCodeDetails::all();
return View::make('zipCodes')->with('zipCodes', $zipCodes);
});
The error I'm running into is when I try to load the URL:
http://localhost:8888/zipCodes
In my browser I'm getting the error code:
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'zipcodes.zip_code_details' doesn't exist (SQL: select * from `zip_code_details`)
There's nothing written in my code where I define the database zipCodes as zipcodes or the table zipCodesDetails as zip_code_details. Something in laravel is changing the database and table names.
Does anyone know why this is happening and how I can prevent it? I don't want to just rename the database or table names because while that may get me by in testing it's not a viable solution in practice.
Thanks!
This is the behaviour that uses if no table is being explicitly defined. In your ZipCodeDetails class, you can set the table name that this model will be using.
class ZipCodeDetails extends Eloquent
{
protected $table = 'zipCodesDetails';
}

Categories