A table was not found - php

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';

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 assumes wrong table name in model when it is hard coded

i have a model:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class MultiProductVariantPivot extends Model
{
//use HasFactory;
protected $table = "multi_product_variant_pivot";
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'multi_product_id',
'variant_id',
'decision_tree',
'hashed_decision_tree'
];
}
I have a query:
$variant_decision_trees = MultiProductVariantPivot::where('multi_product_id', $multi_product_id)->get();
I have an error:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'products.multi_product_variant_pivot' in 'where clause' (SQL: select * from `products` where `products`.`multi_product_variant_pivot` = 1 and `products`.`multi_product_variant_pivot` is not null)
Question: Could someone explain to me why Laravel is pointing to the 'products' table (a real table i have) and not the explicitly defined one? How do i stop Laravel overriding my decisions with impunity? Is there a terminal update command that i should have run to refresh something?
EDIT:
I have found another interesting thing, if i change the column name in the where() to "multi_product_id_test" instead of "multi_product_id" it will reference the correct table..
the new error given:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'multi_product_id_test' in 'where clause' (SQL: select * from `multi_product_variant_pivot` where `multi_product_id_test` = 1)
Thus, the column selection in the where() is affecting the table selection.. anyone care to explain how to avoid this? also it seems to have added an extra "is not null" clause in the first query, there is defiantly something weird going on.
EDIT 2:
If I change my table name to anything wrong like mproduct_variant it uses the proper query, if I change it to match an existing table it does the wrong query.. Laravel is trying its hardest to make me not be productive, I'm quite impressed.
EDIT 3:
if i change the table name in my model to:
protected $table = "multi_product_variant";
the error i get is:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'products.multi_product_variant_pivot' in 'where clause' (SQL: select * from `products` where `products`.`multi_product_variant_pivot` = 1 and `products`.`multi_product_variant_pivot` is not null)
as can be seen its using products.multi_product_variant_pivot instead of multi_product_variant. could someone explain this behavior? it seems to be caching my old table name? very strange.
That's because you are naming your model with "Pivot" suffix, which is interfering with Laravel's many-to-many relationship system and not the best practice. What you can do is "force" Laravel by telling it which table to use:
$variant_decision_trees = MultiProductVariantPivot
::where(`multi_product_variant_pivot.multi_product_id`, $multi_product_id)->get();
That's the possibility that I can think of, it may not be the root tho. And for the love of god. Follow the convention if you can.
okayy so here it is, i had a model called MultiProduct with a function to relate the variants of the product like so:
public function variants(){
return $this->hasMany( 'App\Models\Product', 'multi_product_variant_pivot');
}
so what was happening was my MultiProductVariant model was being translated into activating the variants() function from the MultiProduct model. I changed it to be:
public function products(){
return $this->hasMany( 'App\Models\Product', 'multi_product_variant');
}
and now it works because its not being linked! Dont ask me why, I'm just a consumer of this framework. Crazy stuff.

I am trying to insert some data into status table but getting SQL Query Error in Laravel

I am new in Laravel. I have a table, and that table name is {status}.
Under the Models\ Status.php when I removed protected $table='status'; from status.php then I am getting this error!
Illuminate\Database\QueryException SQLSTATE[42S02]: Base table or view
not found: 1146 Table 'cms.statuses' doesn't exist (SQL: insert into
statuses (status, user_id, updated_at, created_at) values
(dfg, 1, 2021-06-22 15:16:10, 2021-06-22 15:16:10))
Here is my Table schema
And HomeController function
I would be very happy if anyone can explain, why I am getting this kind of error when I'm not using this line => protected $table='status';
thank you!
To understand better:
Each model is extended from use Illuminate\Database\Eloquent\Model; So it has method called getTable() like below
/**
* Get the table associated with the model.
*
* #return string
*/
public function getTable()
{
return $this->table ?? Str::snake(Str::pluralStudly(class_basename($this)));
}
if you see this:
First it will check if you have set table property manually. If its then it will take table name from protected $table='status';
Suppose if you haven't set property then it will take class name and then pluralize the name then it will convert to snake case.
For example in your case, Model name is Status
$response=Str::snake(Str::pluralStudly(class_basename("Status")));
dd($response);
Result will be statuses. So it expect statuses table in database.
For example Consider You have model name UserDetail then
$response=Str::snake(Str::pluralStudly(class_basename("UserDetail")));
dd($response);
then result will be user_details. So it expect user_details table in database.

Laravel - querying from existing Mysql

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

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