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
Related
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
I run the Artisan command in the code like
Artisan::call('migrate', ['--path' => 'database/migrations/core']);
I Need to know the columns added by that batch of migration which ran recently. I tried using Artisan::output(); command but it returns the migration name alone.
Is there any way to get the column names of the migration batch or to extract the column name from the migration name?
You can analyze the Laravel query log:
DB::enableQueryLog();
Artisan::call('migrate', ['--path' => 'database/migrations/core']);
$log = DB::getQueryLog();
dd($log);
I'm trying to read data from a .sql file in a seeder to fill 3-4 tables with some data and DatabaseSeeder.php looks like this
public function run() {
$this->call([
UsersTableSeeder::class,
// Bunch of seeders using Eloquent
SqlSeeder::class
]);
}
All other seeders execute and, actually, when trying to throw an exception in SqlSeeder.php I'm able to stop the seeding. However, SqlSeeder.php won't seed the database via php artisan migrate:fresh --seed, seems like it's bypassed. I always need to run php artisan db:seed --class SqlSeeder after, in order to make it seed the database. SqlSeeder.php looks like this
public function run() {
$path = base_path().'/database/seeds/sql/data.sql';
$sql = file_get_contents($path);
DB::unprepared($sql);
}
Why's that?
I solved my own issue by removing transactions from the .sql file I was trying to execute via DB::unprepared(). Oddly enough, transactions completely fail when executing php artisan migrate:refresh --seed, but they work if I later call the SqlSeeder individually via php artisan db:seed --class SqlSeeder. There are no foreign key constraints for now and InnoDB was chosen as engine, just to be sure, but still transactions both fail and work depending on the command.
I guess it all depends on how Illuminate\Database\Seeder::call works and calls seeder classes internally, but I'm not sure.
Check if seeding data in sql file is in right order.
For example if you have foreign key category_id that references to categories.id posts table will be empty without any errors when you use this sql file:
INSERT INTO posts (title, category_id) VALUES ('test', 1);
INSERT INTO categories (title) VALUES ('category');
You should seed categories first and only then posts.
For Laravel 5 all you have to do inside of your main seed file is:
\DB::unprepared(\File::get(base_path('path/to/your/sql/file.sql')));
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?
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';
}