Home > Mobile >  change page details based on URL by using if statement
change page details based on URL by using if statement

Time:04-18

i am new to PHP but I would like to make the content of the page change depending on the invoice selected by using if statement but I tried numerous ways which did not work out for me since I am using a pdf export package for laravell. my goal is to show different content based on the page url. Here is the controller

InvoiceController

<?php
namespace App\Http\Controllers;

use LaravelDaily\Invoices\Invoice;
use LaravelDaily\Invoices\Classes\Buyer;
use LaravelDaily\Invoices\Classes\InvoiceItem;
use App\Models\Invoice1;

class InvoiceController extends Controller
{
    
    public function show()
    {
        if('http://localhost/Laravel/HotelManagement/admin/invoice/1') {      
        $customer = new Buyer([
            'name'          => 'Ahmed',
            'custom_fields' => [
                'Email' => '[email protected]',
            ],
        ]);

        $item = (new InvoiceItem())->title('Reservation 1')->pricePerUnit(245);

        $invoice = Invoice::make()
            ->buyer($customer)
            ->series('10')
            ->sequence(10000)
            ->dateFormat('m/d/Y')
            ->currencySymbol('SR')
            ->discountByPercent(10)
            ->taxRate(15)
            ->addItem($item);

        return $invoice->stream();
    }
    else if('localhost/Laravel/HotelManagement/admin/invoice/2'){
        $customer = new Buyer([
            'name'          => ' Abdullah',
            'custom_fields' => [
                'Email' => '[email protected]',
            ],
        ]);

        $item = (new InvoiceItem())->title('Reservation 2')->pricePerUnit(467);

        $invoice = Invoice::make()
            ->buyer($customer)
            ->series('10')
            ->sequence(10000)
            ->dateFormat('m/d/Y')
            ->currencySymbol('SR')
            ->discountByPercent(10)
            ->taxRate(15)
            ->addItem($item);

        return $invoice->stream();
    }
    }
}

Route

Route::get('admin/invoice/{id}/',[InvoiceController::class, 'show']);

CodePudding user response:

You can recover URL parameters this way

Route

Route::get('admin/invoice/{id}/',[InvoiceController::class, 'show']);

Example URL

https://yoursite.com/admin/invoice/1234/
class InvoiceController extends Controller
{ 
    public function show(int $id)
    {
        dd($id) // outputs 1234
    }
}

CodePudding user response:

It's bad idea to use endpoint like http://localhost/Laravel/HotelManagement/admin/invoice/1 in this case. you can use request() for captured invoice ID. For example:

if(request()->get('id') == 1) {      
    // Do you logic here 
} 
else if(request()->get('id') == 2) {      
    // Do you logic here for invoice 2
} 
  • Related