I am trying to enter data inside database table via form. I have created an instance of Model and accessed the columns of that table and assigned the input names of form but when I enter data an submit it says Attempt to read property "name" on array. Can anyone guide me if there is anything wrong.
Code of Controller where im inserting datas to database
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\User;
class UserController extends Controller
{
function registerUser(Request $req)
{
$data= $req->input();
$user_model=new User;
$user_model->fullname=$data->name;
$user_model->mobile=$data->mobile;
$user_model->email=$data->email;
$user_model->password=$data->password;
$user_model->save();
return "registered successfully";
}
}
Code of Form
<div class="register-form">
<h4>Sign Up</h4>
<form method="post" action="register">
@csrf
<input type="text" name="name" placeholder="Full Name" size="40">
<br>
<input type="tel" name="mobile" placeholder="Mobile number" size="40">
<br>
<input type="email" name="email" placeholder="Email" size="40">
<br>
<input type="password" name="password" placeholder="Password" size="40">
<br>
<input id="submit_btn" type="submit" placeholder="Sign Up" size="40">
<hr >
<p>Already, have an account <span href="#" id="sign-in-link"> Log In </span> </p>
</form>
</div>
CodePudding user response:
$req->input
is an array. Access it like so.
$user_model->fullname = $data['name'];
$user_model->mobile = $data['name'];
$user_model->email = $data['email'];
$user_model->password = $data['password'];
CodePudding user response:
function registerUser(Request $req)
{
$user_model = new User;
$user_model->fullname = $req->name;
$user_model->mobile = $req->mobile;
$user_model->email = $req->email;
$user_model->password = bcrypt($req->password); //laravel hashed password
$user_model->save();
return "registered successfully";
}