To open a link in a new tab using a controller function in Laravel, you can utilize the target attribute in the HTML anchor tag. Here’s an example:
In your controller, you can define a function that returns the URL you want to open in a new tab:
public function openLinkInNewTab()
{
$url = "https://example.com"; // Replace with your desired URL
return view('open_link', compact('url'));
}
In your open_link.blade.php view file, you can generate the anchor tag with the target attribute set to _blank:
// In html
<a href="{{ $url }}" target="_blank">Open Link in New Tab</a>
Now, when you visit the route associated with the openLinkInNewTab controller function, clicking on the “Open Link in New Tab” anchor tag will open the specified URL in a new browser tab.
Make sure you have defined the necessary routes and associated them with the controller function in your Laravel application.
I’m Abhishek, a DevOps, SRE, DevSecOps, and Cloud expert with a passion for sharing knowledge and real-world experiences. I’ve had the opportunity to work with Cotocus and continue to contribute to multiple platforms where I share insights across different domains:
-
DevOps School – Tech blogs and tutorials
-
Holiday Landmark – Travel stories and guides
-
Stocks Mantra – Stock market strategies and tips
-
My Medic Plus – Health and fitness guidance
-
TrueReviewNow – Honest product reviews
-
Wizbrand – SEO and digital tools for businesses
I’m also exploring the fascinating world of Quantum Computing.
[…] https://www.devopsconsulting.in/blog/open-link-in-new-tab-using-controller-function-in-laravel/ […]