本文整理汇总了PHP中app\models\Article::findOrFail方法的典型用法代码示例。如果您正苦于以下问题:PHP Article::findOrFail方法的具体用法?PHP Article::findOrFail怎么用?PHP Article::findOrFail使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类app\models\Article
的用法示例。
在下文中一共展示了Article::findOrFail方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: getArticle
public function getArticle($id, $api)
{
$article = Article::findOrFail($id);
if ($api) {
return response()->json($article);
} else {
return $article;
}
}
示例2: destroy
public function destroy($id)
{
$article = Article::findOrFail($id);
if ($article->delete()) {
return Redirect::to('/user/article');
} else {
return Redirect::back()->withInput()->withErrors('更新失败!');
}
}
示例3: testRouteCreateArticleArticleJsonReturned
/**
* Calls the creation route of an article
* and checks the response json and the db.
**/
public function testRouteCreateArticleArticleJsonReturned()
{
$response = $this->assertOk('POST', '/api/articles', ['title' => 'testing']);
$this->assertNotNull($response->article);
$this->assertNotNull($response->article->id);
$this->assertEquals('testing', $response->article->title);
$insertedArticle = Article::findOrFail($response->article->id);
$this->assertNotNull($insertedArticle);
$this->assertEquals($response->article->title, $insertedArticle->title);
}
示例4: listArticles
/**
* Lists the inserted articles
* either by id, title or none.
*
* @param int $id
* @param str $title
* @param int $limit
*
* @throws Illuminate\Database\Eloquent\ModelNotFoundException
*
* @return array
**/
public function listArticles($id = null, $title = null, $limit = 20)
{
$query = Article::orderBy('created_at');
if ($id) {
return Article::findOrFail($id);
}
if ($title) {
return $query->where('title', 'LIKE', '%' . $title . '%')->paginate($limit)->items();
}
return $query->paginate($limit)->items();
}
示例5: updateArticle
public function updateArticle(Request $request, $id, $api)
{
$article = Article::findOrFail($id);
$article->title = $request->input('title');
$article->content = $request->input('content');
$article->save();
if ($api) {
return response()->json($article);
} else {
return $article;
}
}
示例6: update
public function update($id, ArticleVal $request)
{
$file = $request->file('image');
$destinationPath = base_path() . '/portfolio-assessment/images/featured';
$filename = $file->getClientOriginalName();
$file->move($destinationPath, $filename);
echo $filename;
//echo '<img src="uploads/'. $filename . '"/>';
$article = Article::findOrFail($id);
$user = $article->update(['title' => $request->input('title'), 'image' => $filename, 'body' => $request->input('body'), 'excerpt' => $request->input('excerpt'), 'published_at' => $request->input('published_at'), 'tags' => $request->input('tags')]);
return redirect()->route('home')->withInfo('Your article has been updated');
}
示例7: createEdition
/**
* Creates a new edition on
* the passed article ID.
*
* @param int $articleID
* @param str $editionContent
* @param User $user
*
* @throws ModelNotFoundException
*
* @return Edition
**/
public function createEdition($articleID, $editionContent, User $user)
{
// TODO: Use a repository static method for this
if (method_exists(Article::class, 'newQuery')) {
Article::findOrFail($articleID);
}
$edition = new Edition();
$edition->article_id = $articleID;
$edition->content = $editionContent;
$edition->created_by = $user->id;
$edition->save();
return $edition;
}
示例8: update
public function update($id)
{
$id = intval($id);
$articleInstance = Article::findOrFail($id);
$data = Request::only('slag', 'content', 'title');
if (Request::hasFile('article_image')) {
$data['image_url'] = upload::upload('asset/article_image/', 'article_image');
}
$rules = ['slag' => 'required|max:255', 'title' => 'required|max:255', 'content' => 'required'];
if (validate::make($data, $rules)) {
$categories = Request::only('category');
$articleInstance->update($data);
$articleInstance->categories()->sync($categories['category']);
return Redirect::back()->withResult('operation complete');
} else {
return Redirect::back()->withResult('whoops!looks like something wrong happened');
}
}
示例9: boot
/**
* Define your route model bindings, pattern filters, etc.
*
* @return void
*/
public function boot()
{
parent::boot();
Route::bind('article', function ($slug) {
if (!Auth::user()) {
return Article::published()->slug($slug)->first();
}
return Article::where('slug', $slug)->first() ?: Article::findOrFail((int) $slug);
});
Route::bind('tag', function ($slug) {
if (!Auth::user()) {
return Tag::slug($slug)->first();
}
return Tag::where('slug', $slug)->first() ?: Tag::findOrFail((int) $slug);
});
Route::bind('page', function ($id) {
return Page::find($id);
});
}
示例10: edit
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
$article = Article::findOrFail($id);
return view('admin.articles.edit', ['article' => $article]);
}
示例11: show
public function show($id)
{
$article = Article::findOrFail($id);
return view('articles.show', compact('article'));
}
示例12: findWithCreatedAtAndSlug
public function findWithCreatedAtAndSlug($id, $created_at, $slug)
{
return Article::findOrFail($id)->where('created_at', 'LIKE', $created_at . '%')->where('slug', $slug)->first();
}
示例13: saveThisArticleRequest
/**
* @param Request $request
* @return Article
*/
private function saveThisArticleRequest(Request $request)
{
$article = new Article();
$path = storage_path('articles');
$file = '';
if ($request->id) {
$article = Article::findOrFail((int) $request->id);
$file = $article->file;
}
if (!$file) {
$file = str_random(12) . '.md';
while (app('files')->exists("{$path}/{$file}")) {
$file = str_random(12) . '.md';
}
}
app('files')->put("{$path}/{$file}", $request->article);
$article->title = $request->title;
$article->slug = $request->title;
$article->excerpt = str_words($request->article, 20);
$article->published = $request->published;
$article->path = $path;
$article->file = $file;
$article->author_id = app('auth')->user()->id;
$article->save();
return $article;
}