White Cat
White Cat

Reputation: 25

laravel collective form - how to send id (foreign key) with form

As in title how to do that? I have table comments, the structure of this table is:

-id -title -contents -user_id

Here is code of my form:

{!! Form::open(['action' => ['LibraryController@newcomment', $sth->myuserid]]) !!}

        {!! Form::label('title', 'Title:') !!}
        {!! Form::text('title', null) !!}

        {!! Form::label('contents', 'Contents:') !!}
        {!! Form::textarea('contents', null) !!}

        {!! Form::submit('Send') !!}

{!! Form::close() !!}

So, comment has the user_id and I have to send it too with form, maybee by this:$sth->myuserid but how can I use it in LibraryController?

Additionally, I found this: http://laravel-recipes.com/recipes/157/creating-a-hidden-input-field is it a good option?

Upvotes: 0

Views: 1001

Answers (3)

White Cat
White Cat

Reputation: 25

Okay, but what if it isn't auth but something else and I want use it:['LibraryController@sth', $sth->sth_id], how to use it in LibraryController ?

Upvotes: 0

YouneL
YouneL

Reputation: 8371

You could use a hidden field to store the user id and then retrieve it in your controller:

{{ Form::hidden('user_id', $user_id) }}

But if you are using Auth component you can get the authenticated user by using the Auth facade:

use Illuminate\Support\Facades\Auth;

...

// Get the currently authenticated user's ID...
$id = Auth::id();

Upvotes: 2

Alexey Mezenin
Alexey Mezenin

Reputation: 163898

If it's for admin panel or something, you can use hidden input:

{!! Form::hidden('user_id', $user->id) !!}

But if you want to pass an ID of a current user, do not pass it through a form. Use auth()->id() in a controller to get ID.

Upvotes: 2

Related Questions