0

In my controller in finorfail it returns array but instead i want pass package name My controller:

    public function package($package){
        $package = Packages::findOrFail($package);
        return view('pages.package',[
            'package' => $package,
        ]);
    }

My we.php:

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

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'];
}

SO i want to pass PackageName instead of array of numbers enter image description here So instead of 1 i want it to be package name in the url

My Migration:

   public function up()
    {
        Schema::create('pages', function (Blueprint $table) {
            $table->increments('id');
            $table->string('title')->nullable();
            $table->string('slug')->nullable();
            $table->text('description')->nullable();
            $table->string('image')->nullable();
            $table->string('meta_title')->nullable();
            $table->text('meta_description')->nullable();
            $table->integer('main')->nullable();
            $table->string('publish')->nullable();
            $table->timestamps();
        });
    }
Nutan Panta
  • 59
  • 1
  • 16
  • There is a misspelled `'Package_Short_Description',','Package_Price'` would be `'Package_Short_Description','Package_Price'` – STA Aug 07 '20 at 13:02
  • @sta i was taking out some of the migration data unnecessary for this question so now how can i pass package_name instead of array in the url and i fixed that in the question – Nutan Panta Aug 07 '20 at 13:04
  • you should give correct syntax, before asking a question. If anyone try with that code, they may face unexcepted error. Read [How to ask](https://stackoverflow.com/help/how-to-ask) – STA Aug 07 '20 at 13:07
  • @sta yeah i am sorry for that and i fixed the syntax – Nutan Panta Aug 07 '20 at 13:08
  • Read documents : https://laravel.com/docs/master/responses#redirecting-named-routes @NutanPanta – xNoJustice Aug 07 '20 at 13:08
  • You want to return `package_name` only? Then pass it like that, change code as `return view('pages.package',[ 'package' => $package->Package_Name ]);` access `Package_Name` from blade as `{{ $package }}` – STA Aug 07 '20 at 13:09
  • @sta can you look in the picture above i just edited i do need it in the view i need in my url – Nutan Panta Aug 07 '20 at 13:15
  • 1
    If you want name, then your will be like this `Packages::where('Package_name', $package)->first();` – STA Aug 07 '20 at 13:18
  • If returns empty object, return a 404 `$package = Packages::where('Package_name', $package)->first(); if(empty($package)) { return abort(404)} ;` – STA Aug 07 '20 at 14:35
  • @sta it worked now is there a way to put the package name in kebab case in url – Nutan Panta Aug 07 '20 at 14:40
  • 1
    Use **STR Slug Method**, to make your url pretty and SEO friendly, Use it while insert url to table, make an another field for store slug url, And one foe name (not slug) https://laravel.com/docs/7.x/helpers#method-str-slug – STA Aug 07 '20 at 14:45

1 Answers1

1

You can do it like this :

public function package($package)
{
   $package = Packages::where('Package_name', $package)->first();
  
    if(empty($package)) { 
      return abort(404); // if $package is empty, it will return 404
    } 

    return view('pages.package',[
         'package' => $package,
    ]);
}

The Str::slug method generates a URL friendly "slug" from the given string, so while saving Package_name make an another fileld called slug, where the slug name will be saved :

use Illuminate\Support\Str;

$slug = Str::slug('welcome to stackoverflow', '-');

// welcome-to-stackoverflow
STA
  • 30,729
  • 8
  • 45
  • 59
  • Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/219452/discussion-on-answer-by-sta-how-to-pass-a-database-data-instead-of-array-in-the). – Samuel Liew Aug 08 '20 at 09:46