laravel - How can i change the sentence and join them in a blade.php file?
Get the solution ↓↓↓I am new to PHP and laravel so i wanted to ask a simple question. Suppose i have a few words such as "Hello I Am Nutan" and i have brought it in from my database so can i change that to "hello-i-am-nutan". I am trying to implement this in my URL in my project where i want to make my package name to package URL and according to the URL the proper data to be shown in next page so can some one tell me how to change the "Hello I Am Nutan" to "hello-i-am-nutan" in blade.php file.
My Controller:
public function package($package_name){
// your code goes here
}
public function packages(){
$packages = Packages::all();
$converted = Str::kebab();
return view('pages.packages',compact('packages'),compact('converted'));
}
My blade.php file:
@extends('layouts.layout')
@section('style')
<!-- Owl Carousel -->
<link rel="stylesheet" href="css/owl.carousel.min.css" />
<link rel="stylesheet" href="css/owl.theme.default.min.css" />
<link rel="stylesheet" href="css/packages.css">
@endsection
@section('content')
<section id="wrapper" class="skewed">
<div class="layer bottom"></div>
<div class="layer top">
<div class="content-wrap">
<div class="content-body">
<h1>Packages</h1>
<p class="text-capitalize">Scroll Down to learn more about packages</p>
</div>
</div>
</div>
</section>
<!-- Property to Buy Start -->
<section class="packages mt-5">
<div class="container">
<div class="package-heading text-center mb-4">
<h2 class="font-weight-bold text-capitalize">Tour Packages</h2>
<p>Check out all of our latest tour packages.</p>
</div>
<div class="packages owl-carousel owl-carousel2 row mb-5">
@foreach ($packages as $package)
@if ($package->Package_Type == 'Tour')
<div class="col-md-3">
<div class="package shadow" >
<div class="package-image">
<img src="{{$package->Package_Image}}" alt="Kathmandu - Pokhara Trek">
<div class="package_hover">
<a href="{{ route('package.name',['package_name' => $converted]) }}">Read More</a>
<img src="img/svg/heart.svg" alt="" >
</div>
</div>
<div class="package-description">
<div class="package-name">
<h5 class="font-weight-bold text-capitalize">{{$package->Package_Name}} {{$package->Package_Type}}</h5>
</div>
<div class="package-short-description">
<p class="text-justify">{{$package->Package_Short_Description}}</p>
</div>
<div class="package-detail d-flex justify-content-between font-weight-bold">
<span><i class="far fa-clock"></i> Duration: {{$package->Package_Duration}}Days</span>
<span><i class="fas fa-bars"></i> Level:{{$package->Package_Level}}</span>
</div>
<div class="package-price pt-2">
<span class="font-weight-bold">Tour Price<br/>${{$package->Package_Price}}</span>
</div>
</div>
</div>
</div>
@endif
@endforeach
</div>
</div>
</section>
<!-- Property to Buy End -->
<!-- Property to Rent Start -->
<section class="packages">
<div class="container">
<div class="package-heading text-center mb-4">
<h2 class="font-weight-bold text-capitalize">Trek Packages</h2>
<p>Check out all of our latest treaking packages.</p>
</div>
<div class="owl-carousel owl-carousel2 row mb-5">
@foreach ($packages as $package)
@if ($package->Package_Type == 'Trek')
<div class="col-md-3">
<div class="package shadow" >
<div class="package-image">
<img src="{{$package->Package_Image}}" alt="Kathmandu - Pokhara Trek">
<div class="package_hover">
<a href="{{ route('package.name',['package_name' => $converted]) }}">Read More</a>
<img src="img/svg/heart.svg" alt="" >
</div>
</div>
<div class="package-description">
<div class="package-name">
<h5 class="font-weight-bold text-capitalize">{{$package->Package_Name}} {{$package->Package_Type}}</h5>
</div>
<div class="package-short-description">
<p class="text-justify">{{$package->Package_Short_Description}}</p>
</div>
<div class="package-detail d-flex justify-content-between font-weight-bold">
<span><i class="far fa-clock"></i> Duration: {{$package->Package_Duration}}Days</span>
<span><i class="fas fa-bars"></i> Level:{{$package->Package_Level}}</span>
</div>
<div class="package-price pt-2">
<span class="font-weight-bold">Tour Price<br/>${{$package->Package_Price}}</span>
</div>
</div>
</div>
</div>
@endif
@endforeach
</div>
</div>
</section>
<!-- Property to rent End -->
@endsection
@section('script')
{{-- Owl Carousel js --}}
<script src="js/owl.carousel.min.js"></script>
<script src="js/packages.js"></script>
@endsection
i wnat it in<a href="{{url('/package/')}}">Read More</a>
Okay i have not made my routes for the next page but it will be /package/something
Route::prefix('/package')->group(function() {
Route::get('/{package_name}', 'PackageController@package')->name('package.name');
});
But it seems that Package_Name is not database data but just plain text how to bring Package_Name in controller inside kebab. My Migration file:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreatePackagesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('packages', function (Blueprint $table) {
$table->id();
$table->string('Package_Banner_Image');
$table->string('Package_Image');
$table->string('Package_Type');
$table->string('Package_Name')->unique();
$table->integer('Package_Price');
$table->integer('Package_Duration');
$table->string('Package_Level');
$table->string('Package_Short_Description');
$table->text('Day_One');
$table->text('Day_Two')->nullable();
$table->text('Day_Three')->nullable();
$table->text('Day_Four')->nullable();
$table->text('Day_Five')->nullable();
$table->text('Day_Six')->nullable();
$table->text('Day_Seven')->nullable();
$table->text('Day_Eight')->nullable();
$table->text('Day_Nine')->nullable();
$table->text('Day_Ten')->nullable();
$table->text('Day_Eleven')->nullable();
$table->text('Day_Twelve')->nullable();
$table->text('Day_Thirteen')->nullable();
$table->text('Day_Fourteen')->nullable();
$table->text('Day_Fiveteen')->nullable();
$table->text('Cost_Includes_One');
$table->text('Cost_Includes_Two')->nullable();
$table->text('Cost_Includes_Three')->nullable();
$table->text('Cost_Includes_Four')->nullable();
$table->text('Cost_Includes_Five')->nullable();
$table->text('Cost_Includes_Six')->nullable();
$table->text('Cost_Includes_Seven')->nullable();
$table->text('Cost_Includes_Eight')->nullable();
$table->text('Cost_Includes_Nine')->nullable();
$table->text('Cost_Includes_Ten')->nullable();
$table->longText('Package_Location');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('packages');
}
}
Answer
Solution:
The "hello-i-am-nutan" is called kebab case You can use laravel helper Str::kebab() as in the docs: https://laravel.com/docs/7.x/helpers#method-kebab-case
Answer
Solution:
In blade file
<?php $converted = Illuminate\Support\Str::kebab($package['Package_Name']);?>
<a href="{{ route('package-info',['package_name' => $converted]) }}">Read More</a>
In route file
Route::get('/package-info/{package_name}', 'master\webEventController@package_info')->name('package-info');
In controller file
public function package_info($package_name) {
$package_name = str_replace('-', ' ', $package_name);
$package_info = Packages::where('package_name', $package_name)->first();
return view('')->with('package_info', $package_info);
}
Use laravel helper Str::kebab() See all Helper function here in docs, https://laravel.com/docs/7.x/helpers#method-kebab-case
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: php undefined array key
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.