Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

newbie14's avatar

How to read post value, verify and insert into db?

I have a form which I can post data and I can confirm that is post the data. Thus I have created a function in my controller.

public function registerexam() {

 $InstrumentType = $request->InstrumentType;

} I know this how to read the post value. But say if I need to echo out to confirm in the controller what is the method to do? Thereafter I need to run an insert statement. Will it be like this DB::query('insert into users (username, email, password) values ("johndoe", "[email protected]", "password")'); I am used to the raw method will this be fine?

0 likes
13 replies
ejdelmonico's avatar

Are you speaking of a validator?

As far as using raw queries, I think if it works for you then use it. However, the reason Laravel wraps those queries is mostly for readability besides ease of use. So, I would recommend that you start using Eloquent. You might find it easier to start with database query builder.

ElvisMagagula's avatar

Here's a little example to help you:

public function store(Requests\AddPostRequest $request) 
{
    Post::create($request->all());
        return response()->json([
            'message' => 'Post created successfully!'
        ], 200);
}
newbie14's avatar

Hi All, No I have done my validation via javascript in the view itself. I know my form is able to be submit I just need to just do like normal php form _POST['InstrumentType'] and next I will do an insert query. Basically I need to say my input text name is InstrumentType so I need some kind of echo $InstrumentType = $request->InstrumentType; to check that I have receive the value ?

Another question after I run I this DB::query('insert into users (username, email, password) values ("johndoe", "[email protected]", "password")'); how to check if its successfully inserted?

ctroms's avatar

@newbie14 Even though you have done your validation on the client side, you may want to perform your validation server side too since client side validation can be manipulated without too much effort.

All of your $_POST input will be present in the request. You do not need to store the post data a separate variable to confirm that you've received it. If it is present in the request, you received it.

To submit queries with the query builder, I would encourage you to give a look at the query builder docs and any of the videos covering its use right here on Laracasts. I also agree with @ejdelmonico. Checkout Eloquent. You might find it more helpful to use an ORM like Eloquent or the query builder methods to avoid building raw query strings unless you really have a reason to use them.

newbie14's avatar

Hi ctroms, How to do server side validation with laravel and how to stop the form submission.

How to view the request in my controller method actually is more for debugging purooses.

How to then add field by field into the table for insert?

ctroms's avatar

@newbie14 The docs that @zachleigh referenced will help you with your questions. Also take a look at the Laravel 5 Fundamentals series. Jeffrey covers both validation and Eloquent in that series.

Basically, if you are using Eloquent, this will make an insert into your DB.

$model = new Model();
$model->column = $request->input;
$model->column2 = $request->input2;
$model->save();

If you want to see all of the request data for debugging, you can view all of the input present in your request using dd as @zachleigh suggested.

dd($request->all());
newbie14's avatar

Hi Ctroms, I tried this public function registerexam(Requests\AddPostRequest $request) { dd($request->all()); } but I got this error ReflectionException in Route.php line 339: Class App\Http\Requests\AddPostRequest does not exist

At the top of the controller namespace App\Http\Controllers;

//use Illuminate\Http\Request; use Illuminate\Support\Facades\DB;

use App\Http\Requests; use Illuminate\Http\Request; //use App\Syllabus;

newbie14's avatar

Hi Ctroms , Ok I have managed to resolve that issue of Request error. Now I want to do next two things how to capture the user id whom have login into the system via laravel. Next thing I have been reading regarding the db insert via this link https://laravel.com/docs/master/eloquent#inserting-and-updating-models. I dont understand how is the model created say for example for my insert do I need to create a model $model = new Model(); what is this model and how it represent my table?

ctroms's avatar

@newbie14 To get the current authenticated user you can do this.

use Illuminate\Support\Facades\Auth;

$user = Auth::user();

Here are the auth docs. Give them a read.

The introduction to eloquent in the docs will give you a clear understanding on Eloquent, how to create models, naming conventions, etc. In short,

By convention, the "snake case", plural name of the class will be used as the > table name unless another name is explicitly specified.

Also, next time you post, try to include a code sample of what you have tried. It is much easier to give assistance, and as a result, people will be much more likely to help if they can see what you are trying.

newbie14's avatar

Hi Ctroms, So far I tried this but I am not getting any error or results in the db table either? What could be the issue or gone wrong? public function rexam(Request $request) { dd($request->all()); $id = Auth::id(); $IType=$request->IType; $GID=$request->GID; $MTypeID=$request->MTypeID; $values = array('StudentID' => $id,'IType' => $IType,'GID' => $GID,'MTypeID' => $MTypeID); DB::table('exams')->insert($values); }

newbie14's avatar

Hi Ctroms, Once I commented this //dd($request->all()); now yes I can see it insert into db. My next question is that after inserting say If I need to go into the same earlier form and say that data is succesfully inserted ? What is the best method in laravel like redirect?

ctroms's avatar

At the end of your controller method after you have done your insert, you can flash a message back to your user.

Here is a link to the docs on flashing data.

Jeffrey also did a video on this in the Building Project Flyer series. It is a little older but still will help you i think.

Please or to participate in this conversation.