here is the answer
public function save(array $options = array())
{
if( ! $this->company_id)
{
$this->company_id = Auth::user()->company->id;
}
parent::save($options);
}
Thanks to Antonio Carlos Ribeiro from stackoverflow http://stackoverflow.com/questions/21803077/laravel-4-how-to-add-user-id-as-a-default-value-when-inserting-new-records
You can use property mutators inside your models:
public function setCompanyIdAttribute($cid)
{
$this->attributes['company_id'] = Auth::user()->company->id;
}
Just another solution :)
usm4n said:
You can use property mutators inside your models:
public function setCompanyIdAttribute($cid) { $this->attributes['company_id'] = Auth::user()->company->id; }
Just another solution :)
not really 'another solution' because you still need to call for the method to trigger
$model->company_id = 'dummy value';
what ahmd8 needs is Model Observers (http://laravel.com/docs/eloquent#model-observers) and listen to the 'saving' event
Sorry :(, i misunderstood the problem, you are right :)
usm4n said:
Sorry :(, i misunderstood the problem, you are right :)
no worries ;)
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community