required table for Laravel 5 Authentication - laravel

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

Related

Create eloquent relationships for Table with foreign keys to Pivot table - Laravel

I have a pivot table (builder_project) which I'm using on a many-to-many relationship between the 'projects' and 'builders' tables. And everything is working fine to this point.
However, I would like users to be able to add 'notes' to the pivot table (builder_project) and because many notes should be able to be added I created a table called 'notes' which has a one-to-many relationship with the pivot table.
I tried creating a model for the pivot table, however; when I try to access the method within the pivot model, I'm getting the following error:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'notes.' in
'where clause' (SQL: select * from builder_project where exists
(select * from notes where builder_project.project_projectID =
notes.``))
This is the code for my pivot model:
namespace Estimating;
use Illuminate\Database\Eloquent\Relations\Pivot;
class Builder_project extends Pivot
{
protected $table = "builder_project";
protected $primaryKey = 'project_projectID'; // I need to add the other primary key here but I undestand it's not possible on Laravel
protected $fillable = [
'project_projectID',
'builder_builderID',
'note',
];
// One to many relationships
/**
*
*/
public function test()
{
return $this->hasMany('Estimating\Builder_project_note');
}
}
This is the model for Builder_project_note:
<?php
namespace Estimating;
use Illuminate\Database\Eloquent\Model;
class Builder_project_note extends Model
{
//Determines which database table to use, in this case 'projects' table
protected $table = "notes";
protected $primaryKey = 'noteID';
protected $fillable = [
'project_projectID',
'builder_builderID',
'note',
];
// One to many relationships
/**
* Get the status that owns the project.
*/
public function test()
{
return $this->belongsTo(
'Estimating\Builder_project'); // I know that here I need an ID from Builder_project pivot table - but I have a composite key!;
}
}
And this is how I'm trying to get the data from my controller and I get the error:
public function editBuilderProject(Request $request, $builderID, $projectID)
{
$browserDetails = new Agent();
$project = Project::find($projectID);
$builder = Builder::find($builderID);
$builder_project_statuses = Builder_project_status::all();
$builder_project_note = Builder_project::has('test')->get();
dd($builder_project_note);
return view('projects/edit-builder-project', compact('browserDetails', 'project','builder', 'builder_project_statuses', 'builder_project_note'));
//return $projectID." ". $builderID;
}
I would appreciate if anyone can point me to the right direction with this and my apologies if I'm not being clear enough - this is my first post here :)
I've uploaded a sample of my database model:
Database Model example
Laravel does not like primary keys based upon multiple columns. You need to add its own builder_project.id column (in your pivot table) which will be the field used by your notes table.

Accessing Laravel custom Pivot class directly

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

Laravel 5. How to create a register using tables : users,profile and account

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.

Laravel hasManyThrough Eloquent query failing

I have 3 models:
Client, Store, Format
A client has many stores, and a store has one format.
So, I would like to get all Formats from Client.
In Store table, I have client_id, format_id
in Format table, I have no direct relationship, I have plan_id
In Plan table, I have client_id.
In Client table, I have no useful relationship
I need to get all formats of a client,
So I need to create a relationship $client->formats
So basically,I want to do a hasManyThrough relationship,
class Client extends Model {
protected $table = 'client';
protected $primaryKey = 'clientid';
public function formats()
{
return $this->hasManyThrough(Format::class,Store::class, 'clientid', 'storeid');
}
}
But I get this error:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'format.storeid' in 'on clause' (SQL: select `format`.*, `store`.`clientid` from `format` inner join `store` on `store`.`storeid` = `format`.`storeid` where `store`.`clientid` = 58)
I'm a little bit stuck in this relation, any idea how to fix it????
You have a many-to-many relationship
class Client extends Model {
protected $table = 'client';
protected $primaryKey = 'clientid';
public function formats()
{
return $this->belongsToMany(Format::class, 'Store_table', 'client_id', 'format_id');
}
}

Laravel get wrong table while querying

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

Resources