laravel - Get related posts based on common tag? -


i have posts table has many-to-many relationship tags table, connected using pivot table called tagspivot. show post using following method :

public function showpost($titleslug) {     $post = post::where('titleslug','=',$titleslug)->first();     return view('posts/show', compact('post', $post)); } 

then load post tags in view :

@foreach($post->tags $ptags)   <li><a href="{{route('showtag', $ptags->titleslug)}}" class="button smallgrey">#{{$ptags->title}}</a></li> @endforeach 

my question is, how list of posts has same tags current showing post ? doesn't have exact same tags, other post has 1 or 2 common tags. if possible list sorted post has common tags current showing post.

that's all, sorry bad english

posts table :

public function up() {         schema::create('posts', function (blueprint $table) {             $table->increments('id');             $table->string('title');             $table->text('content');             $table->string('titleslug');             $table->timestamps();         });     } 

tags table :

public function up() {         schema::create('tags', function (blueprint $table) {             $table->increments('id');             $table->string('title');             $table->string('titleslug');             $table->timestamps();         });     } 

tagspivot table :

public function up() {         schema::create('tagspivot', function (blueprint $table) {             // create tabel tagspivot             $table->increments('id');             $table->integer('post_id')->nullable()->unsigned()->index();             $table->integer('tag_id')->nullable()->unsigned()->index();             $table->timestamps();              // set fk tagspivot --- posts             $table->foreign('post_id')                     ->references('id')                     ->on('posts')                     ->ondelete('cascade')                     ->onupdate('cascade');              // set fk tagspivot --- tags             $table->foreign('tag_id')                     ->references('id')                     ->on('tags')                     ->ondelete('cascade')                     ->onupdate('cascade');         });     } 

relationship in post model :

public function tags()     {         return $this->belongstomany('app\tag', 'tagspivot', 'post_id', 'tag_id')->withtimestamps();     } 

relationship in tag model :

public function posts() {         return $this->belongstomany('app\post', 'tagspivot', 'tag_id', 'post_id');     } 

if want post current $titleslug need use wherehas method:

post::wherehas('tags', function ($query) use ($titleslug) {         $query->where('slug', $titleslug);     })->get(); 

this code work in case if write relations properly. more information wherehas , other helpfull relations methods watch this:

querying relationship existence

hope find right solution :)


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -