Laravel eloquent a la méthode

Exemples de code

2
0

associé laravel

When updating a belongsTo relationship, you may use the associate method. This 
method will set the foreign key on the child model:

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

When removing a belongsTo relationship, you may use the dissociate method. This
method will set the relationship foreign key to null:

	$user->account()->dissociate();
	$user->save();
0
0

par défaut, les données utilisateur éloquentes nulles dans la relation dans laravel 8

# simple handle in blade file 
{{ $post->author->name or 'No author' }}

# Model action 
public function author()
{
    return $this->belongsTo('App\Author')->withDefault();
}

public function author()
{
    return $this->belongsTo('App\Author')->withDefault([
        'name' => 'Guest Author',
    ]);
}

Pages connexes

Pages d'exemples similaires

Dans d'autres langues

Cette page est dans d'autres langues

Русский
..................................................................................................................
English
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................
Балгарскі
..................................................................................................................
Íslensk
..................................................................................................................