last day (15 days later) » 

2:12 PM
0
Q: Method hospitals does not exist

Nida AkramHey I am working on a project in which i have defined many to many relation between two tables. I have made a method in model class but whenever i call that method from my controller it gives me error method does not exist. Below is my code of controller and model. Controller if($request->hospi...

 
you have created a pivot table for this rite?
 
@DhavalChheda yup i have created a pivot table
 
can you share that table details
 
@DhavalChheda shared
 
you should try first() instead of get()
 
2:12 PM
@DhavalChheda thanx it is not giving the error but i have one more that is the id stored in the pivot table is the one which is by default created when we create migration not the one which i want what should i do
 
Sorry , I did not understand which Id you are talking about..
And just for your understanding get() returns a collection and you need an object to call methods on using ->
 
when we create migration an attribute of id is by default created that is incremented automatically. I have made a separate doctorID which i want to store in the pivot table but its not storing that doctorId its storing the the id which is created by default
 
so If i understood correctly, pivot of doctor_hospital created a reference of doctor_id on doctors table and you want to use something else but not the doctor_id .. is that correct?
 
yup
i want to use another id
 
ok..create another migration like this php artisan make:migration update_doctor_id_in_doctor_user_table
 
2:22 PM
and then
 
This will give you the migration and in that in the up method you can enter the new column name and the down method you can enter the column you want to drop
and then db:migrate
it should make the changes .. I guess this will work as expected
 
no you dnt understand see i have my doctor table in which i have two ids $table->increments('id');
$table->string('doctorId')->unique(); like this i want to use the second id in my pivot table but its storing the first id what can i do that the id stored in the pivot table is the second one not the first
 
correct .. I understood that .. now what laravel did was in the migration it created a default id that it will use which is the 'id' of your doctors table but you did not want that and you ran the migration .. so now what you need to do is update the migration to change the doctor_id to doctorId so you will have to create another migration and run it because a migration is migrated only once..
so either roll back and create the migration again and then change the migration manually and then migrate it or else modify the migration
The only thing which I am unsure is that Laravel might be looking for a primary key so try this and see if any success
 
ok i'll try
I have made the id which i want to be in the pivot table primary key and removed the default made id but now its giving me error doctorId cannot be null while I am entering record in the pivot table
 
2:42 PM
you might have to do something like this
$doc->hospitals()->attach($doctorId);
I meant explicitly adding the doctorId
 
but then it will not add hospital id
 
why not?
 
see if I am passing doctor ID but not hospital ID how can it will add hospital ID
 
you are still calling the relationship so I think it should add it
try and see
 
ok let me try
 
2:52 PM
you can always use tinker for testing
 
3:22 PM
is it working?
 

last day (15 days later) »