Terms table:
Term_taxonomy table:
My Term model:
public function TermTaxonomy(){
return $this->hasOne('TermTaxonomy');
}
My TermTaxonomy model:
public function Term(){
return $this->belongsTo('Term');
}
My Categories controller:
public function update($id){
echo "$id"; // echo success
echo $data['name']; // it should update name field in term table
echo $data['slug']; // it should update slug field in term table
echo $data['TermTaxonomy']['description']; // it should update description field in termtaxonomy table
}
how i can update one to one relationships ? maybe with push()
Thanks, sorry i am new in laravel.
You can use Eloquent's update()
method: https://laravel.com/docs/5.4/eloquent#updates
$Term = Term::with('TermTaxonomy')->find($id);
$Term->name = $data['name'];
$Term->slug = $data['slug'];
// Save The Term first
$Term->save();
// Now update the relation
$Term->TermTaxonomy->update([
'taxonomy' => 'category',
'description' => $data['TermTaxonomy']['description']
]);
as Jarek Tkaczyk comment in this question Laravel eloquent: Update A Model And its Relationships
There is no other way, as Eloquent currently doesn't know what relations are on the model until you call them as dynamic property, load with load method, eager load etc. (push works only with loaded relations that are present in model's relations array)
so i use this code.
$Term = Term::with('TermTaxonomy')->find($id);
$Term->name = $data['name'];
$Term->slug = $data['slug'];
$Term->TermTaxonomy->taxonomy = 'category';
$Term->TermTaxonomy->description = $data['TermTaxonomy']['description'];
$Term->push();
and it works. Term and TermTaxonomy table is updated, but if change push() to save() it only update Term table even TermTaxonomy relationships already loaded with Eager load Term::with('TermTaxonomy')
Thanks for all :D
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With