2016-06-06 11 views
0

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

Antwort

0

Für einzelne Tag, das Sie etwas tun könnte dies:

$tag = Tag::with(['posts', 'videos'])->find(1); 
$relations = $tag->getRelations(); 

$posts = $relations['posts']; // Collection of Post models 
$videos = $relations['videos']; // Collection of Video models 

$allRelations = array_merge($posts->toArray(), $videos->toArray()); 

Für mehrere Tag müssen Sie eine benutzerdefinierte Modellfunktion wie folgt schreiben:

public function withTag($tag) 
{ 
    foreach (func_get_args() as $arg) { 
     $tags = ($arg instanceof Collection) ? $arg->all() : is_array($arg) ? $arg : array($arg); 

     foreach ($tags as $t) { 
      if (is_object($t)) { 
       $this->filters[] = array('tag_id' => $t->id); 
      } 
      else { 
       $this->filters[] = array('tag' => $t); 
      } 
     } 
    } 

    return $this; 
} 
0

Die Werte in der Spalte *_type müssen Namespaced sein. So dies ändern:

INSERT INTO `taggables` (`id`, `taggable_id`, `tag_id`, `taggable_type`) VALUES 
(1, 1, 1, 'Post'), 
(2, 2, 2, 'Video'); 

zu

INSERT INTO `taggables` (`id`, `taggable_id`, `tag_id`, `taggable_type`) VALUES 
(1, 1, 1, 'App\Post'), 
(2, 2, 2, 'App\Video'); 
0

wie dies Ihr Beitrag Modell ändern.

class Post extends Model { 
    public function taggable() 
    { 
     return $this->morphToMany(); 
    } 
}