SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous - laravel

When I display the name of table using the id of another table in Laravel I declare like this. But when I want to get the id to edit, the error is like the title. Please help me, I am very grateful.
Controller
public function manage_departments(){
$manage_departments=DB::table('departments')
->join('faculties','faculties.id','=','departments.faculty_id')
->orderBy('departments.created_at','desc')->get();
$all_manage_departments=view('admin.manage-departments')->with('manage_departments', $manage_departments);
return view('layouts.master')->with('admin.manage-departments', $all_manage_departments);
}

You don't have select in your query builder. As laravel doc said, you can specify what do you want to select in select method. I select faculties.id and departments.id for example.
DB::table('departments')
->select('faculties.id','departments.id')
->join('faculties','faculties.id','=','departments.faculty_id')
->orderBy('departments.created_at','desc')->get();

Related

Eloquent Intermediate Tabel with None Standard ID Column

While creating my database according to the eloquent standard, I ran into the problem that my table_name and id column name combined would be longer as 64 characters.
very_long_table_name.very_long_column_name_id
So I used a shorter column name as the foreign key in my Intermediate Table.
Migration file:
$table->unsignedBigInteger('short_id');
$table->foreign('short_id')->references('id')->on('very_long_table_name');
That worked fine, yet now I would like to insert a connection
Seeder.php:
$x->very_long_table_name()->attach($other_table_name->id);
I get the error:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'very_long_column_name_id' in 'field list' (SQL: insert into very_long_table_name (just_an_id, very_long_column_name_id) values (1, 1))
What I would want is that it would use the column short_id instead of very_long_column_name_id to insert this, is there any cool way to fix this? or do I need to insert and query the connection manually if I keep the long names?
Like #Tim points out in the comments this needs to be done in the Model VeryLongTableName.php where you define the relationship:
public function very_long_table_name() {
return $this->belongsToMany(Model::class, 'very_long_table_name', 'local_id', 'short_id');
}

How to get specific columns from relation table in laravel?

I am trying to fetch soecific columns data from relational table but it is giving me null
$allConsignments = Consignment::query();
$allConsignments->select(['id','customer_reference'])->with('customers:name,id')->orderBy('id', 'desc')->limit(5000)->get();
When I don't use select() then it gives correct data .
like this
$allConsignments = Consignment::query();
$allConsignments->with('customers:name,id')->orderBy('id', 'desc')->limit(5000)->get()
it is working but I also need specific columns from Consignment Table. what could be the reason?
You can also do like this.
$allConsignments = Consignment::query();
$allConsignments::with('customers:name,id')->orderBy('id', 'desc')->limit(5000)->get(['id','customer_reference']);
Actually, I also need to select the foreign key column from the table on which relationship is based. for example in my case I have customer_id in consignment table so it should be like that
$allConsignments = Consignment::query();
$allConsignments->select('id','customer_reference','customer_id')->with('customers:name,id')->orderBy('id', 'desc')->limit(5000)->get();
I need to select customer_id as well

Lumen 5.1 - many to many sync is missing data

I'm trying to create a reusable method for creating a relationship for a many to many pivot table but it seems to be missing the listing_id when trying to sync the data.
$model = $this->model->findOrFail($model_id)->with($relation);
return $model->getRelation($relation)->sync($data);
Returns:
integrity constraint violation: 1048 Column 'listing_id' cannot be null (SQL: insert into `tenants_listings` (`created_at`, `listing_id`, `tenant_id`, `updated_at`) values (2019-03-01 11:10:36, , ef4c9d60-a7a3-3340-8dd0-a901d624cd97, 2019-03-01 11:10:36)
This works perfectly fine when done like this:
$model = $this->model->findOrFail($model_id)->tenants();
return $model->sync($data);

Laravel Find Not working with custom primary key

I am stuck with a situation where i coudn't update my records using Eloquent in Laravel 5.5 using custom primary key.
Here is what i tried :-
In my Model.php i have,
protected $primarykey = 'custom_primary_key';
my migrations looks like :-
$table->increments('custom_primary_key');
in my controller,i have :-
Model::find(custom_primary_key);
but when i tries to find a record using find() in my controller, it gives
Column not found: 1054 Unknown column 'table.id' in 'where clause' (SQL: select * from `table` where `table`.`id` = 1 and `table`.`deleted_at` is null limit 1)"
table also contains a column with custom primary key and there is no column named id.
It doesnt recognize the cutom primary key. Where can i be missing something ?
Set $primarykey into $primaryKey. Check the documentation from Laravel here:
https://laravel.com/docs/5.6/eloquent#eloquent-model-conventions

Pivot table but not using table id

Is it possible to make a Pivot Table without using table id?
users
id
biometric_id
first_name
last_name
attendances
id
biometric_id
date
emp_in
emp_out
user_attendances
user_id
attendances_biometrics_id
I wanted to ask if this is available to link it like this? Because I need to show the attendance of the user that has his biometrics.
If it is possible, how?
If attendances.biometric_id has a unique constraint on it then there should be no reason why you cannot use it as a foreign key constraint.
Assuming your tables have been setup properly with foreign key constraints, your user model would probably have something like this:
public function attendances() {
return $this->belongsToMany('App\Attendances', 'user_attendances', 'user_id', 'attendances_biometrics_id');
}

Resources