0

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!

10
  • I'm not sure what you mean when you say you want to put all of the values separate? Commented May 28, 2016 at 11:45
  • Now it just print all my values in one time but i want to wrap an anchor around each unique id Commented May 28, 2016 at 11:48
  • put an anchor around each one, its html Commented May 28, 2016 at 12:21
  • @foreach($categories as $categories) <tr> <td>{{$categories->category_id}}</td> <td>{{ $categories->Description }}</td> @endforeach So lets say i want to go to the page gsm? What do i do? Commented May 28, 2016 at 12:24
  • @Wesley does your categories table contain a row for the link? So you could have $categories->category_url Commented May 28, 2016 at 12:54

2 Answers 2

1
$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>
Sign up to request clarification or add additional context in comments.

21 Comments

Good but i get an error: Undefined variable category, what must i do? I'am really new in laravel sorry for that ['id' => $category->id ])
try @foreach($categories as $category)
I'am using your last solution with the url helpers <td> <a href="{{ route('category.show', ['id' => $category->id ]) }}"> {{ $categories->name }} </a> </td> But get the error:Undefined property:
It's my fault, you should use $category variable instead of $categories inside your foreach loop
<td>{{ $category->category_id}}</td> <td><a href="{{ route('eindwerk.index', ['id' => $category->id ]) }}">{{ $category->name }}</a> </td> <td>{{ $category->Description}}</td> I still get no links/anchors?
|
0

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.