Helllo家伙我在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();
// }
}
}
阅读后回复课程
<?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');
}
}
答案 0 :(得分:1)
这是您的违规代码,位于PostController
:
$post = $this->dispatch(new ReadPostCommand());
error_log(print_r($post->likes , true));
问题是,您的ReadPostCommand
正在返回Collection
个Post
个对象。由于$post
是Collection
,因此当您尝试$post->likes
时,您会收到您所看到的错误。 Collection
没有$likes
属性。您需要访问单个$likes
对象的Post
属性,而不是Collection
。
向下几行,您使用$post
迭代foreach
集合。如果您将error_log
移动到该循环中,它应该可以正常工作:
foreach ($post as $key => $value) {
error_log(print_r($value->likes , true));
// code...
}