1
votes

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 enter image description here

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',
          ],
        ];
    }
}
1
That's because you're overridding the fields selected on the Article model. Remove the select method call. - fubar
Show me your Article Model? - imrealashu
Please revert your question to the original question. You have completely changed the question (including the title). If you have a new question, ask a new question. - Chris
ok. im new to this forum. Ill try a new question - user6780526
thats ok. people are just trying to help, and when you change your question dramatically it means the effort they put in is lost - Chris

1 Answers

1
votes

You are opening the query with a select against only id and slug so you will always be limited to those fields.

Try:

$locale = Lang::locale();
        $article = Article::join('article_translations', 'articles.id', '=', 'article_translations.article_id')
        ->where('slug', $slug)->withTranslations($locale)
        ->firstOrFail();
        $article->addPageView();
        return $article;