Wesley
Wesley

Reputation: 93

Laravel use array of controller to echo something in view

I'm new to laravel and i'm stuck at this. So this code gives me all my results from my table

$categories = new Categories();
$categories = Categories::all();

In my view i got a foreach loop to get all the values from the table: categories. Like so:

@foreach($categories as $categories)
    <tr>
        <td>{{$categories->category_id}}</td>
        <td>{{ $categories->Description }}</td>
@endforeach

My question is, is there a way to print all the values seperate? So i can put an anchor before them? Do i have to put it in an array? My output must be something like this:

1----<a href="goGSM.php">GSM</a>
2----<a href="goGPS.php">GPS</a>

Thanks a lot!

Upvotes: 0

Views: 445

Answers (2)

Wesley
Wesley

Reputation: 93

A big thank you to everyone who helped me! This is the correct solution if you were wondering.

<td><a href="shop{{$category->Description}}">{{$category->Description}}</a></td>

Upvotes: 0

huuuk
huuuk

Reputation: 4795

$categories = new Categories(); // for what this line???
$categories = Categories::all();



@foreach($categories as $category)
    <tr>
        <td>{{$category->id}}</td>
        <td> <a href="{{ yourFunctionThatGeneratesURL($category) }}"> {{ $category->name }} </a> </td>
        // or as suggested in comments
        <td> <a href="{{ $category->urlToCategory }}"> {{ $category->name }} </a> </td>
        // or you can use one of the laravel's url helpers
        <td> <a href="{{ route('category.show', ['id' => $category->id ]) }}"> {{ $category->name }} </a> </td> 
        <td>{{ $category->Description }}</td>
@endforeach

more info about helpers you can find in [docs][1]

EDIT

if you have categories

category1
  id - 1
name - GSM

category2
  id - 2
name - GPS

with this code

@foreach($categories as $category)
    <tr>
        <td>{{$category->id}}</td>
        <td><a href="go{{$category->name}}.php">{{$category->name}}</a></td>
    </tr>
@endforeach

you'll get

<tr>
    <td>1</td>
    <td><a href="goGSM.php">GSM</a></td>
</tr>
<tr>
    <td>2</td>
    <td><a href="goGPS.php">GPS</a></td>
</tr>

Upvotes: 1

Related Questions