Replace space with dash underscore in Laravel

Sometimes we need to replace some character or spaces with another character in laravel or any other programming languages similarly today, i am going to explain how can we replace space with dash “-” symbol with spaces between words such as replace space with dash underscore in laravel to replace-space-with-dash-underscore-in-laravel. This is very useful concept which I recently used in making the slug of pages because these URL should be seo friendly so I made the slugs using dash by replacing spaces between words of the title of page so in following example I am explaining how we can make slugs in laravel.

Step 1

First, I need to make a controller where I can fetch the content of the page so that I can show this detail on web page. To do that I need to create a controller that can fetch the data from database so to create a controller you need to run the following command.
php artisan make:controller title now your controller is successfully created.

You need to write a function which can be able to fetch the data such as following.

public function articles()
{
        $qry="article";
        $articles = Blog::where('status', 1)->where('category','article')->paginate(10);
        return view('layouts.articles',compact('articles'));
}

Step 2

Create a blade file which can list your articles and when someone click the title it should go to detail page where it can display slug replaced with “-” symbol so following is the code to write that stuff.

<div class="container">
            <ul class="course__listing">
            @foreach($articles as $article)
                <li>
                    <div class="row gx-2 gy-4 gy-md-0 gx-md-4 align-items-center">
                        <div class="col-md-4 col-lg-3">
                            <a href="{{ url('/')}}/article-details/{{$article->id }}" class="d-block">
                              <img src="{{ asset('storage/blogs/'.$article->picture) }}" class="img-fluid" alt="">
                            </a>
                        </div><!--.//col-->
                        <div class="col-md-8 col-lg-9">
                            <h3 class="h5 fw-medium">
                                <a href="{{ url('/')}}/article-details/{{$article->id }}/{{$article->title }}">{{ substr($article->title, 0, 40) }}</a>
                            </h3>
                            <ul class="info__list">
                                <li class="d-flex">
                                    <div class="caption">
                                    {!! substr($article->article, 0, 1800) !!}
                                    </div> 
                                </li>
                                
                                
                            </ul>
                            <div class="row g-2 align-items-center mt-2">
                                <div class="col-md-8 order-md-2">
                                    <div class="d-flex flex-wrap justify-content-md-end align-items-center date">
                                        <i class="fa fa-calendar me-2"></i>
                                        <span>{{ \Carbon\Carbon::parse($article->created_at)->isoFormat('MMM Do YYYY @ H:mm:ss')}}</span>
                                        
                                    </div>
                                </div>
                                <div class="col-md-4 order-md-1">
                                    <a href="{{ url('/')}}/article-details/{{$article->id }}/{{str_replace(' ','-',substr($article->title, 0, 100))}}" class="btn btn-primary fs-6 fw-medium rounded">Details</a>
                                </div>
                            </div><!--.//row-->
                        </div><!--.//col-->
                    </div><!--.//row-->
                </li><!--.//li-->
                @endforeach
              </ul><!--.//ul-->
              {{$articles->links('pagination::bootstrap-4')}}
            
        </div>

So following is the code which is used to replace the spaces with dash.

<a href="{{ url('/')}}/article-details/{{$article->id }}/{{str_replace(' ','-',substr($article->title, 0, 100))}}" class="btn btn-primary fs-6 fw-medium rounded">Details</a>

.

The function str_replace() is used to replace space with “-” in title of the post.

{{str_replace(' ','-',substr($article->title, 0, 100))}} is the full code to replace spaces with dash symbol.You can avoid using the substr() method you can use it in simple way such as following.

{{str_replace(' ','-',$article->title)}}

Leave a Comment