php 在 Laravel 5 中将数据保存到数据库中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29776773/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:34:32  来源:igfitidea点击:

Save data into database in Laravel 5

phpdatabaselaravellaravel-5

提问by cyber8200

First

第一的

I wrote a migration script.

我写了一个迁移脚本。



Second

第二

I run the php artisan migrateto migrate the table into my database.

我运行php artisan migrate将表迁移到我的数据库中。



Database

数据库

Now, I have a subscribestable in my database. It has 2 fields : id, and email.

现在,subscribes我的数据库中有一个表。它有 2 个字段:id, 和email

enter image description here

在此处输入图片说明



Route

路线

Route::post('/subscribe', array('as' =>'subscribe','uses'=>'AccountController@postSubscribe'));

Route::post('/subscribe', array('as' =>'subscribe','uses'=>'AccountController@postSubscribe'));



Model

模型

<?php namespace App;

use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;

class Subscribe extends Model {

    protected $table = 'subscribes';


    //Validation Rules and Validator Function
    public static function validator($input){

        $rules = array(
            'email'    =>'required|email'
        );

        return Validator::make($input,$rules);
    }

}


Controller

控制器

<?php namespace App\Http\Controllers;

use Input, Validator, Auth, Redirect;

class AccountController extends Controller {

    public function postSubscribe() {

            $subscribe        = new Subscribe; <-------- Line#46 (BUG HERE)
            $subscribe->email = Input::get('email');
            $subscribe->save();

            dd("Hi");

            return Redirect::to('/')
                ->with('success','You have been successfully subscribe to us.');

        }
}

?>


Error

错误

enter image description here

在此处输入图片说明



Questions

问题

Why can't I do $subscribe = new Subscribe;?

为什么我做不到$subscribe = new Subscribe;

What is the best practice to insertdata into database using Laravel 5 ?

使用 Laravel 5数据插入数据库的最佳实践是什么?



Update

更新

Thanks to @Mark Baker. It seems that I have an issue with my namespace.

感谢@Mark Ba​​ker。我的命名空间似乎有问题。

This namspacing is a bit confusing to me right now. Can someone pleaseclarify or explain that a bit ?

这个 namspacing 现在让我有点困惑。可有人澄清或稍微解释一下吗?

Anything is appreciated.

任何事情都值得赞赏。

Thanks in advance.

提前致谢。

采纳答案by David Barker

Here is a high level overview of how namespaces work in PHP to try and help you understand this and give you a solution to your problem.

下面是对命名空间在 PHP 中如何工作的高级概述,以尝试帮助您理解这一点并为您提供解决问题的方法。

<?php

// This is the namespace of this file, as Laravel 5 uses PSR-4 and the
// App namespace is mapped to the folder 'app' the folder structure is
// app/Http/Controllers
namespace App\Http\Controllers;

// Use statements. You can include classes you wish to use without having
// to reference them by namespace when you use them further on in this
// namespaces scope.
use App\Subscribe;

class MyController extends BaseController 
{
    public function postSubscribe()
    {
        // You can now use the Subscribe model without its namespace
        // as you referenced it by its namespace in a use statement.
        $subscribe = new Subscribe();

        // If you want to use a class that is not referenced in a use
        // statement then you must reference it by its full namespace.
        $otherModel = new \App\Models\Other\Namespace\OtherModel();

        // Note the prefixed \ to App. This denotes that PHP should get this
        // class from the root namespace. If you leave this off, you will
        // reference a namespace relative to the current namespace.
    }
}

回答by Inspire Shahin

You can try this, Simply use it :

你可以试试这个,只需使用它:

$subscribe        = new App\Subscribe;

回答by Alp Altunel

Use App\Subscribe;.

使用App\Subscribe;.

Then you can use $subscribe = new Subscribe;in your code.

然后你可以$subscribe = new Subscribe;在你的代码中使用。