Laravel define multiple foregin key in models and getting error

Multi tool use
Laravel define multiple foregin key in models and getting error
for this migration as:
Schema::create('user_tickets', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->string('subject');
$table->integer('ticket_number')->unique();
$table->tinyInteger('priority')->default(2);
$table->tinyInteger('section');
$table->boolean('solved')->default(false);
$table->timestamps();
});
and
Schema::create('user_tickets_answers', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->integer('ticket_id')->unsigned();
$table->foreign('ticket_id')->references('id')->on('user_tickets')->onDelete('cascade');
$table->text('reply',200);
$table->timestamps();
});
laravel create this foreign keys for me
user_tickets_answers_user_id_foreign
user_tickets_answers_ticket_id_foreign
on user_tickets_answers
table, now when i try to get user_tickets
data with user_tickets_answers
relation such as:
user_tickets_answers
user_tickets
user_tickets_answers
$tickets = UserTickets::with('reply')->get();
i get this error:
Base table or view not found: 1146 Table 'diabet.user_ticket_answers' doesn't exist (SQL: select * from `user_ticket_answers` where `user_ticket_answers`.`ticket_id` in (1))
how can i define this foreign keys on self models?
UserTickets :
class UserTickets extends Model
{
protected $guarded = ['id'];
public function reply()
{
return $this->hasMany(UserTicketAnswers::class, 'ticket_id');
}
}
UserTicketAnswers :
class UserTicketAnswers extends Model
{
protected $guarded = ['id'];
public function ticket()
{
return $this->belongsTo(UserTickets::class);
}
}
2 Answers
2
Your table name is different in migration it is user_tickets_answers while you are querying user_ticket_answers table in database.
Try to change the statements if it works, because it works for me when I had this error previously. To be sure, check again the Documentation in passing custom key like
('AppPhone', 'foreign_key', 'local_key');
In UserTickets model change
return $this->hasMany(UserTicketAnswers::class, 'ticket_id');
to
return $this->hasMany('AppUserTicketAnswers','ticket_id','id');
In UserTicketAnswers
return $this->belongsTo(UserTickets::class);
to
return $this->belongsTo('AppUserTickets','ticket_id','ticket_id');
same logic with your User
model.
User
public function userTickets(){
return $this->hasMany('AppUserTickets','user_id','id');
}
public function userTicketAnswers(){
return $this->hasMany('AppUserTicketAnswers','user_id','id');
}
Add each to UserTickets
and UserTicketAnswers
model
UserTickets
UserTicketAnswers
public function user{
return $this->belongsTo('AppUsers','user_id','user_id');
}
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.