2015-04-21 19 views
9

Đầu tiênLưu dữ liệu vào cơ sở dữ liệu trong Laravel 5

Tôi đã viết một tập lệnh di chuyển.


Second

Tôi chạy php artisan migrate để di chuyển bảng vào cơ sở dữ liệu của tôi.


Cơ sở dữ liệu

Bây giờ, tôi có một bảng subscribes trong cơ sở dữ liệu của tôi. Nó có 2 trường: idemail.

enter image description here


Route

Route::post('/subscribe', array('as' =>'subscribe','uses'=>'[email protected]'));


Mẫu

<?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); 
    } 

} 

khiển

<?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.'); 

     } 
} 

?> 

Lỗi

enter image description here


Câu hỏi

Tại sao tôi không thể làm $subscribe = new Subscribe;?

Thực tiễn tốt nhất để chèn dữ liệu vào cơ sở dữ liệu bằng Laravel 5 là gì?


Cập nhật

Nhờ @ Mark Baker. Dường như tôi có vấn đề với không gian tên của mình.

Khoảng cách tên này hơi khó hiểu với tôi ngay bây giờ. Ai đó có thể xin vui lòng làm rõ hoặc giải thích một chút?

Mọi thứ được đánh giá cao.

Xin cảm ơn trước.

+2

Mô hình Đăng ký của bạn nằm trong không gian tên 'Ứng dụng'; bạn không đề cập đến không gian tên cho bộ điều khiển của bạn: vì vậy có lẽ '$ subscribe = new \ App \ Subscribe;' –

+0

Có vẻ như nó sửa lỗi của tôi. : D. Làm thế nào để sửa lỗi này nếu tôi không muốn sử dụng '\ App \' trước khi đăng ký. – ihue

+0

Nếu bạn không muốn khai báo không gian tên mỗi khi bạn khởi tạo một mô hình Đăng ký, thì đừng đặt tên cho nó, hoặc đặt 'sử dụng' ở đầu Bộ điều khiển của bạn –

Trả lời

8

Dưới đây là tổng quan cấp cao về cách các không gian tên hoạt động trong PHP để thử và giúp bạn hiểu điều này và cung cấp cho bạn giải pháp cho vấn đề của mình.

<?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. 
    } 
} 
+0

Câu trả lời này giúp tôi hiểu thêm về cách đặt tên không gian. Cảm ơn bạn. – ihue

1

Bạn có thể thử này, Đơn giản chỉ cần sử dụng nó:

$subscribe  = new App\Subscribe; 
+0

Xem bình luận đầu tiên, @Mark đã gợi ý cho tôi điều đó. – ihue

0

Sử dụng App\Subscribe;.

Sau đó, bạn có thể sử dụng $subscribe = new Subscribe; trong mã của mình.

Các vấn đề liên quan