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);

}

```
~~You can resolve the first warning by declaring `eventType` as `Class<? extends A>`. This makes the cast to `A` superfluous.~~

If you cannot change the return type of the first method, there is no option to resolve this compilation issue without resorting to casting. The compilation issue can be resolved by changing the return type of the method to `Event`. If you change the return type to `Event`, you can fully drop the generic parameter:

```
public static Event copy(Event e)
        throws NoSuchMethodException,
            IllegalAccessException,
            InstantiationException,
            InvocationTargetException {
    Class<? extends Event> cls = e.getClass();
    return cls.getConstructor(cls).newInstance();
}

```

---

For the second warning, you can also fully drop the generic type of the method:

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

```

This assumes that `Congress extends Event` and `Exhibition extends Event`.