繁体   English   中英

无法从我的 Laravel 网站显示数据库

[英]Not able to display database from my laravel website

这是我的控制器,名为 CommentController.php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Comment;
use Auth;





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

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

/**
 * Store a newly created resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return \Illuminate\Http\Response
 */
public function store(Request $request)
{


    $comment= new Comment();
    $comment->user_id=Auth::user()->id;
    $comment->body=$request->body;
    $comment->save();
    return back();
}

/**
 * Display the specified resource.
 *
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function show()
{
    //

 $comments=comment::all();
 return view('comments',['comments'=>$comments]);
}

/**
 * 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)
{
    //
}
}

这是我名为 create.blade.html 的布局代码

     <html>
     <head>
     <h1>DONE</h1>


    </head>
    <body>
    <div class="row new-post">
        <div class="col-md-6 col-md-offset-3">

            <header><h3>Comments</h3></header>
            <form action="/comments" method="post">
            {{csrf_field()}}
                <div class="form-group">
                    <textarea class="form-control" name="body" id="new-post"            rows="5" placeholder="Your review on above game"></textarea>
                </div>
                <button type="submit" class="btn btn-primary">Post Comment</button>

            </form>
        </div>
    </div>

   <?php foreach($comments as $comment) ?>
   <h1><?php echo $comment ?> <h1>
  </body>
    </html>

这是我的路由代码命名为 web.php

<?php



Route::get('/', function () {
return view('welcome');
});

Auth::routes();

Route::resource('comments','CommentsController');

Route::get('comments.create','CommentsController@show');
?>

现在,我提交的任何数据都被输入到数据库中,但是当我尝试显示该数据时,它给了我错误未定义变量:注释。 提前致谢 :-)

那是因为您没有从create()方法传递$comments变量,它应该如下所示:

public function create()
{
    $comments = comment::all();
    return view('comments.create', ['comments' => $comments]);
}

还要删除Route::get('comments.create' ,因为尝试覆盖资源路由是一个坏主意。使用create()显示表单并使用show()显示一条评论。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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