在控制器中有条件地调用 Laravel 访问器吗?

huangapple go评论68阅读模式
英文:

Laravel: Conditionally call laravel accesor when I want in controller?

问题

以下是翻译的代码部分:

public function getChatReceiverAttribute()
{
    $request = resolve(Request::class);
    $user_id = $request->get('user_id', false);

    if($user_id)
    {
        if($user_id == $this->user->id)
        {
            return 
            [
                'id' => $this->receiver->id,
                'name' => $this->receiver->name,
                'lat' => $this->receiver->lat,
                'lng' => $this->receiver->lng,
                'images' => $this->receiver->images,
                'sexuality' => $this->receiver->sexuality,
                'birthYear' => $this->receiver->birthYear,
                'birthMonth' => $this->receiver->birthMonth,
                'birthDay' => $this->receiver->birthDay,
                'gender' => $this->receiver->gender,
                'fcm_token' => $this->user->fcm_token,
                'languageCode' => $this->user->languageCode,
                'mood' => $this->user->mood,
                'description' => $this->user->description,
            ];
            
        }
        else
        {
            return 
            [
                'id' => $this->user->id,
                'name' => $this->user->name,
                'lat' => $this->user->lat,
                'lng' => $this->user->lng,
                'images' => $this->user->images,
                'sexuality' => $this->user->sexuality,
                'birthYear' => $this->user->birthYear,
                'birthMonth' => $this->user->birthMonth,
                'birthDay' => $this->user->birthDay,
                'gender' => $this->user->gender,
                'fcm_token' => $this->user->fcm_token,
                'languageCode' => $this->user->languageCode,
                'mood' => $this->user->mood,
                'description' => $this->user->description,
            ];
            
        }
    }
    else
    {
        return [];
    }
}

感谢您的提问。

英文:

in my laravel app controller I want to retrieve chats where a user participates, I append user id to the request itself and then in controlelr I create a laravel model accesor called chat_receiver, if I append this accesor everytime I retrieve the chat model I get the chat_receiver too, that way I don't retrive both chat participants (user_id,receiver_id), howver I DON'T ALWAYS WANT to get accesor.

So the question, is there a way to call the accesor in controller whenever I need instead of appending it, like how I can use Model::with([ 'relationship ])...

My accessor on Chat Model:

public function getChatReceiverAttribute()
{
$request = resolve(Request::class);
$user_id = $request->get('user_id', false);
if($user_id)
{
if($user_id == $this->user->id)
{
//return $this->receiver;
return 
[
'id' => $this->receiver->id,
'name' => $this->receiver->name,
'lat' => $this->receiver->lat,
'lng' => $this->receiver->lng,
'images' => $this->receiver->images,
'sexuality' => $this->receiver->sexuality,
'birthYear' => $this->receiver->birthYear,
'birthMonth' => $this->receiver->birthMonth,
'birthDay' => $this->receiver->birthDay,
'gender' => $this->receiver->gender,
'fcm_token' => $this->user->fcm_token,
'languageCode' => $this->user->languageCode,
'mood' => $this->user->mood,
'description' => $this->user->description,
];
}
else
{
//return $this->user;
return 
[
'id' => $this->user->id,
'name' => $this->user->name,
'lat' => $this->user->lat,
'lng' => $this->user->lng,
'images' => $this->user->images,
'sexuality' => $this->user->sexuality,
'birthYear' => $this->user->birthYear,
'birthMonth' => $this->user->birthMonth,
'birthDay' => $this->user->birthDay,
'gender' => $this->user->gender,
'fcm_token' => $this->user->fcm_token,
'languageCode' => $this->user->languageCode,
'mood' => $this->user->mood,
'description' => $this->user->description,
];
}
}
else
{
return [];
}
}

Thanks in advance.

答案1

得分: 0

不要添加到appends数组中。当需要时,可以在程序中添加它。如果查看Model特性HasAttributes.php的代码,您可以看到可以根据需要添加附加属性。为此,使用了高阶函数集合助手。

Model::where('something', 'something')
    ->get()
    ->each
    ->append(['chat_receiver']);

为单个模型添加它将是:

$model->append(['chat_receiver']);
英文:

Instead of adding it to the appends array. You can add it programatically when you need it. If you look at the code for the Model trait HasAttributes.php. You can see you can add appended attributes on demand. For this is used the higher order function collection helper.

Model::where('something', 'something')
->get()
->each
->append(['chat_receiver']);

Adding it for a single model would be.

$model->append(['chat_receiver']);

huangapple
  • 本文由 发表于 2023年3月15日 20:32:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/75744737.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定