Helllo guys I'm using the polymorphic relation in laravel. I'm doing somehting like this. I'm making a polymorphic relation between of likes with comment and post. The problem that I'm facing is that when I'm trying to retrieve the likes on the basis of posts or comments it gives me a error which is
Undefined property: Illuminate\Database\Eloquent\Collection::$likes
I can't understand what is it that I'm doing wrong. Here is my code. Please tell me what is it that I'm doing wrong
Post Controller
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 Command class
<?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();
// }
}
}
Read Post Repo class
<?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);
}
}
Like Model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class LikeModel extends Model
{
public $table = "likes";
//
public function likeable()
{
return $this->morphTo();
}
}
Post Model
<?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');
}
}
Comment Model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class CommentModel extends Model
{
//
public $table = "comments";
public function likes()
{
return $this->morphMany('App\LikeModel', 'likeable');
}
}