Laravel - Pass more than one variable to view
Just pass it as an array:
$data = [
'name' => 'Raphael',
'age' => 22,
'email' => 'r.mobis@rmobis.com'
];
return View::make('user')->with($data);
Or chain them, like @Antonio mentioned.
How to Pass multiple variables to a single view Laravel?
Yes, you can do like this,
return view('viewblade', compact('userTotal','userActive','userInactive','userOnAndOff'));
take a look at this question and it's answers
passing multiple variable to one view in laravel 5.6
Your site will go to the first route and will never go to your second controller.
You should rather write.
Route
Route::get('sitemap.html','CategoryController@site')->name('sitemap');
Controller
public function site(){
$data = array();
$data['subcategories'] = SubCategory::all();
$data['categories'] = Category::all();
return view('template.sitemap',compact("data"));
}
View
@foreach($data['categories'] as $category)
<li><a href="category.html">{{$category->name}}</a></li>
<ul>
@foreach($data['subcategories'] as $subcategory)
<li><a href="category.html">{{$subcategory->category_name->name}}</li>
@endforeach
</ul>
@endforeach
Best Way to return multiple variables to view in laravel
you can create an array like this
$data['bus']=Bus::all();
$data['user']=User::all();
$data['employer']=employer::all();
return view('create',['data'=>$data]);
Passing more than one variables from different controllers to a single view in Laravel 8
So based on your comment I understand your issue. And you don't need Two controllers and seperate routes.
Create a New controller with name of StudentEnrollmentController
by running
php artisan make:controller StudentEnrollmentController
and paste the below code into controller
<?php
namespace App\Http\Controllers;
use App\Models\Time;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class StudentEnrollmentController extends Controller
{
public function showEnrollmentForm()
{
$timeData = DB::table('time')->get();
$courseData = DB::table('courses')->get();
return view('admin.forms.enrollment', compact('timeData', 'courseData'));
}
}
Add following line to routes/web.php
Route::get('/enrollmentForm', [App\Http\Controllers\App\Http\Controllers::class, 'showEnrollmentForm'])->name('enrollmentForm');
Now visit the URL enrollmentForm
it will Display the form without errors.
how to pass multiple variables in to a view using controller in laravel
Update your controller function like:
$categories = Item::all();
$products = Item::find($id)->products;
return view('category.index', compact('categories', 'products'));
Note: You can not define products variable in your function please define
@if(!empty($products))
@foreach($products as $key=>$product)
<div class="col-md-4 col-sm-6 portfolio-item" id="items">
<div class="card h-100">
<a href="#"><img class="card-img-top" src="/storage/{{ $product->image }}" alt="Product Image"></a>
<div class="card-body">
<h4 class="card-title">
<a href="#"><br />{{ $product->item_name}}</a>
</h4>
<p class="card-text" style="color: #A9A9A9;text-decoration: line-through;">LKR {{ $product->old_price}}</p>
<h4 style="text-align: center; color: #fff;"><a class="waves-effect waves-light btn btn-dark btn-block">LKR {{ $product->new_price}}</a></h4>
</div>
</div>
</div>
@endforeach
@endif
Pass variables to multiple view in laravel
You can make a variable accessible in multiple views using one of these methods for example:
AppServiceProvider ( reference: https://laravel.com/docs/5.6/providers ) with ViewComposer ( reference: https://laravel.com/docs/master/views#view-composers )
You'll need to add to your ServiceProvider boot() method something similar to this:
public function boot()
{
View::share('variable_name', 'some_value_here');
}
Or inside a controller:
public function __construct() {
$something = 'just a test';
View::share('something', $something);
}
how to pass same data to multiple views in laravel
If you're wanting to pass the same data to multiple views within your application you could use View Composers
E.g. in the boot()
method of your AppServiceProvider you would have something like:
public function boot()
{
view()->composer(['home', 'profile'], function ($view) {
$notifications = \App\Notification::all(); //Change this to the code you would use to get the notifications
$view->with('notifications', $notifications);
});
}
Then you would just add the different blade file names (like you would with a route) to the array.
Alternatively, you could share the notifications with all views:
public function boot()
{
$notifications = \App\Notification::all(); //Change this to the code you would use to get the notifications
view()->share('notifications', $notifications);
}
cannot pass multiple variables to view laravel
You're passing it wrong to view. You've to use it something like this:
return view('foo',compact('key'=>'bazz','key'=>'bar','key'=>'foo bar'));
or you can do it something like this:
return view('foo',['key'=>'bazz', 'key'=>'bar', 'key'=>'foo bar']);
Related Topics
PHP Remove Special Character from String
Uploading in Codeigniter - the Filetype You Are Attempting to Upload Is Not Allowed
Split Camelcase Word into Words with PHP Preg_Match (Regular Expression)
Get the Sum of All Digits in a Numeric String
How to Prevent Pdo from Interpreting a Question Mark as a Placeholder
What's an Actual Use of Variable Variables
Unserialize PHP Array in JavaScript
How to Build a JSON Array from MySQL Database
Get Element by Classname with Domdocument() Method
PHP Exec() as Background Process (Windows Wampserver Environment)