You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel向MySQL数据库插入数据及Request::input()报错求助

Hey there! Let's sort out that "Call to undefined method Illuminate\Support\Facades\Request::input()" error you're hitting when trying to insert data into your Laravel database.

First, let's break down why this is happening: That error pops up because you're trying to use input() on the Request facade, but either you've imported the wrong class, or you're not using Laravel's request handling the right way. Here's how to fix it, step by step:

Laravel's best practice is to inject the actual request instance into your route closure or controller method, instead of relying on the facade. This is cleaner and aligns with the framework's design.

Example in a Route Closure:

// Make sure you import the correct Request class at the top
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB; // Or your model, whichever you're using

Route::post('/save-data', function (Request $request) {
    // Get input values from the request
    $name = $request->input('name');
    $email = $request->input('email');

    // Option 1: Insert using the DB facade
    DB::table('your_table_name')->insert([
        'name' => $name,
        'email' => $email,
        'created_at' => now(),
        'updated_at' => now(),
    ]);

    // Option 2: Insert using a Model (better for long-term)
    // YourModel::create([
    //     'name' => $name,
    //     'email' => $email,
    // ]);

    return redirect()->back()->with('success', 'Data added successfully!');
});

Example in a Controller:

If you're using a controller instead of a closure, inject the request instance into your method:

// In your controller file
use Illuminate\Http\Request;
use App\Models\YourModel; // Import your model

class DataController extends Controller
{
    public function store(Request $request)
    {
        // You can also use shorthand like $request->name instead of input()
        $data = [
            'name' => $request->name,
            'email' => $request->email,
        ];

        YourModel::create($data);

        return redirect()->route('some.route')->with('success', 'Record saved!');
    }
}

2. If You Insist on Using the Request Facade

If you really want to use the facade, make sure you're importing the right one and using the correct method (though again, dependency injection is preferred):

use Illuminate\Support\Facades\Request;

Route::post('/save-data', function () {
    // Use Request::get() or Request::input() – both work in modern Laravel versions
    $name = Request::get('name');
    // Or $name = Request::input('name');

    // Insert logic here...
});

Important Note for Model Inserts

If you're using a model with create(), don't forget to set the $fillable property in your model to avoid mass assignment errors:

// In your model file (e.g., app/Models/YourModel.php)
class YourModel extends Model
{
    protected $fillable = ['name', 'email']; // List all fields you want to allow for mass assignment
}

That should fix the undefined method error and get your data inserting into the database smoothly. Let me know if you run into any other snags!

内容的提问来源于stack exchange,提问作者hon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 10:15:52