dongsou8980 2014-06-15 06:29
浏览 13
已采纳

为作者添加新书

I'm new to Laravel so the solution to this may be quite simple. I have an already created Author and I'm trying to add a new book for that Author.

For example, on the Author view there will be an "Add new Book" button. When clicked, you are taken to the "Create new Book" view with the Author id already provided from the previous view.

Author model has a hasMany to Books
Books model has a belongsTo Author

What is the cleanest way to achieve this?

  • 写回答

1条回答 默认 最新

  • douqian8238 2014-06-15 06:34
    关注
    $book= new Book(array('name' => 'your new book here'));
    
    $author= Author::find(1);
    
    $book= $author->books()->save($book);
    

    You can read more about inserting related models here in the official Laravel docs.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办