6

2 months agoby Salman Zafar

How to create relationship with user table while developing a package?

Hi,

how can i define relationship with user table while creating a Laravel package? Suppose i've a table to links and i want to add it relationship with existing user table in laravel ? How can i achieve that?

Thanks

72

Sarthak Shrivastava replied 2 months ago

1

Let user model is always App\User on every laravel. Just create user relation in your package as you normally do.

If you want dynamic user model, then in your package config file you need to set for user model like this

return [
	'models'  => [
		'user' => App\User::class,
	],
],

then on your other model, then get this from your your config file.

public function user()
{
		return $this->belongsTo(config('packageName.models.user'), 'user_id');
}
Ask Question
Most Visited
Categories