Home > Blockchain >  Unable to Store Data in MySql Database using Laravel and React
Unable to Store Data in MySql Database using Laravel and React

Time:12-10

I am a Node.js Dev and I am having a hard time with Laravel, I am following a Tutorial but when i try to store my data it gives me this Error:

Error: Request failed with status code 500

My Table Name is: _donor_sign_up. I assume that something is wrong with controller code.

Here is The React Code:

const PostData =async (e) => {
        e.preventDefault();
        console.log("Posting");
        const res = await axios.post('/signups', user);
        console.log(res);
        
    }

Controller Code:

public function store(Request $request)
    {
        // return response()->json("Hello World");
        $newSignup = _donor_sign_up::create([
            'fname' => $request->fname,
            'lname' => $request->lname,
            'email' =>$request->email,
            'password' =>$request->password,
            're'=>$request->re,
            'role'=>$request->role
        ]);

        if($newSignup)
        {
            return response()->json("Hello World");
        }
        
    }

Web.php code:

Route::post("/signups", "App\Http\Controllers\signupController@store");

Model code:

class _donor_sign_up extends Model
{
    protected $fillable = ["fname", "lname", "email","password","re","role"];
}

CodePudding user response:

Since you will have to define the table name for this model I would name the model in a more conventional way as DonorSignUp then you can define the table name property on the Model so it knows what the table is named (since your table name does not follow convention; not plural):

protected $table = '_donor_sign_up';

Laravel 8.x Docs - Eloquent - Eloquent Model Conventions - Table Names

If you were following conventions the model would be DonorSignUp and the table would be named donor_sign_ups.

  • Related