R
R
Radiss2019-06-17 23:12:06
Laravel
Radiss, 2019-06-17 23:12:06

How to implement the output of the latest blog articles?

I use version 5.2
The selection of materials is implemented not by id, but through the alias alias
route

spoiler

Route::resource('articles','ArticlesController',[
                    'parameters'=>[
                        
                          'articles' => 'alias'
                        
                        ]
                        
                        ]);	
Route::get('articles/cat/{cat_alias?}',['uses'=>'[email protected]','as'=>'articlesCat'])->where('cat_alias','[\w-]+');


Entries are displayed in 3 pcs.
Repository (get method)
spoiler

public function get($select = '*',$take = FALSE,$pagination = FALSE, $where = FALSE) {
    
    $builder = $this->model->select($select);
    
    if($take) {
      $builder->take($take);
    }
    
    if($where) {
      $builder->where($where[0],$where[1]);
    }
    
    
    if($pagination) {
      return $this->check($builder->paginate(Config::get('settings.paginate')));
    }

    return $this->check($builder->get());
  }


ArticlesRepository(getArticles)
spoiler

public function getArticles($alias = FALSE) {
    	
    	$where = FALSE;
    	
    	if($alias) {
    		// WHERE `alias` = $alias
      $id = Category::select('id')->where('alias',$alias)->first()->id;
      //WHERE `category_id` = $id
      $where = ['category_id',$id];
    }
    
    $articles = $this->a_rep->get(['id','title','alias','created_at','img','desc','user_id','category_id','keywords','meta_desc'],FALSE,TRUE,$where);
    
    if($articles) {
      $articles->load('user','category','comments');
    }
    
    return $articles;
    
  }


Articles are displayed in the right sidebar on the main page and in the "blog" section. SiteController
is responsible for their output
spoiler

<?php

namespace Corp\Http\Controllers;
use Illuminate\Http\Request;
use Corp\Http\Requests;
use Corp\Repositories\SlidersRepository;
use Corp\Repositories\PortfoliosRepository;
use Corp\Repositories\ArticlesRepository;
use Config;

class IndexController extends SiteController
{
    
    public function __construct(SlidersRepository $s_rep, PortfoliosRepository $p_rep, ArticlesRepository $a_rep) {
    	
    	parent::__construct(new \Corp\Repositories\MenusRepository(new \Corp\Menu));
    	
    	$this->s_rep = $s_rep;
    	$this->p_rep = $p_rep;
    	$this->a_rep = $a_rep;
    	
    	$this->bar = 'right';
    	
    	$this->template = config('settings.theme').'.index';
    
  }
    
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
   
        $articles = $this->getArticles();
        
             $this->contentRightBar = view(config('settings.theme').'.indexBar')->with('articles',$articles)->render();
        
          return $this->renderOutput();
    }
    
    protected function getArticles() {
    	$articles = $this->a_rep->get(['title','created_at','img','alias'],Config::get('settings.home_articles_count'));
    	
    	return $articles;
    }	
    
    
Макет
indexBar.blade

<spoiler title=""><code lang="php">

                    
                    
                    <div class="widget-first widget recent-posts">
                    
                    @if($articles)
                     	<h3>{{ trans('ru.from_blog') }}</h3>
                     	<div class="recent-post group">
                    	
                    		@foreach($articles as $article)
                    			
                    			<div class="hentry-post group">
                                  <div class="thumb-img"><img src="{{asset(config('settings.theme'))}}/images/articles/{{ $article->img->mini }}" alt="001" title="001" /></div>
                                  <div class="text">
                                      <a href="{{ route('articles.show',['alias'=>$article->alias]) }}" title="Section shortcodes &amp; sticky posts!" class="title">{{ $article->title }}</a>

                                      <p class="post-date">{{ $article->created_at->format('F d, Y') }}</p>
                                  </div>
                              </div>
                    			
                    		@endforeach
                    	
                    	</div>
                    @endif

                    <div class="widget-last widget text-image">
                        <h3>Customer support</h3>
                        <div class="text-image" style="text-align:left"><img src="{{asset(config('settings.theme'))}}/images/callus.gif" alt="Customer support" /></div>
                        <p>Proin porttitor dolor eu nibh lacinia at ultrices lorem venenatis. Sed volutpat scelerisque vulputate. </p>
                    </div>
                    
                

