Laravel,Eloquent:关系。 在一行中写两个实体

There are 3 entities. Article, User and Category. Article

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

 public function category()
    {
      return $this->belongsTo(Category::class);
    }
}

User

class User extends Authenticatable{
public function articles()
  {
      return $this->hasMany(Article::class);
  }

Category

class Category extends Model{
public function articles()
  {
    return $this->hasMany(Article::class);
  }
}

To save the user who created the article in the database, I use the following code in the controller:

    class TestController extends Controller
{
    public function test()
    {
      $user = Auth::user();
      $article = new Article(['name' => 'test']);

      //save record
      $user->articles()->save($article);
}

}

But I also need to keep the category of the article in the table with articles. I receive the article category in the controller:

$category = Category::find(2);

But how do I save these two entities ($user and $category) inside $article. I need to have two cells in the articles table - user_id and category_id. Tell me please.

Set the ids manually:

$article->user_id = Auth::id();
$article->category_id = $category->id;
$article->save();

Use the associate method.

class TestController extends Controller
{
    public function test()
    {
        $user = Auth::user();
        $category = Category::first(); // find category

        $article = Article::create(['name' => 'test']); //save record

        $article->user()->associate($user); 
        $article->category()->associate($category);
    }
}