1

So I am new to laravel. I am trying to use a view but it keeps referencing its self with links.

See below what I mean

So I have a route "customers"

Route::get('customers/{cid?}',  [
    'uses' => 'customers@getCustomerView'
])->name('customers');

In this route as you can see I reference a controller getCustomerView. With an optional CID as someone might just want to see a list of customers. Then choose their customer. So here is the controller function

public function getCustomerView($cid = null){
        $activeCustomer = array();
        if(!empty($cid)){
            // do middleware to get customer active detail
            $activeCustomer = array(
                'company' => 'Company '.$cid,
                'fname' => 'Test',
                'lname' => 'test'
            );
        }
        return view('customers.view', [
                'title' => 'Customer List',
                'cid' => $cid,
                'activeCustomer' => $activeCustomer,
                'clist' => [
                    ['company'=>'Company 1', 'fname' => 'Bob', 'lname' => 'Smith'],
                    ['company'=>'Company 2', 'fname' => 'Julie', 'lname' => 'Reid'],
                    ['company'=>'Company 3', 'fname' => 'Tony', 'lname' => 'Tima']
                ]
        ]);
    }

When I load http://domain/customers - Everything works fine.

In my customers.view I have the following that loops and put's the array into a table. Later I will be using some middle ware or self function to get data from database. For now I am just using a harden array.

@foreach($clist as $key=>$customer)
    <tr>
        <td>{{$key+1}}</td>
        <td><a href="customers/{{$key+1}}">{{$customer['company']}}</a></td>
        <td>{{$customer['fname']}}</td>
        <td>{{$customer['lname']}}</td>
    </tr>
@endforeach

The problem lies. Once I click on a customer. Page loads fine. http://domain/customers/1 - But if I go to click on another customer it does this

http://domain/customers/1/customers/2 - obviously this would cause an error. So why is it doing this?

How can I prevent it?

GrafixMastaMD
  • 139
  • 5
  • 15
  • In case you have a leisure time, you might need to read this [href: relative path vs absolute path](https://stackoverflow.com/questions/10659459/starting-with-a-forward-slash-in-html-for-href) – Chay22 Dec 07 '17 at 17:14
  • Its not a laravel at all, you can see [this answer](https://stackoverflow.com/a/10659501/1347094) for more information. – Torv Dec 07 '17 at 17:16
  • yeah. Understand now. I just know how sometimes laravel when referencing files you need to use url or asset. So I thought maybe the same thing. Glad to hear it's a simple as that. Thanks – GrafixMastaMD Dec 07 '17 at 17:29

1 Answers1

1

use this :

<td><a href="{{ route('customers' , [$key+1]) }}">{{$customer['company']}}</a></td>

it will generate a full url like http://domain/customers/1

but you can simply do that :

<td><a href="/customers/{{$key+1}}">{{$customer['company']}}</a></td>
Mathieu Ferre
  • 4,246
  • 1
  • 14
  • 31