R
R
Rodion2018-06-20 19:02:34
Laravel
Rodion, 2018-06-20 19:02:34

How to exclude a specific section from the sitemap?

Laravel version 5.2
I can't figure out how to exclude a category with a certain id from $category?
Sitemap controller:

<?php

namespace App\Http\Controllers;

use App\Models\Category;
use App\Models\Country;
use App\Models\PhotoCategory;
use App\Models\Photowork;

class SitemapController extends Controller
{
    public function index()
    {
        $photographer_category = PhotoCategory::get(['slug'])->map(function ($item) {
            return $item->slug;
        })->push('')->all();

        $users = Photowork::join('users' , 'users.id', '=', 'photoworks.user_id')
            ->where('users.active', true)
            ->where('parent_id', 0)
            ->groupBy('user_id')
            ->get()
            ->map(function ($item){
            return [$item->user_id => $item->user->username];
        })->flatten()->all();
    
        $categories = Category::get(['slug'])->map(function ($item) {
            return $item->slug;
        })->push('')->all();

        return response()->view('sitemap.index', [
            'photographer_categories' => $photographer_category,
            'users' => $users,
            'categories' => $categories
        ])->header('Content-Type', 'text/xml');
    }
}

view
<?php echo '<?xml version="1.0" encoding="UTF-8"?>'; ?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    @foreach ($photographer_categories as $photographer_category)
        <url>
            <loc>{{route('photostream.index').'/'.$photographer_category }}</loc>
            <changefreq>weekly</changefreq>
            <priority>0.6</priority>
        </url>
    @endforeach

    @foreach ($users as $user)
        <url>
            <loc>{{route('users.show',[$user]) }}</loc>
            <changefreq>weekly</changefreq>
            <priority>0.6</priority>
        </url>
    @endforeach

        @foreach ($categories as $category)
            <url>
                <loc>{{route('users.search').'/'.$category }}</loc>
                <changefreq>weekly</changefreq>
                <priority>0.6</priority>
            </url>
        @endforeach
</urlset>

Route:
Route::get('sitemap.xml', ['as' => 'sitemap', 'uses' => '[email protected]']);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Aksentiev, 2018-06-20
@azmarin

well, maybe something like that

Category::whereNotIn('id', [1,2,3])->get(['slug']);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question