</code></spoiler>


Under Blog ArticlesController
spoiler
<?php

namespace Corp\Http\Controllers;
use Illuminate\Http\Request;
use Corp\Repositories\PortfoliosRepository;
use Corp\Repositories\ArticlesRepository;
use Corp\Repositories\CommentsRepository;
use Corp\Http\Requests;
use Corp\Category;

class ArticlesController extends SiteController
{
    
     public function __construct(PortfoliosRepository $p_rep, ArticlesRepository $a_rep, CommentsRepository $c_rep) {
    	
    	parent::__construct(new \Corp\Repositories\MenusRepository(new \Corp\Menu));
    	
    	$this->p_rep = $p_rep;
    	$this->a_rep = $a_rep;
    	$this->c_rep = $c_rep;
    	
    	$this->bar = 'right';
    	
    	$this->template = config('settings.theme').'.articles';
    
  }
  
  public function index($cat_alias = FALSE)
    {
        //
        
        $this->title = 'Блог';
    $this->keywords = 'String';
    $this->meta_desc = 'String';
        
        $articles = $this->getArticles($cat_alias);
        
        $content = view(config('settings.theme').'.articles_content')->with('articles',$articles)->render();
        $this->vars = array_add($this->vars,'content',$content);
        
        $comments = $this->getComments(config('settings.recent_comments'));
        $portfolios = $this->getPortfolios(config('settings.recent_portfolios'));

        
        $this->contentRightBar = view(config('settings.theme').'.articlesBar')->with(['comments' => $comments,'portfolios' => $portfolios]);
    
        
        return $this->renderOutput();
    }
    
    public function getComments($take) {
    
    $comments = $this->c_rep->get(['text','name','email','site','article_id','user_id'],$take);
    
    if($comments) {
      $comments->load('article','user');
    }
    
    return $comments;
  }
  
  
    
    public function getArticles($alias = FALSE) {
    	
    	$where = FALSE;
    	
    	if($alias) {
    		// WHERE `alias` = $alias
      $id = Category::select('id')->where('alias',$alias)->first()->id;
      //WHERE `category_id` = $id
      $where = ['category_id',$id];
    }
    
    $articles = $this->a_rep->get(['id','title','alias','created_at','img','desc','user_id','category_id','keywords','meta_desc'],FALSE,TRUE,$where);
    
    if($articles) {
      $articles->load('user','category','comments');
    }
    
    return $articles;
    
  }
  
  public function show($alias = FALSE) {
    
    $article = $this->a_rep->one($alias,['comments' => TRUE]);
    
    if($article) {
      $article->img = json_decode($article->img);
    }
    
    //dd($article->comments->groupBy('parent_id'));
    
    if(isset($article->id)) {
      $this->title = $article->title;
      $this->keywords = $article->keywords;
      $this->meta_desc = $article->meta_desc;
    }
    
    
    $content = view(config('settings.theme').'.article_content')->with('article',$article)->render();
    $this->vars = array_add($this->vars,'content',$content);
    
    
    $comments = $this->getComments(config('settings.recent_comments'));
        $portfolios = $this->getPortfolios(config('settings.recent_portfolios'));

        
        $this->contentRightBar = view(config('settings.theme').'.articlesBar')->with(['comments' => $comments,'portfolios' => $portfolios]);
    
    
    return $this->renderOutput();
  }	
    
}

reghtBar.blade layout
spoiler
<div class="sidebar group">
  {!! $content_rightBar !!}
</div>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
jazzus, 2019-06-19
@jazzus

I mean to simplify in general. Repository with get method only duplicates Laravel functions and adds nothing but confusion. Articles are displayed simply

Article::where('alias',$alias)
       ->latest()
       ->take($take)
       ->paginate($paginate);

or through categories
Category::with(['articles' => function($query) use ($alias,$take) {
                  $query->where('alias',$alias)->запрос;
              }])->запрос->paginate($paginate);

there will be a collection of categories with their articles. Get them in a nested loop in the template.
Error because in the Repository after paginate is no longer a builder

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question