Question: So I learned in JeffreyWay's screencasts that I can use Eloquent to get the associated id from a model injected to another model.

I'm following his series about Laravel 5.4.

Here, I have a one-to-many relationships of user to posts.

**App/Post**

```
public function user()
{
    return $this->belongsTo(User::class);
}

```

In my User Model, I have a publish method where the Post Model is injected. The publish method is used to create a post entry into the database.

**App/User**

```
public function posts()
{
    return $this->hasMany(Post::class);
}

public function publish(Post $post)
{
    $this->posts()->save($post);
}

```

I then have a store method in my PostsController that calls the publish method inside my User Model.

**PostsController**

```
class PostsController extends Controller
{
     public function __construct()
    {
        $this->middleware('auth')->except(['index', 'show']);
    }

    public function store()
    {
        auth()->user()->publish(
           new Post(request(['title', 'body']))
        );
    }
}

```

When the publish method is called, the injected Post class automatically sets the user\_id to the save method.

My question is, how do I make a relationship like this in a situation where for every posts, there are comments. These comments are associated to the Post and the User that created the comment. 

In short, I should have both user\_id and post\_id when I call the addComment method.

Answer: 
Seems like you are misusing generics. If the *method* gets to decide what type to return, you should not use generics. Generics are used when the caller decides what type to return.

So `create` should be like this:

```
public static Event create(EventType type) {
    if(type == EventType.CONGRESS){ 
        return new Congress();
    }else {
        return new Exhibition();
    }
}

```

For the first warning, you can do something like this.

```
public static <A extends Event> A copy(A event) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
    return (A) event.getClass().getConstructor(event.getClass()).newInstance(event);

}

```
I would recommend using TypeTags aka ClassTags. Something like: 

```
public static <A extends Event> Optional<A> create(Class<A> classTag) {
    try {
        return Optional.of(classTag.newInstance());
    } catch (InstantiationException | IllegalAccessException e) {
        return Optional.empty();
    }
}

```

Call it like so:

```
public static void main(String[] args) {
    Optional<Exhibition> e = create(Exhibition.class);
    e.ifPresent(System.out::println);
}

```