1

I have a form that will send data like title, demo, text. These fields can be inserted by the user but I have user_id in the table that should automatically be inserted by the framework itself. I don't want to create <input type='hidden' name='user_id' value='{{Auth::user()->id}}'> because it is NOT safe at all. How can I fill up user_id automatically when a user has to fill the form?

Controller

public function store(Request $request)
{
    //TODO: ADD user_id to this data and insert into tbl
    $v_data = $request->validate([
        'title' => 'required|min:5',
        'demo' => 'required',
        'text' => 'required',
        'category' => 'required|numeric',
    ]);

    Post::create($v_data);

    return redirect()->back();
}
1
  • Please show the code in your controller. Commented Dec 24, 2019 at 6:03

2 Answers 2

2

Instead of:

Post::create($v_data);

you can use:

Post::create($v_data + ['user_id' => auth()->id()]);
Sign up to request clarification or add additional context in comments.

1 Comment

wow .. i didn't know it was just simple array .. thanks dude.
0

Just do it..

public function store(Request $request) {

//TODO: ADD user_id to this data and insert into tbl
$v_data=$request->validate([
    'title'=>'required|min:5',
    'demo'=>'required',
    'text'=>'required',
    'category'=>'required|numeric',
]);

$v_data['user_id']=auth()->user()->id;// this extra line

Post::create($v_data);
return redirect()->back();

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.