dongyuling0312 2016-08-26 02:49
浏览 15
已采纳

Laravel试图将用户ID存储到posts表中

public function store(Request $request)
{
   $this->validate($request, array(
       'title' => 'required|max:255|min:2',
       'slug' => 'required|alpha_dash|unique:articles,slug',
       'category_id' => 'required|integer',
       'content' => 'required|min:2'
   ));

   $article = new Article;
   $article->title = $request->title;
   $article->slug = $request->slug;
   $article->category_id = $request->category_id;

   $article->content = $request->content;
   $article->save();

   Session::flash('success', 'Your article has been published.');
   return redirect()->route('article.show', $article->id);
}

There is a user_id column on the articles table,

This is my store function but when I create the post I am looking for a way to store the logged in user id to the user_id column in the articles table.

  • 写回答

3条回答 默认 最新

  • dpy87530 2016-08-26 03:00
    关注

    Get the authed user.

    $article->user_id = Auth::user()->id;
    $article->save();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)
  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失