PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, January 17, 2022

[FIXED] Unable to Store Data in MySql Database using Laravel and React

 January 17, 2022     laravel     No comments   

Issue

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"];
}

Solution

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.



Answered By - lagbox
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing