Php 未定义的属性:Illumb\Database\Eloquent\Collection::$laravel
嘿,伙计们,我用的是拉威尔的多态关系。我在做这样的事。我正在用评论和帖子在喜欢的人之间建立多态关系。我面临的问题是,当我试图根据帖子或评论检索喜欢的内容时,它会给我一个错误 未定义的属性:Illumb\Database\Eloquent\Collection::$likes 我不明白我做错了什么。这是我的密码。请告诉我我做错了什么 岗位控制员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
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...
}