Laravel : passing variable from controller to view

In this tutorial learn how to pass value or variable from controller to view in laravel, Passing variable controller to view using with method, to pass variable in laravel.
return view('category')->with('name',"passing value");       
Create controller CategoryController.php in controller


<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class CategoryController extends Controller
{
    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Contracts\Support\Renderable
     */
    public function index()
    {
        $name = "Nakiya";
        return view('category')->with('name',$name);
    }
}
create a view in views category.blade.php
 Welcome To the Category! {{$name}}            
Dependency Injection & Route Parameters

If your controller method is also expecting input from a route parameter you should list your route parameters after your other dependencies. For example, if your route is defined like so:

web.php

Route::get('/category', 'CategoryController@index')->name('category');
Run with php artisan serve and pass the variable
http://127.0.0.1:8000/category

No comments:

Post a Comment

how to call ssh from vs code

 To call SSH from VS Code, you can use the built-in Remote Development extension. This extension allows you to open a remote folder or works...