码迷,mamicode.com
首页 > 其他好文 > 详细

laravel管理模型插入

时间:2019-10-16 23:11:12      阅读:94      评论:0      收藏:0      [点我收藏+]

标签:ace   _id   with   comment   php   derby   app   ase   base   

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 App\Models;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
//

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

public function user()
{
return $this->belongsTo("App\Models\User","user_id",‘id‘);
}

public function comments()
{
return $this->hasMany(‘App\Models\Comment‘,‘post_id‘,‘id‘)->orderBy("created_at",‘desc‘);
}
}

laravel管理模型插入

标签:ace   _id   with   comment   php   derby   app   ase   base   

原文地址:https://www.cnblogs.com/php-linux/p/11688968.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!