Search code examples
laravelblogs

Trying to get shorter link laravel


This is my first question in this forum so please be patient to me ;) I'm making a blog using Laravel and I have a page with a view of all articles inside my database enter image description here

Last column is "Action" column whet after hit the button you can see single article. The problem is to show each content (title,subtitle etc.) i must create loooong UrL like this http://127.0.0.1:8000/article/Test/Test/test/2020-05-08%2016:00:00

Is there any chance to cut URL to be like that: http://127.0.0.1:800/article/Test and still have all content?

Files

web.php Route::get('article/{title_article}/{subtitle_created}/{text_article}/{created_at}','ReadController@index');

Controller

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Article;
class ReadController extends Controller
{
        public function index($title_article,$subtitle_article,$text_article,$created_at) 
        {
            return view('article',compact('title_article','subtitle_article','text_article','created_at'))->with('title','Artykuł');
        }
}


Solution

  • You should use the primary key (probably an incrementing id) of your database object (App\Article) to identify the object. Laravel has a wonderful function to automatically convert the primary key (id) in the route to an eloquent model.

    In your case:

    Route::get('article/{article}','ReadController@index');

    and

    public function index(Article $article) { ... }

    I hope this helps you. If you don't understand parts of it, feel free to ask :)