zoukankan      html  css  js  c++  java
  • laravel管理模型插入

    post控制器
    public function comment(Post $post,Request $request)
    {
    try{
    if(empty($request->content)){
    ExceptionResult::throwException(ErrorCode::COMMENT_EMPTY);
    }

    $comment = new Comment();
    $comment->content = $request->content;
    $comment->user_id = Auth::id();
    $res = $post->comments()->save($comment);
    //还有种
    $comment->post_id = $post->id;
    $res = $comment->save();
    if($res){
    return Redirect::back()->with('success', '评论成功');
    } else {
    ExceptionResult::throwException(ErrorCode::ACTION_FAIL);
    }

    }catch (ExceptionResult $e){

    return Redirect::back()->with('error', $e->getMessage())->withInput();
    }
    }


    post模型
    <?php

    namespace AppModels;

    use IlluminateDatabaseEloquentModel;

    class Post extends Model
    {
    //

    protected $table = "posts";
    public $primaryKey = 'id';

    public function user()
    {
    return $this->belongsTo("AppModelsUser","user_id",'id');
    }

    public function comments()
    {
    return $this->hasMany('AppModelsComment','post_id','id')->orderBy("created_at",'desc');
    }
    }
  • 相关阅读:
    登陆界面
    信号和槽
    线程同步
    java script简介
    css粘性定位sticky的使用
    vue中使用qrcodejs2生成二维码
    webpack基本使用
    总结一些h5出现的问题及解决方案
    srcset属性配合w宽度描述符配合sizes属性
    vw实现页面布局
  • 原文地址:https://www.cnblogs.com/brady-wang/p/11688968.html
Copyright © 2011-2022 走看看