public function store2(Request $request) { $this->validate($request, ['ft_id' => 'required', 'ftep_id' => 'required', 'content' => 'required_with:title', 'title' => 'max:255']); $favorite = new Ftepfav(); $favorite->user_id = $request->user()->id; $favorite->ftep_id = $request->input('ftep_id'); $favorite->created_at = new Carbon(); if ($request->has('content')) { $review = new Ftrev(); $review->user_id = $favorite->user_id; $review->ft_id = $request->input('ft_id'); $review->ftep_id = $request->input('ftep_id'); $review->title = $request->input('title'); $review->content = $request->input('content'); if ($review->save()) { DB::table('fts')->where('id', $review->ft_id)->increment('reviews'); DB::table('fteps')->where('id', $review->ftep_id)->increment('reviews'); } else { return redirect()->back()->withInput()->withErrors('添加失败'); } } if ($favorite->save()) { DB::table('fteps')->where('id', $favorite->ftep_id)->increment('favorites'); return redirect()->route('ftep.show', [$favorite->ftep_id]); } else { return redirect()->back()->withInput()->withErrors('评论添加成功,收藏添加失败!'); } }
public function index() { $favorites = Ftfav::with(['user' => function ($query) { $query->select('id', 'name'); }, 'ft' => function ($query) { $query->select('id', 'title'); }])->select('user_id', 'ft_id', 'created_at')->orderBy('created_at', 'desc')->take(50)->get(); $epfavs = Ftepfav::join('fteps', function ($join) { $join->on('fteps.id', '=', 'ftepfavs.ftep_id'); })->join('fts', function ($join) { $join->on('fts.id', '=', 'fteps.ft_id'); })->select('user_id', 'ft_id', 'fts.title as ft_title', 'ftep_id', 'fteps.title as ftep_title', 'ftepfavs.created_at as created_at')->orderBy('ftepfavs.created_at', 'desc')->take(50)->get(); $epfavs->load(['user' => function ($query) { $query->select('id', 'name'); }]); return view('ftfav.index', ['favorites' => $favorites, 'epfavs' => $epfavs]); }
public function favorites($id) { $ftep = Ftep::find($id, ['id', 'ft_id', 'title']); $ft = Ft::find($ftep->ft_id, ['id', 'title']); $favorites = Ftepfav::with(['user' => function ($query) { $query->select('id', 'name'); }])->select('user_id', 'created_at')->where('ftep_id', $id)->orderBy('created_at')->paginate(20); return view('ftep.favorites', ['ftep' => $ftep, 'ft' => $ft, 'favorites' => $favorites]); }
public function ftepfavs(Request $request, $id) { if ($request->has('order')) { $order = $request->input('order'); } else { $order = 'desc'; } $ftepfavs = Ftepfav::with(['ftep' => function ($query) { $query->join('fts', 'fts.id', '=', 'fteps.ft_id')->select('fteps.id as id', 'ft_id', 'fts.title as ft_title', 'fteps.title as title', 'release_date', 'staff', 'fteps.poster_url as poster_url'); }])->select('ftep_id', 'created_at')->where('user_id', $request->user()->id)->orderBy('created_at', $order)->paginate(50); return view('user.ftepfavs', ['user' => User::find($id, ['id', 'name']), 'ftepfavs' => $ftepfavs, 'order' => $order]); }