mardi 6 août 2019

How to get a current model in controller in order to save a relation?

I will try to explain as clear as possible since since I am new to laravel and php.

Suppose I have 3 models, Company, Branch and contact.

Company - branch 1 to many

Branch - contact 1 to many

In Branch controller, when I create a new branch, I want to associate it to a Company. I know that it might be a stupid question, but in documentation is appears:

$account = App\Account::find(10);
$user->account()->associate($account);
$user->save();

There obviously the "account" is hardcoded.

My question is, how do I get the current company in which the user is logged in, without calling the hole model. (Company::find...), and how to check if there is actually a "company". In this last question I came up with the idea of checkign isset(), but I dont know which is the best practice.

Thank you.

note: I don't have any code to show yet, I hope it was clear.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire