iT邦幫忙

2019 iT 邦幫忙鐵人賽

DAY 27
0
自我挑戰組

來用Laravel做點什麼吧系列 第 27

D27 超簡易版FB - 留言(1)

有鑒於貼文修改真的有夠難(明明是自己太菜),總之我不給修改留言的功能!!!哼!!!!

基本上留言的功能幾乎可以用貼文的方式去處理,只是要考慮資料怎麼傳遞跟顯示。

Comment Migration設計:

Schema::create('comments', function (Blueprint $table) {
    $table->increments('comment_id');
    $table->integer('post_id');
    $table->integer('user_id');
    $table->string('message');
    $table->timestamps();
});

Model一如往常的只有一行protected $table = 'comments';

Route規劃:

Route::post('comment/create', 'CommentController@create')->name('c_create');
Route::post('comment/delete/{id}', 'CommentController@delete')->name('c_destroy');

新增了一個CommentController,不過show方法是修改原本的UserController。

// UserController.php
public function show()
{
    $post = DB::table('posts')
                ->leftJoin('users', 'posts.user_id', '=', 'users.id')
                ->orderBy('posts.post_id', 'DESC')
                ->get();
    $comment = DB::table('comments')
                ->leftJoin('users', 'comments.user_id', '=', 'users.id')
                ->select('comments.*', 'users.name')
                ->get();
    return view('home')->with('posts', $post)->with('comments', $comment);
}

留言的Create和Delete完全複製貼文的程式,修改了資料庫操作的部分而以,這裡就不貼著佔空間了XDDDDDDDD

View的部分新增了一個comment.blade.php作為子視圖給Home使用,幾乎都是複製之前做好的貼文功能。然而測試後發現沒有控制到讓留言只顯示在post_id相符的貼文上,又要修改了orz


上一篇
D26 超簡易版FB - CRUD(4)
下一篇
D28 超簡易版FB - 留言(2)
系列文
來用Laravel做點什麼吧30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言