henryoladj
1 week ago
72
36
Laravel

Generate Clean SEO URL

Posted 1 week ago by henryoladj

My URL is currently like this -> http://localhost:8000/news?tags=1. 1 is ofcourse the ID of the tags in the database

i have tags with names (Entertainment, News) How can i make my URL Look like this lhttp://localhost:8000/entertainment instead of http://localhost:8000/news?tags=1

NewsController.php

<?php

namespace App\Http\Controllers;

use App\News;
use App\Tag;
use Illuminate\Http\Request;
use Illuminate\Support\Str;

class NewsController extends Controller
{
    function __construct()
    {
        $this->middleware('auth', ['except' => ['index', 'show']]);
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {   
        if($request->has('tags')){
            $tag=Tag::find($request->tags);
            $news=$tag->news;
        }else{
            $news= News::paginate(15);
        }
        return view('categories.news',compact('news'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('news.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //validate

        $this->validate($request,[
            'subject'=>'required|min:10',
            'tags' => 'required',
            'body' => 'required|min:20'

        ]);
        
        //store
        $news=auth()->user()->news()->create($request->all());

        $news->tags()->attach($request->tags);

        //redirect
        return redirect()->route('news.index');
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\News  $news
     * @return \Illuminate\Http\Response
     */
    public function show(News $news)
    {   
        
        
        return view('news.single', compact('news'));
    }

    

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\News  $news
     * @return \Illuminate\Http\Response
     */
    public function edit(News $news)
    {
        return view('news.edit', compact('news'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\News  $news
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, News $news)
    {
        if(auth()->user()->id !== $news->user_id){
            abort(401, "Please Login");
        }

        $this->validate($request,[
            'subject'=>'required|min:10',
            'body' => 'required|min:20'
        ]);

        $news->update($request->all());

        return redirect()->route('news.show', $news->slug)->withMessage('News Updated');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\News  $news
     * @return \Illuminate\Http\Response
     */
    public function destroy(News $news)
    {   
        if(auth()->user()->id !== $news->user_id){
            abort(401, "Please Login");
        }

      
        $news->delete();
        $news= News::paginate(15);
        return view('categories.news', compact('news'));
    }
}

Present URL

<a href="{{route('news.index', ['tags'=>$tag->id])}}">{{$tag->name}}</a>

ROUTES

Route::resource('/news', 'NewsController');


Route::get('/post/{news}', '[email protected]')->name('news.show');

Please sign in or create an account to participate in this conversation.