I have an articles table with image, category_id, created-by, updated_by, created_at, updated_at fields. I'm storing article translations in a separate table which has a slug, title, subtitle, description, locale fields.
I want to use seo friendly urls like articles/category-slug/article-slug
I tried the code below in the ArticeController
public function show(ArticleCategory $articlecategory, $slug)
{
$locale = Lang::locale();
$article = Article::join('article_translations', 'articles.id', '=', 'article_translations.article_id')
->where([['slug', $slug], ['locale', $locale]])->with('category')
->firstOrFail();
$article->addPageView();
return ArticleResource::collection($article);
}
old-question(solved by Chris answer) : But it doesn't display all the fields
I have updated the code above as per suggestions by Chris. However im also using a resource collection and it shows an error with the code above. Error
Method Illuminate\Database\Query\Builder::mapInto does not exist.
ArticleResource
<?php
namespace App\Http\Resources\Articles;
use Illuminate\Http\Resources\Json\JsonResource;
use Carbon\Carbon;
use App\Models\ArticleTranslation;
class ArticleResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return [
'id' => $this->id,
'article_url' => '/articles/'.$this->category->slug.'/'.$this->slug,
'category' => $this->category->name,
'category_slug' => $this->category->slug,
'category_url' => '/articles/'.$this->category->slug,
'image' => url('/').$this->image,
'author' => $this->creator->name,
'created' => Carbon::parse($this->created_at)->diffForHumans(),
'updated' => Carbon::parse($this->updated_at)->diffForHumans(),
'views' => $this->page_views,
'title' => $this->title,
'subtitle' => $this->subtitle,
'description' => $this->content,
'links' => [
'self' => 'link-value',
],
];
}
}
Article
model. Remove theselect
method call. - fubarArticle
Model? - imrealashu