Tips for extracting the value of every iteration in a for each loop and incorporating it into route query parameters with Laravel

Currently, I am facing an issue where I have a table generated from a foreach loop as shown below and attempting to pass it through a URL path href=" {{ url('grade/semester/schoolyear')}} "

SEMESTER SCHOOL YEAR Action
1st 2020-2021 View
2nd 2019-2020 View
1st 2018-2019 View
2nd 2018-2019 View

Blade Code:

<table class="table">
    <thead>
        <tr >
            <th scope="col" style="color:#1a0dab">Semester</th>
            <th scope="col" style="color:#1a0dab">Academic Year</th>
            <th scope="col" style="color:#1a0dab">Action</th>
        </tr>
    </thead>
    <tbody>
    @foreach($years as $year)
        <tr>
            <td style="text-align: left;">{{ $year->sem}}</td>
            <td>{{ $year->sy}} </td>
            <td>
                <a title="View Grade" **href="{{ url('/grade/{{$year->sem}}/{{$year->sy}}')}}"**>
                    <i class="fas fa-file-alt"></i>
                </a> &nbsp;&nbsp;
                <a title="Print Grade" href="#">
                    <i class="fas fa-print"></i>
                </a>
            </td>        
        </tr>
    @endforeach
    </tbody>
</table>

My goal is to achieve the correct route: Clicking on the "View" link in the third row of my table should redirect me to the correct path.

Thank you for your assistance.

Answer №1

Instead of evaluating the template multiple times, a workaround is to combine the strings to create the desired url:

{{ url('/grade/' . $year->sem . '/' . $year->sy) }}

In this case, the . symbol functions as the PHP operator for string concatenation.

Answer №2

To set up a route in your web.php file, use the following syntax:

Route::get('/grade/{sem}/{sy}')->name('grades');

In your blade file, you can then link to this route using its name and pass in parameters like so:

<a href="{{ route('grades', ['sem' => $year->sem, 'sy' => $year->sy]) }}">LinkTitle</a>

The grades in route('grades'... represents the assigned name for the route.

Learn more about generating URLs for named routes in Laravel documentation

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Changing the color of placeholder text in MUI 5 TextField

Looking to customize the text color and placeholder text color in my MUI TextField component to be green https://i.sstatic.net/NZmsi.png The documentation doesn't provide clear instructions, so I attempted a solution that didn't work: <TextF ...

There was an error in reading the 'rotateOnAxis' property due to its undefined nature

When working with files downloaded from three.js such as cube.obj and cube.mtl, rotating them appears to work without any issues. However, when attempting to rotate files created in Adobe Dimension like capsule.obj and capsule.mtl, an error pops up as seen ...

NodeJS Express REST API fails to fetch data in GET request

As I work on developing a REST API using NodeJS and Express, I have set up routes for handling "offers". However, when attempting to send a GET request, I receive an empty response along with a 200 status code. Here is the snippet from routes.js: route ...

SQL - Retrieve information for a specified time frame using a predetermined value

I am looking to retrieve data from a table, extract the information using JavaScript, and visualize it on a graph where the x-axis represents dates and the y-axis represents numbers. The DATA table is structured as follows (please note that attempts to fo ...

Creating a Distinct Interior Array Separate from the Exterior

I'm currently working on a project that involves creating a 2D array. I want the interior elements of this array to be black while the exterior elements should be white. However, my 2D array doesn't seem to be forming correctly - it looks more li ...

What is the best way to remove excess content that falls outside the viewBox?

Is there a function or method to trim a path that extends beyond the viewbox rather than simply concealing it? I am currently using svg-edit, which has a specific viewbox or canvas area. Any elements drawn outside of this canvas remain hidden. However, wh ...

SwipeJS is not compatible with a JQuery-Mobile web application

I am currently attempting to integrate SwipeJS (www.swipejs.com) into my JQuery-Mobile website. <script src="bin/js/swipe.js"></script> <style> /* Swipe 2 required styles */ .swipe { overflow: hidden; ...

React refrains from directly updating the DOM with entries

Within my export default class List extends React Component, I have implemented an AJAX request. The request is successful, and I receive an array in the format of: [{...}, {...}, ...] Each object in the array has the following structure: { descriptio ...

Dealing with the issue of receiving raw JavaScript in the .ajax error function even when receiving a 200

I am encountering an issue with a jQuery.ajax() call to a third-party product. The POST request is successful, returning a 200 OK status code, but instead of executing the success function, it redirects to the error function. The response variable displays ...

The Angular ternary operator can be used in conjunction with functions for optimal

Consider this scenario: I have two functions that are triggered by a button click: <button ng-click="functionOne || functionTwo()"></button> However, I want to optimize this setup so that the functions are only called if they return a value. ...

Utilizing ng-repeat to loop through a div element that consists of various nested tags

I need to display multiple tabs, with each tab having a table where the values are populated from a database. The number of tabs is dynamic and fetched from another page of the application. All tables have the same structure but different values. How can I ...

The jQuery fadeOut function modifies or erases the window hash

While troubleshooting my website, I discovered the following: /* SOME my-web.com/index/#hash HERE... */ me.slides.eq(me.curID).fadeOut(me.options.fade.interval, me.options.fade.easing, function(){ /* HERE HASH IS CLEARED: my-web.com/index/# * ...

Tips for efficiently retrieving a record from an array using a JavaScript function

It seems like there might be a simple solution that I'm overlooking, but the code below isn't functioning as expected when the currentRecord is found to be true. How can I efficiently loop through an array and return the record when a match is f ...

jquery counter is malfunctioning

I noticed a strange issue with the counters on my website. They count up as expected when the page loads, but for some reason, when the screen width shrinks below 800px, they don't start automatically. However, if I quickly scroll to where the counter ...

When properties remain unchanged, they do not hold the same value in a Firestore-triggered Cloud Function

Within my Firestore database, there is a collection named events consisting of documents with attributes such as begin, end, and title. The function in question is triggered when any changes occur within a document. The begin and end fields are both categ ...

What is the best way to position my logo on top of the stunning space background?

Click here to check out the code on CodePen Please take a look at my code on codepen.io. I am new to Stack Overflow so please be patient with me if I make any mistakes. ;-; I currently have my logo (https://i.stack.imgur.com/W0nWc.png) included in the co ...

What methods can be utilized to avoid displaying a Bootstrap popover intermittently within an AngularJS framework?

When using the bootstrap popover in a custom AngularJS directive, I need to display an error message when the button is disabled by setting $scope.buytypeButton= {type:false}. The error message should be displayed in a popover. On the other hand, when $sco ...

The delete button is designed to remove the record from the database while keeping it visible on the frontend

When the delete button is pressed, it successfully removes the record from the database. However, it does not disappear from the frontend until the page is reloaded or refreshed. This is what I see: @foreach (var item in Model) { <a href="#" ...

What could be causing my jQuery AJAX to trigger on all buttons, not just the submit button?

I am new to using AJAX form submission and I have encountered an issue. The submit function is triggered when clicking any button within the form, not just the submit button as intended. I plan to eventually connect the clear button to a form clearing acti ...

What is the best method for sending data from two PHP files to a MySQL database?

I am attempting to merge information from p1.php with data from p2.php and submit them together to mySQL. In p1.php: $.post("p2.php", $("#myForm1").serialize()); and then redirect to p2.php : location.href='p2.php'; In p2.php, there is also a ...