draxq02664 2017-09-16 11:51
浏览 56

在Laravel 5.2中显示表格数据

I am developing project management app using Laravel. in my application I have project and one project have many tasks and one task have many comments. so, I have comment table like this,

id    comment  project_id
1       abc         1
2       dcf         1
3       fgt         2
4       fgt         2
5       fhgt        1

this is my comment controller for comment post

public function postNewComment(Request $request, $id, Comment $comment)
{
    $this->validate($request, [
        'comments' => 'required|min:5',
    ]);

    $comment->comments   = $request->input('comments');
    $comment->project_id = $id;
    $comment->user_id    = Auth::user()->id;
    $comment->save();

    return redirect()->back()->with('info', 'Comment posted successfully');
}

and this is my comment model

protected $fillable = ['comments', 'project_id'];

now I need show comments of the each project when I go to the each project. how can do this?

  • 写回答

1条回答 默认 最新

  • dongxin991209 2017-09-16 11:57
    关注

    In your Project model add method

    public comments()
    {
       return $this->hasMany('App\Comment');
    }
    

    Now in blade you can use

    @foreach($project->comments as $comment)
    
    {{-- print your commets here --}}
    
    @endforeach;
    

    Here is documentation https://laravel.com/docs/5.2/eloquent-relationships#one-to-many

    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测