Laravel Find Not working with custom primary key - laravel

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

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 access to user data through intermediate table (hasOneThrough / hasManyThrough)

For a job posting application, I have three tables, which shortly are defined as:
applications:
id as primary key
job_offer_uuid as external key
job_offers:
uuid as primary key
user_id as external key
users:
Just laravel normal users table with id as primary key
Because I need to notify job_offer owner (a member of User model) any time that an application is registered, I'm trying to create a hasOneThrough or hasManyThrough relationship from applications to users, but without success for the moment.
For clarification:
User model only hosts users that publish job offers, and any user can publish many job offers. There is not applicants in users table
Based on my understanding of eloquent documentation (https://laravel.com/docs/8.x/eloquent-relationships#has-one-through), my actual code in Application model is:
public function publisher()
{
return $this->hasOneThrough(User::class, JobOffer::class, 'job_offer_uuid', 'user_id');
}
But it fires an SQL error:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'job_offers.job_offer_uuid' in 'field list' (SQL: select `users`.*, `job_offers`.`job_offer_uuid` as `laravel_through_key` from `users` inner join `job_offers` on `job_offers`.`id` = `users`.`user_id` where `job_offers`.`job_offer_uuid` in (1)
using hasManyThrough instead, I got an identical error:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'job_offers.job_offer_uuid' in 'field list' (SQL: select `users`.*, `job_offers`.`job_offer_uuid` as `laravel_through_key` from `users` inner join `job_offers` on `job_offers`.`id` = `users`.`user_id` where `job_offers`.`job_offer_uuid` in (1))
I can get accurate results using pure SQL with a sentence like this:
select applications.id, applications.job_offer_uuid, job_offers.uuid, job_offers.user_id, users.id, users.name, users.email from `applications` inner join job_offers on `applications`.`job_offer_uuid` = `job_offers`.`uuid` join users on job_offers.user_id = users.id where `applications`.id = 1
Any video or tutorial that I found related to this point are using the final table with a foreign key to the intermediate table, and thats means my User model should have a foreign job_offer_id key, but that make no sense to me.
Any clarification should be truly appreciate. Regards!
You are doing it wrong. You have to define relationship in User Model as follow:
public function publisher()
{
return $this->hasOneThrough(
Application::class,
JobOffer::class,
'user_id', // Foreign key on job_offers table
'job_offer_uuid', // Foreign key on applications table
'id', // Local key on user table
'uuid' // Local key on job_offer table
);
}

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

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();

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

sql 42S22 error in Laravel (choosing a column I didn't mention)

I am doing a basic project in Laravel, when trying to delete an entry, it generates this error:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'id' in 'where clause' (SQL: select * from `employees` where `id` = 6 limit 1)
and it is true I don't have a column named 'id', instead I have employee_id, but why is it choosing id instead of employee_id?
Please explain from where did it bring this id column?
In your Employee model (Employee.php), add
protected $primaryKey = 'employee_id';
This will tell Laravel to use employee_id as the primary key for Empolyee objects.

Resources