簡體   English   中英

未定義的屬性:Illuminate\\Database\\Eloquent\\Collection::$likes laravel

[英]Undefined property: Illuminate\Database\Eloquent\Collection::$likes laravel

大家好,我在 Laravel 中使用了多態關系。 我正在做這樣的事情。 我正在通過評論和帖子在喜歡之間建立多態關系。 我面臨的問題是,當我嘗試根據帖子或評論檢索贊時,它給了我一個錯誤,即

未定義的屬性:Illuminate\\Database\\Eloquent\\Collection::$likes

我不明白我做錯了什么。 這是我的代碼。 請告訴我我做錯了什么

后控制器

class PostController extends Controller
{
    //
    public function index($id=null){
    // if (is_null($id) || $id=''){
            $post = $this->dispatch(new ReadPostCommand());
            error_log(print_r($post->likes , true));
            $comment = array();
            foreach ($post as $key => $value) {
                // error_log(print_r($value->))

                $comment[$key] = $this->dispatch(new ReadCommentCommand($value["original"]["id"]));
                // error_log(print_r($comment[$key]->likes , true));
            }
            $id = '';
            $courses = $this->dispatch(new ReadCourseCommand("getAll"));
        // }else{
        //  $course = $this->dispatch(new ReadCourseCommand($id , "getById"));
        //  $post=$course->posts;
        //  $comment = array();
        //  foreach ($post as $key => $value) {
        //      $comment[$key] = $this->dispatch(new ReadCommentCommand($value["original"]["id"]));
        //  }

        // }
        // error_log(print_r($courses , true));
        return \View::make('post.index' , ['posts'=>$post , 'comments'=> $comment , 'courses'=>$courses , 'id'=>$id]);
    }
}

READ POST 命令類

<?php

namespace App\Commands;

use App\Commands\Command;
use Illuminate\Contracts\Bus\SelfHandling;
use App\Exceptions\Custom\NotPermissionException;
use App\Exceptions\Custom\NameNotFound;
use App\Repositories\PostRepository;

class ReadPostCommand extends Command implements SelfHandling
{
    /**
     * @var
     */
    // private $action;
    // private $id;

    /**
     * Create a new command instance.
     *
     * @param $id
     */
    public function __construct()
    {
        // $id was in the parameter.
        // $this->id = $id;
        // $this->action = $action;
    }

    /**
     * Execute the command.
     *
     * @param TestRepository $TestRepository
     *
     * @return
     * @throws NotPermissionException
     * @throws NameNotFound
     * @internal param TestRepository $TestRepository
     */
    public function handle(PostRepository $PostRepository)
    {
        // if($this->action == "getAll"){
            $post = $PostRepository->getAll();
            return $post;
        // }else{
        //     $post = $PostRepository->getAllbyCourse();
        // }
    }
}

閱讀 Post Repo 類

<?php

namespace App\Repositories;

use App\PostModel;

class PostRepository
{
    public function create(PostModel $post)
    {
        $post->save();
    }
    public function getAll() {
        return PostModel::get();
    }

    public function getById($id){
        return PostModel::find($id);
    }
}

喜歡模特

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class LikeModel extends Model
{
    public $table = "likes";
    //
    public function likeable()
    {
      return $this->morphTo();
    }
}

崗位模型

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use App\LikeModel;

class PostModel extends Model
{

    //
    public $table = "posts";

    public function likes()
    {

      return $this->morphMany('App\LikeModel', 'likeable');
    }

}

評論模型

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class CommentModel extends Model
{
    //
    public $table = "comments";

    public function likes()
    {
      return $this->morphMany('App\LikeModel', 'likeable');
    }
}

這是您的PostController違規代碼:

$post = $this->dispatch(new ReadPostCommand());
error_log(print_r($post->likes , true));

問題是,你的ReadPostCommand返回一個CollectionPost對象。 由於$post是一個Collection ,當您嘗試$post->likes ,您會看到您看到的錯誤。 Collection沒有$likes屬性。 您需要訪問各個Post對象上的$likes屬性,而不是Collection

再往下幾行,你用foreach迭代你的$post集合。 如果您將error_log移動到該循環中,它應該可以正常工作:

foreach ($post as $key => $value) {
    error_log(print_r($value->likes , true));

    // code...
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM