无法通过自定义函数验证 auth::user() 是否具有对象



我正在尝试验证经过身份验证的用户是否喜欢该帖子(或链接)。

我有一个like模型,带有user_idlikeable_idlikeable_type(因为用户应该能够喜欢两个独立模型的帖子或链接)

AppUser.php

public function likes() {
    return $this->hasMany('AppLike');
}
public function likedCustom($str) {
    $likedCustom = new Collection();
    $this->likes()->where([
        'likeable_type' => $str,
    ])->each(function ($like) use ($likedCustom) {
        $likedCustom->push($like->likeable);
    });
    return is_null($likedCustom) ? false: $likedCustom;
}
public function likedEntries() {
    $likedEntries = $this->likedCustom('post');
    $links = $this->likedCustom('link');
    $links->each(function ($link) use ($likedEntries) {
        $likedEntries->push($link);
    });
    return is_null($likedEntries) ? false: $likedEntries;
}

这工作得很好,因为我将AppPost设置为 'post' 并将AppLink设置为 'link'.在此之后,当我使用php artisan tinker时,我能够将一个集合中的所有帖子和链接作为$entries。所以运行AppUser::find(1)->likedEntries()->contains(AppPost::find(1));返回true.

当我尝试在视图上实现此功能时,问题从这里开始。例如,如果经过身份验证的用户喜欢帖子或链接:

@foreach ($entries as $entry)
    @if ( Auth::user()->likedEntries()->contains($entry) )
        <li class="active"> {{ $entry->title }} </li>
    @else
        <li> {{ $entry->title }} </li>
    @endif
@endforeach

所以我猜这可能是对象返回差异的原因。由于我在ViewComposersEntryComposer.php中以这种方式返回它们:

public function compose(View $view)
{
    // $test = Post::all();
    $entries = Post::all();
    $links = Link::all();
    $links->each(function ($link) use ($entries) {
        $entries->push($link);
    });
    $entries = $entries->sortByDesc(function($entry) {
        return $entry->score();
    });
    $view->with('entries', $entries);
    // $view->with('entries', $test); // Didn't work either.
}

我错过了什么?

也:

Psy Shell v0.8.12 (PHP 7.1.10 — cli) by Justin Hileman
>>> AppUser::find(1)->likedEntries()
=> IlluminateSupportCollection {#754
     all: [
       AppPost {#780
         id: "1",
         title: "debitis",
         body: "Quis rerum amet saepe eligendi. Ullam ea consectetur rerum rem repellat qui qui vel. Aspernatur officiis aut tenetur est perspiciatis harum.",
         user_id: "144",
         category_id: "3",
         created_at: "2017-10-23 12:38:34",
         updated_at: "2017-10-23 12:38:34",
       },
       AppLink {#781
         id: "1",
         title: "Numquam consequuntur minima sunt aut vel facilis deleniti et.",
         url: "https://www.dibbert.info/qui-voluptate-ratione-aperiam-et-ullam-perferendis-et",
         user_id: "136",
         category_id: "2",
         created_at: "2017-10-23 12:38:36",
         updated_at: "2017-10-23 12:38:36",
       },
     ],
   }
>>>  AppUser::find(1)->likedEntries()->contains(AppPost::find(1))
=> true
>>>
查看多

态关系,它将大大简化关系逻辑和 2 个自定义函数,用于从用户那里获取喜欢的对象。

无论如何,您需要在对查询执行->where()后执行->get()

public function likedCustom($str) {
    return $this->likes()->where('likeable_type', $str)->get()->map(function ($like) {
        return $like->likeable;
    });
   // This will return a Collection that can be empty, so you 
   // should check against $collection->isEmpty() or ->count()
}    

相关内容

最新更新