2016-11-29 49 views
0

從Laravel 5.3插入數據時,它不會顯示在MySQL 5.7.1數據庫中。我正在學習一個教程,老師正在使用Laravel 5.2這是一個問題嗎? 任何幫助,將不勝感激。Laravel 5.3 - MySQL 5.7.1中的數據插入問題

public function store(Request $request) 
    { 
     // validate the data 
     $this->validate($request, array(
      'title' => 'required|max:255', 
      'body' => 'required' 
     )); 

     // store in the database 
     $post = new Post; 

     $post->title = $request->title; 
     $post->body = $request->body; 

     $post->save(); 
     // Session::flash('success', 'The blog post was successfully save!'); 
     return redirect()->route('posts.show', $post->id); 
    } 

添加這裏整個代碼...

<?php 
namespace App\Http\Controllers; 

use Illuminate\Http\Request; 

use App\Http\Requests; 
use App\Http\Controllers\Controller; 
use App\Post; 
use Session; 


class PostController extends Controller 
{ 
/** 
* Display a listing of the resource. 
* 
* @return \Illuminate\Http\Response 
*/ 
public function index() 
{ 

    $posts = Post::all(); 
    return view('posts.index')->withPosts($posts); 
} 

/** 
* Show the form for creating a new resource. 
* 
* @return \Illuminate\Http\Response 
*/ 
public function create() 
{ 
    // 
    return view('posts.create'); 
} 

/** 
* Store a newly created resource in storage. 
* 
* @param \Illuminate\Http\Request $request 
* @return \Illuminate\Http\Response 
*/ 
public function store(Request $request) 
{ 
    // validate the data 
    $this->validate($request, array(
      'title' => 'required|max:255', 
      'body' => 'required' 
     )); 

    // store in the database 
    $post = new Post; 

    $post->title = $request->title; 
    $post->body = $request->body; 

    $post->save(); 

    return redirect()->route('posts.show', $post->id); 


} 

/** 
* Display the specified resource. 
* 
* @param int $id 
* @return \Illuminate\Http\Response 
*/ 
public function show($id) 
{ 
    $post = Post::find($id); 
    return view('posts.show')->withPost($post); 
} 

/** 
* Show the form for editing the specified resource. 
* 
* @param int $id 
* @return \Illuminate\Http\Response 
*/ 
public function edit($id) 
{ 

} 

/** 
* Update the specified resource in storage. 
* 
* @param \Illuminate\Http\Request $request 
* @param int $id 
* @return \Illuminate\Http\Response 
*/ 
public function update(Request $request, $id) 
{ 

} 

/** 
* Remove the specified resource from storage. 
* 
* @param int $id 
* @return \Illuminate\Http\Response 
*/ 
public function destroy($id) 
{ 
    // 
} 
} 
+1

你可以發佈不工作的代碼,我們可以試試看看嗎? – Monkeybrain

+0

@Monkeybrain代碼添加,謝謝你看看這個。 – hamad360

+0

當您運行代碼時,您是否收到任何錯誤或警告?是否所有命名空間都是正確的(例如App \ Post聲明?) – Monkeybrain

回答

1

確保你的$可填寫陣列被填充。

<?php 
namespace App; 

class Post extends Model 
{ 
    protected $fillable = ['title', 'body']; 
    ... 
} 
+0

試過這個不行的:( – hamad360