Larravel Model relationships Query does not find a value Return default value Author: Chuwen Time: 2023-05-29 Classification: Laravel comment ##Larravel model association query does not find a value, and the default value is returned In actual business, model association queries are often used, but sometimes the associated model does not exist in the database, and null will be returned. If you judge whether it is null, the logic is complex. Here is a common example in reality Suppose there are two models, 'User:: class' and' UserConfig:: class` **User::class** ```php <? php namespace App\Models; /** * \App\Models\User * * @property int $id * @property string $username * ... Omit some attributes * * @property-read \App\Models\UserConfig|null $config */ class User extends Model { protected $table = "users"; public function config() { return $this->hasOne(\App\Models\UserConfig::class, 'uid', 'id'); } } ``` **UserConfig::class** ```php <? php namespace App\Models; /** * \App\Models\UserConfig * * @property int $id * @property int $uid * @property array $locales * ... Omit some attributes */ class UserConfig extends Model { protected $table = "config"; } ``` Sample code: ```php $uid = 1; $userM = User::findOrFail($uid); return [ //If the associated config model cannot find a value, an error will be reported 'locales' => $userM->config->locales ]; ``` ##Solution [Flipping through documents]( https://laravel.com/docs/10.x/eloquent-relationships#default -Models) found such a method ` withDefault()` Interpretation of official documents: > The belongsTo, hasOne, hasOneThrough, and morphOne relationships allow you to define a default model that will be returned if the given relationship is null. This pattern is often referred to as the Null Object pattern and can help remove conditional checks in your code. In the following example, the user relation will return an empty App\Models\User model if no user is attached to the Post model: > >Machine translation into Chinese: > >>If the given relationship is empty, you can define a default model. This mode is usually called 'empty object mode', which can help delete condition checks in code. In the following example, if no user is attached to the Post model, the user relationship will return an empty App Models User model: The usage is simple. Just add the method '->withDefault()' at the end of 'hasOne', and then write the default value on the '$attributes' attribute of' UserConfig:: class' **User::class** ```php <? php namespace App\Models\Admin; /** * \App\Models\User * * @property int $id * @property string $username * ... Omit some attributes * * @property-read \App\Models\UserConfig|null $config */ class User extends Model { protected $table = "users"; public function config() { return $this->hasOne(\App\Models\UserConfig::class, 'uid', 'id')->withDefault(); } } ``` **UserConfig::class** ```php <? php namespace App\Models; /** * \App\Models\UserConfig * * @property int $id * @property int $uid * @property array $locales * ... Omit some attributes */ class UserConfig extends Model { protected $table = "config"; protected $attributes = [ 'locales' => ['en'] ]; } ``` ```php $uid = 1; $userM = User::findOrFail($uid); return [ //If the associated config model cannot find a value, the default value ['en '] will be returned 'locales' => $userM->config->locales ]; ```