I'm trying to access a custom pivot class directly through Artisan tinker like this: App\Registration::all(). But it seems that classes that extend pivot are not directly accessible?
The Errormessage: Illuminate/Database/QueryException with message 'SQLSTATE[42S02]: Base table or view not found: 1146 Table 'teknologiskolen.registration' doesn't exist (SQL: select * from registration where registration.id = 1 limit 1)'
Specify the table in the pivot model:
class Registration extends Pivot {
protected $table = 'registrations';
}
Related
I am trying to make Multi-Level Category using laravel but I am facing this error: How to fix this error?
Illuminate\Database\QueryException
SQLSTATE[42S02]: Base table or view
not found: 1146 Table 'storykadb.category_navmenus' doesn't exist
(SQL: select * from category_navmenus where p_id = 0)
Model
namespace App;
use Illuminate\Database\Eloquent\Model;
class category_navmenu extends Model
{
public function childs(){
return $this->hasMany('App\category_navmenu','p_id');
}
}
Route
Route::get('test',function(){
return App\category_navmenu::with('childs')->where('p_id',0)->get();
});
Laravel Eloquent is taking the wrong table name, either you can change your Model name and table name according to Laravel's naming convention or add in the $table property in your model, like this :
namespace App;
use Illuminate\Database\Eloquent\Model;
class category_navmenu extends Model
{
public $table = "category_navmenu";
public function childs(){
return $this->hasMany('App\category_navmenu','p_id');
}
}
Because Laravel will find your table by changing your classname to lowercase underscore and plural. Just like:
CategoryNavmenu will become category_navemenus.
You have no specifies the table name of the protected $table. So laravel will find the table by default rule.
You can put this line in your model:
protected $table = 'category_navemenu';
so laravel can find your table.
But the important thing is plz change your class name to Upper Camel Case。This is a normal grammatical norms.
Just Change your model file name to CategoryNavmenu.php and classname to CategoryNavmenu.
I was creating one model file and trying to fetch the table data using this model.
Every time it's showing the error below:
"SQLSTATE[42S02]: Base table or view not found: 1146 Table
'laravel_student.students' doesn't exist (SQL: select * from
students)"
The controller file looks like to the following:
namespace project1\Http\Controllers;
use Illuminate\Http\Request;
use project1\Student;
use project1\Http\Requests;
class StudentController extends Controller
{
public function index()
{
$students = Student::all();
return view('student_form',compact('students'));
}
}
Open your Student Model
and add $table
class Student extends Model {
protected $table = 'student';
}
Follow Eloquent Defining Models for more details
i'm trying to create a system of register and authentication using three tables. Users, Account and Profile.
But when attempting to use a registration form displays the following message below:Tables
Column not found: 1054 Unknown column 'email' in 'where clause' (SQL: select * from users where email = thiagothgb#gmail.com limit 1)
I do a relationship in the models Users like that.
<?php
namespace App\Model;
use Illuminate\Database\Eloquent\Model;
use App\Model\Account;
use App\Model\Profile;
class Users extends Model
{
protected $table = 'users';
protected $connection = 'mysql';
public function account(){
return $this->hasOne('App\Model\Account');
}
public function profile(){
return $this->hasOne('App\Model\Profile');
}
}
What I can try to for to solve this problem and extend Models when i go create a object extended.
I try to insert some data to the new table that I have create but laravel choose wrong reverse table. Instead of job_contract get contract_job.
QueryException in Connection.php line 636:
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'job.contract_job' doesn't exist (SQL: insert into contract_job (contract_id, job_id) values (2, 4))
I am new in laravel. Is anyone know the way that laravel defines the names of tables
I am not sure about what the Model name of your related php file is.
Usually, the table would be like
if your model name isUser the table name should be users !
For tables like customer_details , the model name should be CustomerDetail
But you can also select a particular table with the model using
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Flight extends Model
{
/**
* The table associated with the model.
*
* #var string
*/
protected $table = 'my_flights';
}
In this example, you can see that the table name by default should be flights but you can change it to whatever you want like this !
A way to fix this is to tell the Model wich table to use (Put in your model):
protected $table = 'job_contract';
This way you force the Model to use that table and not some other table
See more here
A quote about the relation table:
The role_user table is derived from the alphabetical order of the related model names, and contains the user_id and role_id columns.
In your case it would be contract_job with contract_id and job_id.
And another quote:
However, you are free to override this convention. You may do so by passing a second argument to the belongsToMany method:
return $this->belongsToMany('App\Role', 'user_roles');
So I guess you only need to pass the correct table name as second param to your belongsToMany method(s) like this:
//in your Contract model
return $this->belongsToMany('App\Job', 'job_contract');
and
//in your Job model
return $this->belongsToMany('App\Contract', 'job_contract');
Quotes from Laravel docs
I'm using Laravel's auth features, and as it is said in the documents I should have a "users" table in my app so that I can use this feature, but instead of "users" table I got a "user" table.
I changed the $table property of my User model, and also changed the table property of my Auth config file:
'table' => 'user'
but still got this error upon registration:
QueryException in Connection.php line 620:
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'project.users' doesn't exist (SQL: select count(*) as aggregate from users where email = sth#yahoo.com)
If you're using a different table name for your models you need to define them. In your case you should have
protected $table = 'user';
in your User model like this.
class User extends Authenticatable
{
/**
* The table associated with the model.
*
* #var string
*/
protected $table = 'user';
}