Laravel - querying from existing Mysql - php

how do i define the table to query from without having to migrate the db ? i've tried manual define in my model but it doesn't helps.
Eloquent/Model
class Activity extends Model {
protected $table = 'log_activity';
}
my table supposed to be 'log_activity' but not 'activities' as the exception below
QueryException in Connection.php line 673:
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'bizoctopus.activities' doesn't exist (SQL: select * from `activities` where `activities`.`id` = 6 limit 1)

my mistake creating the model file inside sub directory folder named 'providers' instead of app folder.
thanks for the help.. appreciate it

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

A table was not found

See this issues below , I having this issue.After creating a Model Like Burgers PHP
My Front Welcome page is working properly but when I go threw 127.0.0.1:8000/burgers
this address it's error below .
here is the error message :
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'backyardburger.burgers' doesn't exist (SQL: select * from `burgers`) ```
Rename your table to burgers
OR
add the following to your Burger.php
protected $table = 'backyardburger';

LARAVEL - Base table or view not found: 1146 Table doesn't exist (SQL: select * from ) [duplicate]

This question already has answers here:
DB query taking wrong table name on laravel [duplicate]
(1 answer)
Laravel - Database, Table and Column Naming Conventions?
(4 answers)
Closed 1 year ago.
I have a Mysql database minho.win and a table called utilizadores.
I created a model class php artisan make:model Utilizador
When I do php artisan tinker and then do App\Utilizador::all() I get this error:
Illuminate\Database\QueryException with message 'SQLSTATE[42S02]: Base table or view not found: 1146 Table 'minho.win.utilizadors' doesn't exist (SQL: select * from utilizadors)'
Why is it looking for a table named utilizadors? How can I make it look for the right table - utilizadores?
You can specify the table in your model:
class Utilizador extends Model {
protected $table = 'utilizadores';
}
https://laravel.com/docs/5.5/eloquent#eloquent-model-conventions
so you have to protect your table in you Model
something like that:in Your Model
protected $table = "utilizadores";
put your field's name in this array, it will seem like that:
protected $filliable = [
"id",
""
];
in migrations check whether the table name is spelt correctly I had this problem where there was an _ before the table name
public function up()
{id();
Schema::create('_employees', function (Blueprint $table) {
$table->
remove the _ and fresh migrate

How to change Laravel model's table name

I am using Laravel 5 and have changed the name of a database table from "domain_related_settings" to "DomainRelatedSettings" by rolling back all migrations, changing the specific migration, and running them again. The new table name is reflected in the database.
But when i use the corresponding model DomainRelatedSetting in a statement like this:
$domainSettings = DomainRelatedSetting::where('hostname', 'foo')->first();
it gives the following error:
SQLSTATE[42S02]: Base table or view not found:
1146 Table 'databasename.domain_related_settings' doesn't exist
(SQL: select * from `domain_related_settings` where `hostname` = foo limit 1)
So it is still using the old table name. How can I ensure the new table name is used?
If you don't want to use the default table name (the "snake case", plural name of the class), you should specify it to the model:
protected $table = 'DomainRelatedSettings';
Check the documentation at the Table Names section.
You may specify a custom table by defining a table property on your model:
class theModel extends Model
{
/**
* The table associated with the model.
*
* #var string
*/
protected $table = 'name_of_table';
}
In case it doesn't work, try typing this command inside from your root folder:
composer dump-autoload -o
You need to specify the table name inside the each Laravel model by using
protected $table = 'name_of_table';
so in your case
protected $table = 'DomainRelatedSettings';
If you specify the real name of the tables in your models but still the same problem, try:
composer dump-autoload

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