0

I wanted to turn the package name into url name as well So when i used laravel helpers inside where() in my controller method i get an error: enter image description here

My Controller:

use Illuminate\Support\Str;

    public function package($package){
        $converted = Packages::where('Package_Name',$package)->first(); 
        $package = Str::kebab($converted);
        return view('pages.package',[
            'package' => $package,
        ]);
    }

My Model:

class Packages extends Model
{
    protected $table = 'packages';

    protected $fillable = ['Package_Banner_Image','Package_Image','Package_Type','Package_Name','Package_Short_Description','Package_Price','Package_Duration','Package_Level','Package_Location'];
}

My Route:

Route::prefix('/packages')->group(function() {
    Route::get('/', 'PackageController@packages')->name('packages');
    Route::get('/{package}', 'PackageController@package')->name('packages.show');
});

My Migration:

        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->longText('Package_Location');
            $table->timestamps();
        });
Ali Bigdeli
  • 1,286
  • 3
  • 17
  • 35
Nutan Panta
  • 59
  • 1
  • 16

1 Answers1

0

two things.
first: validate that $converted is not null, that found a record.
second: change the order to your routes, put first routes whit params, after static routes, and put the model name in your param.

In your case the order change of routes could be the solution

update your code whit this

public function package($package){
    $converted = Packages::where('Package_Name',$package)->first(); 
    if($converted){
        $package = Str::kebab($converted->Package_Name);
        return view('pages.package',[
            'package' => $converted,
        ]);
    }
    return redirect()->back();
}
Mike
  • 95
  • 2
  • 13
  • i tried all this when i do not use Str::kebab(); it works just fine but when i use it this error is shown. – Nutan Panta Aug 07 '20 at 20:15
  • in the documentation [https://laravel.com/docs/7.x/helpers#method-kebab-case] `$converted = Str::kebab('fooBar');` the parameter is string and you send an Object – Mike Aug 07 '20 at 20:20
  • how do i fix that? – Nutan Panta Aug 07 '20 at 20:22
  • 1. validate if the sql is found a record, 2. send as parameter the object title `$converted->title` or other property, and last you need send the object in your view `return view('pages.package',[ 'package' => $converted, ]);` – Mike Aug 07 '20 at 20:25
  • do you mean my packages method in the controller? – Nutan Panta Aug 07 '20 at 20:35
  • I have added above – Nutan Panta Aug 07 '20 at 20:41
  • it works but it even if i write suppose one of my package name is Kathmandu Pokhara so even if i write http://127.0.0.1:8000/packages/Kathmandu%20Pokhara it still works – Nutan Panta Aug 07 '20 at 21:34
  • ok, so you need use the kebab method when you redirect, for example ` package ` – Mike Aug 07 '20 at 21:43
  • i think the answer you above is not working check it at: https://realadventurenepal.herokuapp.com/ – Nutan Panta Aug 07 '20 at 21:48
  • my last answer is only an example, check the correct path and change for your variables name – Mike Aug 07 '20 at 21:58
  • i did not mean the last answer i mean the answer at the top the kebab case is not working because i have kept kebab case in the a tag in packages pages to redirect and it does not work but try using package_name in url check at the site. – Nutan Panta Aug 07 '20 at 22:08