पर एक __construct मेरे पास एक कस्टम सेटर है जो मैं अपने मॉडल पर __construct
विधि में चल रहा हूं।एलोक्वेन्ट लैरवेल मॉडल
यह वह संपत्ति है जिसे मैं सेट करना चाहता हूं।
protected $directory;
मेरे निर्माता
public function __construct()
{
$this->directory = $this->setDirectory();
}
सेटर:
public function setDirectory()
{
if(!is_null($this->student_id)){
return $this->student_id;
}else{
return 'applicant_' . $this->applicant_id;
}
}
मेरे समस्या यह है कि मेरी सेटर के अंदर, $this->student_id
(जो मॉडल की एक विशेषता है डेटाबेस से खींचा जा रहा है) null
लौट रहा है। जब मैं अपने सेटर के अंदर से dd($this)
, मुझे लगता है कि मेरा #attributes:[]
एक खाली सरणी है।
तो, __construct()
के बाद तक मॉडल के गुण सेट नहीं किए जाते हैं। मैं अपनी निर्माण विधि में अपना $directory
विशेषता कैसे सेट कर सकता हूं?
public function __construct(array $attributes = array())
{
parent::__construct($attributes);
$this->directory = $this->setDirectory();
}
पहली पंक्ति (parent::__construct()
) अपने कोड चलाता है से पहले सुवक्ता Model
की खुद की निर्माण विधि चलेंगे, जो आप के लिए सभी विशेषताओं स्थापित करेगा:
वही है जो मुझे चाहिए !! धन्यवाद –