Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 未定义的属性:Illumb\Database\Eloquent\Collection::$laravel_Php_Laravel_Laravel 5 - Fatal编程技术网

Php 未定义的属性:Illumb\Database\Eloquent\Collection::$laravel

Php 未定义的属性:Illumb\Database\Eloquent\Collection::$laravel,php,laravel,laravel-5,Php,Laravel,Laravel 5,嘿,伙计们,我用的是拉威尔的多态关系。我在做这样的事。我正在用评论和帖子在喜欢的人之间建立多态关系。我面临的问题是,当我试图根据帖子或评论检索喜欢的内容时,它会给我一个错误 未定义的属性:Illumb\Database\Eloquent\Collection::$likes 我不明白我做错了什么。这是我的密码。请告诉我我做错了什么 岗位控制员 class PostController extends Controller { // public function index($i

嘿,伙计们,我用的是拉威尔的多态关系。我在做这样的事。我正在用评论和帖子在喜欢的人之间建立多态关系。我面临的问题是,当我试图根据帖子或评论检索喜欢的内容时,它会给我一个错误

未定义的属性:Illumb\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]);
    }
}
读取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();
        // }
    }
}
<?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);
    }
}

这是您的违规代码,在您的
后控制器中:

$post = $this->dispatch(new ReadPostCommand());
error_log(print_r($post->likes , true));
问题是您的
ReadPostCommand
正在返回
Post
对象的
集合。由于
$post
是一个
集合
,当您尝试
$post->likes
时,您会看到错误。
集合
没有
$likes
属性。您需要访问单个
Post
对象的
$likes
属性,而不是
集合

接下来几行,您使用
foreach
迭代您的
$post
集合。如果将
错误日志
移动到该循环中,它应该可以正常工作:

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

    // code...
}

也很代码。TL;博士
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

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

    public function likes()
    {
      return $this->morphMany('App\LikeModel', 'likeable');
    }
}
$post = $this->dispatch(new ReadPostCommand());
error_log(print_r($post->likes , true));
foreach ($post as $key => $value) {
    error_log(print_r($value->likes , true));

    // code...
}