Ich versuche Polymorphismus Assoziation in Laravel zu implementieren, aber es zeigt mir nicht Ergebnis oder Fehler.Polymorphismus Assoziation Laravel
Tabelle stu:
CREATE TABLE IF NOT EXISTS `posts` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`content` varchar(255) NOT NULL,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
INSERT INTO `posts` (`id`, `name`, `content`, `created_at`, `updated_at`) VALUES
(1, 'php post', 'Php mysql javascript', '2016-06-06 07:16:49', '0000-00-00 00:00:00'),
(2, 'node', 'express node js', '2016-06-06 07:16:49', '0000-00-00 00:00:00');
CREATE TABLE IF NOT EXISTS `taggables` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`taggable_id` int(11) NOT NULL,
`tag_id` int(11) NOT NULL,
`taggable_type` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
INSERT INTO `taggables` (`id`, `taggable_id`, `tag_id`, `taggable_type`) VALUES
(1, 1, 1, 'Post'),
(2, 2, 2, 'Video');
CREATE TABLE IF NOT EXISTS `tags` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;
INSERT INTO `tags` (`id`, `name`, `created_at`, `updated_at`) VALUES
(1, 'ruby tag', '2016-06-06 07:20:00', '0000-00-00 00:00:00'),
(2, 'javascript tag', '2016-06-06 07:20:00', '0000-00-00 00:00:00'),
(3, 'rails tags', '2016-06-06 07:20:25', '0000-00-00 00:00:00');
eine andere Tabelle ist Video
Modell
<?php
//Post.php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model {
public function tags()
{
return $this->morphToMany('App\Tag', 'taggable');
}
}
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Tag extends Model {
public function posts()
{
return $this->morphedByMany('App\Post', 'taggable');
}
}
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Tag extends Model {
public function posts()
{
return $this->morphedByMany('App\Post', 'taggable');
}
}
class PostController extends Controller {
function index() {
$id = 1;
$a = Post::all();
return view('Pages.index',compact('a'));
}
}
Ansicht
<table>
<tr>
<td width="100px">Id </td>
<td>Name</td>
</tr>
<?php foreach ($a as $value) { ?>
<tr>
<td>
<?php print_r($value->id);?>
</td>
<td>
<?php print_r($value->name);?>
</td>
<td> <?php dd($value->tags()); ?>
<td>
</tr>
<?php } ?>
<table>
Assoziierte Daten in die Ansicht nicht angezeigt :(schauen Sie bitte in die Dies ein nd lassen Sie mich wissen, wo ich falsch bin