本文整理汇总了PHP中app\models\News::findOrFail方法的典型用法代码示例。如果您正苦于以下问题:PHP News::findOrFail方法的具体用法?PHP News::findOrFail怎么用?PHP News::findOrFail使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类app\models\News
的用法示例。
在下文中一共展示了News::findOrFail方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: postComment
public function postComment(Request $request, $news_id)
{
$this->validate($request, ['type' => 'in:agree,disagree', 'content' => 'required|between:1,100']);
$news = News::findOrFail($news_id);
$comment = new Comment(['type' => $request->type, 'content' => $request->content, 'user_id' => Auth::user()->id]);
$news->comments()->save($comment);
return json_encode(['comments' => $this->comment->forNews($news_id)]);
}
示例2: news
public function news($news_id)
{
$user_id = 0;
if (Auth::check()) {
$user_id = Auth::user()->id;
}
$script = "<script>\n\t\tvar news_id = {$news_id};\n\t\tvar user_id={$user_id};\n\t</script>";
$news = News::findOrFail($news_id);
try {
DB::beginTransaction();
$news->increment("visit");
$news->save();
DB::commit();
} catch (Exception $exception) {
DB::rollback();
}
return view('pages.news', ['ngController' => 'NewsController', 'title' => $news->title, 'news' => $news->parse(), 'script' => $script]);
}
示例3: postDelete
public function postDelete(Request $request, $id)
{
$news = News::findOrFail($id);
$news->delete();
return redirect()->action('NewsController@getIndex');
}
示例4: getComments
public function getComments(Request $request, $newsId)
{
$news = News::findOrFail($newsId);
$querys = $request->query();
$comments = $news->getComments()->orderBy('created_at', 'desc');
if (isset($querys['author_id']) && is_numeric($querys['author_id'])) {
$author_id = (int) $querys['author_id'];
$comments = $comments->where('author_id', $author_id);
}
$page = 1;
if (isset($querys['page']) && is_numeric($querys['page'])) {
$page = (int) $querys['page'];
}
$pageSize = 20;
if (isset($querys['per_page']) && is_numeric($querys['per_page'])) {
$pageSize = (int) $querys['per_page'];
}
$totalCount = count($comments->get());
$skip = $pageSize * ($page - 1);
$comments = $comments->skip($skip)->take($pageSize)->get();
foreach ($comments as $comment) {
$comment = $this->unfoldCommentInfo($comment);
}
$result = array('page' => $page, 'per_page' => $pageSize, 'total_count' => $totalCount, 'data' => $comments);
return response()->json($result);
}
示例5: getTags
public function getTags($id)
{
$news = News::findOrFail($id);
return json_encode($news->Tags());
}
示例6: show
/**
* Display the specified News.
*
* @param int $id
* @return Response
*/
public function show($id)
{
$active = 'news';
$news = News::findOrFail($id);
return view('frontend.news.show', compact('active', 'news'));
}