Anonymous Chatbox
Anonymous Chatbox

Reputation: 481

Unable to pass hidden value to databse in laravel

I want to pass in a the parameter name "ID" from my route to my database column "created_by" as i hit the submit button. I am using a hidden field in my form to perform this task. Can you please tell me where am i going wrong.

Here Are My Routes:-

Route::get('/post/{id}', 'PostsController@create');
Route::post('/p', 'PostsController@store');

Here's My PostsController:-

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Post;
use App\user;

class PostsController extends Controller
{

public function create(Request $request)
    {
      return view('posts.CreatePost', compact('$request'));

    } 

  public function store(Request $request)
    {
post::create([
              'title'=>$request->title,
              'body'=>$request->body,
              'user_id'=>Auth::user()->id,
              'filled_by'=>Auth::user()->id,
              'created_by'=>$request->created_by  ,
            ]);
return redirect('/profile/' . auth()->user()->id);


}
}

Here's My CreatePost View:-


@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">
                <div class="card-header"><h2>{{ __('Info') }}</h2></div>

                <div class="card-body">
                    <form method="POST" action="/p">
                        @csrf

                        <div class="form-group row">
                            <label for="title" class="col-md-4 col-form-label text-md-right">{{ __('Title') }}
                            </label>

                            <div class="col-md-6">
                                <input id="title"
                                name="title"
                                type="text"
                                class="form-control @error('title') is-invalid @enderror"
                                 value="{{ old('title') }}" required autocomplete="title" autofocus>

                                @error('title')
                                    <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                                @enderror
                            </div>
                        </div>

                        <div class="form-group row">
                            <label for="body" class="col-md-4 col-form-label text-md-right">{{ __('Body') }}</label>

                            <div class="col-md-6">
                                <input id="body"
                                name="body"
                                type="text"
                                class="form-control @error('body') is-invalid @enderror"
                                 value="{{ old('body') }}" required autocomplete="body" autofocus>

                                @error('body')
                                    <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                                @enderror
                            </div>
                        </div>

                        <input type="hidden" id="created_by" name="created_by" value="{{$request->id}}"

                        <div class="form-group row mb-0">
                            <div class="col-md-6 offset-md-4">
                                <button type="submit" class="btn btn-primary">
                                    {{ __('Submit') }}
                                </button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

Here's My Posts Migration:-

  public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
          $table->id();
          $table->unsignedBigInteger('user_id');
          $table->string('title');
          $table->text('body');
          $table->unsignedBigInteger('created_by');        
          $table->unsignedBigInteger('filled_by')->nullable();
          $table->timestamps();

          $table->index('user_id');
        });
    }

Here's a Picture Of DD:- enter image description here

It was passing the authenticated id to the filled_by column before i had added the hidden field and created_by column

i am just a beginner please help me it would really mean a lot

-ThankYou

Upvotes: 0

Views: 874

Answers (2)

Oludotun
Oludotun

Reputation: 107

What I suggest you do is add an $id param to your create function like so

public function create($id)
{
      return view('posts.CreatePost', [
            'id' => $id,
        ]);

}

Then pass the id to the hidden field in your blade file like so

<input type="hidden" id="created_by" name="created_by" value="{{ $id}}"

your store method should work as is, provided you have an authorized logged in user Auth::user().

Upvotes: 1

helderneves91
helderneves91

Reputation: 975

To store the ID of the user that posted, I would do:

post::create([
  'title'=>$request->title,
  'body'=>$request->body,
  'user_id'=>Auth::user()->id,
  'filled_by'=>Auth::user()->id,
  'created_by'=> Auth::user()->id,
]);

In this way, you don't need to pass $request in the create function.

I think that is what you want to accomplish...

Upvotes: 1

Related Questions