2017-04-18 55 views
1

我試圖與發佈後用戶在我的應用程序註冊,但這個錯誤發生了: enter image description here列在SQL laravel沒有發現5.4

而且我使用的XAMPP和我的職位表是這幅畫 enter image description here

而這個錯誤是在phpMyAdmin: enter image description here

PostController是:

use App\Post; 

    class PostsController extends Controller 

{ 

public function __construct() 
{ 
    $this->middleware('auth')->except(['index','show']); 
} 

public function index() 
{ 
    $posts=Post::latest()->get(); 

    return view('posts.index',compact('posts')); 
} 

public function show(Post $post) 
{ 

    return view('posts.show',compact('post')); 
} 

public function create() 
{ 
    return view('posts.create'); 
} 

public function store() 
{ 

    $this->validate(request(),[ 
     'title'=>'required', 
     'body' => 'required|min:5' 
    ]); 


    Post::create(request([ 
     'title' => request('title'), 
     'body' => request('body'), 
     'user_id' =>auth()->id() 
     //auth()->user()->id*/ 
    ])); 

    return redirect()->home; 
} 

}

與後型號:

class Post extends Model 
{ 
    public function comments() 
    { 
     return $this->hasMany(Comment::class); 
    } 

    public function user() 
    { 
     return $this->belongsTo(User::class); 
    } 
} 
+0

您能展示Laravel代碼(模型+控制器)來幫助我們進行分析嗎? –

+0

@NapoleãoWingert是的,我編輯了我的問題。 –

回答

3

看來你的商店方法是錯誤的。

嘗試這樣:

public function store() 
{ 

    $this->validate(request(),[ 
     'title'=>'required', 
     'body' => 'required|min:5' 
    ]); 

    Post::create([ 
     'title' => request('title'), 
     'body' => request('body'), 
     'user_id' =>auth()->id() 
    ]); 

    return redirect()->home; 

} 

此代碼對你的作品?

+0

是的!非常感謝你 –

+0

但請解釋我爲什麼我的代碼錯了? –

+0

您的代碼出錯了,因爲您需要將一組信息傳輸到Post的創建方法。該方法需要知道column =>值。以前,您也發送了請求。就這樣... –

2
INSERT into posts (title, body, userId) VALUES ('My title', 'My body', 7) 

第一部分指定的字段名稱,第二部分指定要插入到每個字段中的值。 '我的標題'不是您要插入信息的字段,它顯示爲標題。

此外,createdon使用時間戳,所以你不需要包含它。更新後可以在記錄更改後插入當前時間戳。

+0

請填寫完整如何修復它 –

+0

我不確定上面的聲明中要插入的數據是什麼,但它應該放在上面括號中的VALUES後面的部分。問題出現在你如何命名你的領域。 ('Field1 Value','Field2 Value','Field3 Value') – user3561890