简体   繁体   中英

Save data into database in Laravel 5

First

I wrote a migration script.


Second

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


Database

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

在此输入图像描述


Route

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

在此输入图像描述


Questions

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

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


Update

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

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

Anything is appreciated.

Thanks in advance.

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

// 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.
    }
}

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

$subscribe        = new App\Subscribe;

Use App\\Subscribe; .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM