I have the following DB schema Products and their categories.
I have created Model for both tables in Laravel called Categorie and Product
class product extends Model
{
use HasFactory;
}
categorie
class categorie extends Model
{
// tried by adding this code in product model
use HasFactory;
protected $primaryKey = 'CategoryID';
//also tried without translation...
public static function translateColumn(string $column)
{
switch ($column) {
case 'id':
return 'CategoryID';
default:
return $column;
}
}
function categorie(){
return $this->hasMany('App\Models\categorie');
}
}
fetch data
function fetchData(){
return categorie::find(1)->categorie;
}
I am facing the following exception SQL Exception
I have tried many ways but I can't found a way to run correct query.
I do not want to change DB columns name like change CategoryID to id Only use of model to do this stuff. Cannot use DB class to build query.
If this is possible then how and if its not possible then tell me...
NOTE: I am learning this and want to know if this possible then how... I just don't need some alternate solution... I know alternatives...
CodePudding user response:
edit categorie method to:
function categorie(){
return $this->hasMany('App\Models\categorie','CategoryID');
}