jeudi 4 février 2016

How to retrieve a hasManyThrough in Laravel 5 controller

I have a setup where Spaces have posts, and posts may have links. I need to filter out links that belong to private spaces. Links table does not have a space_id column so it is my understanding that hasMany through could work for getting the links that belong to a space through the posts table.

I did google this, read the docs, and follow the suggested links here on SO but was unable to find anything that showed me the way.

Space.php

public function posts() {
  return $this->hasMany('App\Post');
}

/** 
* A Space can have links through posts.
*
* The first argument passed to the hasManyThrough method is the name of the final model we wish to access
* The second argument is the name of the intermediate model
*
* The third argument is the name of the foreign key on the intermediate model
* The fourth argument is the name of the foreign key on the final model
*
*/

public function links() {
  return $this->hasManyThrough('App\Link', 'App\Post','space_id','post_id');
} 

Post.php

public function links() {
  return $this->hasMany('App\Link');
}

/** A Post belongs to a space.
*
*
*/
public function space() {
  return $this->belongsTo('App\Space');
} 

Link.php

public function post() {
    return $this->belongsTo('App\Post');
}

What I am trying in the links controller is different variations of this

$private = Space::where('spaces.isPublic','=', 0)->get();
$privateLinks = $private->links();

but I have not been able to get it to work and am wondering if someone can show me the path there.

Thanks.



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1P94yxn
via IFTTT

Aucun commentaire:

Enregistrer un commentaire