I have two tables
In above table, I want to search countries.user_name (primary key) and want its isd code from isdcodes.code where isdcodes.country_username is the foreign key.
Country Model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Country extends Model
{
protected $primaryKey = 'my_id';
public function isdcode(){
return $this->hasOne('App\Isdcode', 'country_username', 'user_name');
}
}
Route File
Route::get('user/{country}', function ($country){
return \App\Country::find($country)->isdcode;
});
hear I am trying to search india(user_name in countries table) http://localhost:8000/user/india Trying to get property of non-object the above result is coz it is trying to search india in my_id but I want to search it in user_name.
http://localhost:8000/user/1 {"isd_id":1,"country_username":"india","code":91,"created_at":null,"updated_at":null}
Really thank you in advance.
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire