mail = $mail; } /** * Display a listing of the resource, in the form of a comment tree: each parent comment with its child comments in a nested array * * @param int $catId * @param int $kwId * * @return array */ public function indexTree($catId=0, $kwId=0) { $listArrComments=array(); // user has a name OR facebook_id $arrSelectFields=array('comments.*', DB::raw('CASE WHEN name IS NULL THEN facebook_id ELSE name END AS name')); if($kwId){ // comments are related to a kw $listArrCommentsTmp = kw::find($kwId)->comments()->with('user')->orderBy('created_at', 'desc')->get()->toArray(); }else{ // comments are related to category $listArrCommentsTmp = cat::find($catId)->comments()->with('user')->orderBy('created_at', 'desc')->get()->toArray(); } // add child comments to $listArrComments foreach($listArrCommentsTmp as $index=> $arrComment){ $listArrComments[$index]=$arrComment; $listArrComments[$index]['childComments'] = comment::find($arrComment['id'])->children()->with('user')->get()->toArray(); } return $listArrComments; } /** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Request $request) { $kwId=false; $catId=false; // get user object of the commenting user $obj_user=Auth::user(); // validate $this->validate($request, [ 'comment' => 'required' ]); // save comment $this->dispatch(new CreateComment($request->all(), $obj_user)); // try to get parent comment if($request->parentId) { $objParentComment=comment::find($request->parentId); if($objParentComment->kwId){ // parent comment is connected bij $kwId, set $kwId $kwId=$objParentComment->kwId; }else{ // parent comment is connected bij catId, set $catId $catId=$objParentComment->catId; } }else{ // comment has no parent comment, comment is connected bij its own $kwId or own $catId $kwId=$request->kwId; $catId=$request->catId; } // get url for admin to edit comments. $adminUrl for a comment that is related to a $kwId is different from $kwId that is related to a $catId if($kwId){ // get connected kw object to compose url admin url $objKw=kw::find($kwId); $objParentCat=cat::find($objKw->catId)->parent; // get url for admin $adminUrl=getenv('BASE_URL_WEB').url_::get_url_spot($objKw->place, $objParentCat->title, $objKw->title); }else{ // get connected catId, to compose admin url $objSubcat=cat::find($catId); $objParentCat=$objSubcat->parent; if(is_object($objParentCat)){ // category has a parent category. Parent category is needed to compose admin url // get url for admin $adminUrl=getenv('BASE_URL_WEB').url_::get_url_subcat(NULL, $objParentCat->title, $objSubcat->title); }else{ // get url for admin $adminUrl=getenv('BASE_URL_WEB').url_::getUrlCat(null, $objSubcat->title); } } // notify admin of added comment, if on production server if (App::environment('production')){ // send mail $this->mail->commentAdded(array( 'adminUrl' => $adminUrl, 'catTitlePath' => cat::getTitlePath($catId), 'comment' => $request->comment )); } Session::flash('alert-success', 'Comment successfully added '); return redirect()->back(); } /** * Remove the specified resource from storage. * * @param int $id * @return \Illuminate\Http\Response */ public function destroy($id) { permission::is_allowed(); comment::destroy($id); Session::flash('alert-success', 'comment deleted'); return redirect()->back(); } } // comment model belongsTo('App\kw'); } public function User() { return $this->belongsTo('App\User'); } public function parent() { return $this->belongsTo('App\comment', 'parentId'); } public function children() { return $this->hasMany('App\comment', 'parentId'); } } // views: comments.blade.php: