0

I am working on social app front end Angualar, Backend laravel and with database Mongodb. I have model like:

Hoots
-----------------
 - _id
 - content
 - publish_id

Article 
-----------------
 - _id
 - content
 - publish_id

Story
-----------------
 - _id
 - content
 - publish_id

Publish
-----------------
 - _id
 - post_id
 - type
 - user_id

Post id in publish belongs to _id in hoots , article and story , where type signify wheather it is hoot , article or story.

I have Model like this

//Article model 
class Article extends Eloquent {

  public function getpublish(){
     return $this->hasMany('Publish','post_id');
    }
  }
 //Story model 
 class Story extends Eloquent {

  public function get_publish(){
     return $this->hasMany('Publish','post_id');
    }
  }

 //Hoots model 
 class Hoots extends Eloquent {

  public function get_publ(){
     return $this->hasMany('Publish','post_id');
    }
  }

//Publish model 
 class Publish extends Eloquent {

  public function getdata(){

     return $this->BelongsTo('Hoots','Article','Story','publish_id');
    }
  }

I am using

  Publish::with('getdata')->where('user_id',Auth::user()->id)->get(); 

using this i can only get publish data along with post_id corresponding data in one model i.e hoots only. I want this from all three tables.

I want to fetch publish model data with their corresponding post_id data.How can i accomplish this o single query using eloquent.

2
  • its really hard to work out what you are asking for. Can you provide a table of the outcome you expect?\ Commented Feb 13, 2014 at 18:24
  • @Gaz_Edge I have updated code. Kindly Review.how can i get publish model data along with post_id corresponding data from three different model having foreign key publish_id? Commented Feb 14, 2014 at 7:41

1 Answer 1

1

I think maybe you haven't got your relationships setup correctly;

//Publish model 
 class Publish extends Eloquent {

  public function hoot(){

     return $this->HasMany('Hoots','publish_id');
    }
  }

  public function article(){

     return $this->HasMany('article','publish_id');
    }
  }

  public function story(){

     return $this->HasMany('stort','publish_id');
    }
  }

Publish::with(array('hoot', 'story', 'article'))->where('user_id',Auth::user()->id)->get(); 
Sign up to request clarification or add additional context in comments.

2 Comments

I don't why but this thing is not working , I am only gettting Id not the content? I am specifying table name inside joins as you illustrated above. database i am using is mongodb
Awesome man. I was trying same thing but with belongsto. hasmany does th charm. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.