Passing data from controller to view in Laravel
Can you give this a try,
return View::make("user/regprofile", compact('students')); OR
return View::make("user/regprofile")->with(array('students'=>$students));
While, you can set multiple variables something like this,
$instructors="";
$instituitions="";
$compactData=array('students', 'instructors', 'instituitions');
$data=array('students'=>$students, 'instructors'=>$instructors, 'instituitions'=>$instituitions);
return View::make("user/regprofile", compact($compactData));
return View::make("user/regprofile")->with($data);
Pass data from controller to View in Laravel
This is the the definition of the view
helper
function view($view = null, $data = [], $mergeData = []) { }
You are misusing the function by giving it three separate arrays expecting it to get them as $data
.
Fixes
return view('ausencia', [
'tabela' => $tabela,
'itens' => $ferias,
'dias_usados' => $dias_usados,
]);
return view('ausencia')
->with(compact('tabela'))
->with(['itens' => $ferias])
->with(['dias_usados' => $dias_usados]);
return view(
'ausencia',
array_merge(
compact('tabela'),
['itens' => $ferias],
['dias_usados' => $dias_usados]
)
);
Laravel how to pass data from controller to blade
Thank you all for your tips. The problem was the capital letters I was using in the blade! I will add below the excerpts in case someone else has relevant problem in future projects:
1) Routes.php (or web.php for newer laravel versions):
<?php
/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/
Route::get('/search', 'PagesController@search'); //find student data!
Route::post('/dedomena', 'NewController@psaxe');
2) Search.blade.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<!--<link rel="stylesheet" type="text/css" href="form_style.css"> -->
<title>Εισαγωγή Δεδομένων Χρήστη</title>
</head>
<body>
<!--
<k1 class = "myhome">Εισαγωγή Δεδομένων</k1>
-->
Εισαγωγή Δεδομένων
<form method="post" action="/dedomena" id="forma">
{{csrf_field()}}
<fieldset>
<legend>Εισαγωγή</legend>
Αριθμός Μητρώου:<br>
<input type="number" name="AM" min="1000000" max="9999999"><br>
<input type="submit" value="Αναζήτηση">
</fieldset>
</form>
</body>
</html>
3) Controller (NewController.php)
<?php namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DB;
class NewController extends Controller
{
public function psaxe(Request $request)
{
$myVar = $request->get('AM');
echo('Αποτελέσματα για τον φοιτητή με ΑΜ: ');
echo($myVar);
$entries = DB::table('SCC_ANSWER')->where('AM', $myVar)->get();
// dd($entries);
return view('pinakas', ['entries1' => $entries]);
}}
4) The blade (pinakas.blade.php)
<!DOCTYPE html>
<html lang="el">
<head>
<meta charset="UTF-8">
<title>Αποτελέσματα</title>
</head>
<body>
<table border="1">
<tr>
<th>AM</th>
<th>ST</th>
<th>CS</th>
<th>REGYEAR</th>
<th>REGTERMIN</th>
<th>CSYEAR</th>
<th>REGTYPE</th>
<th>REGDESCR</th>
<th>STATUS</th>
<th>STGROUP</th>
<th>SEM1</th>
<th>SEM2</th>
<th>SEM3</th>
<th>SEM4</th>
<th>SEM5</th>
<th>SEM6</th>
<th>SEM7</th>
<th>SEM8</th>
<th>SEM9</th>
<th>SEM10</th>
<th>SEM11</th>
<th>SEM12</th>
<th>SEM1B</th>
<th>SEM2B</th>
<th>SEM3B</th>
<th>SEM4B</th>
<th>SEM5B</th>
<th>SEM6B</th>
<th>SEM7B</th>
<th>SEM8B</th>
<th>SEM9B</th>
<th>SEM10B</th>
<th>SEM11B</th>
<th>SEM12B</th>
<th>SEM1P</th>
<th>SEM2P</th>
<th>SEM3P</th>
<th>SEM4P</th>
<th>SEM5P</th>
<th>SEM6P</th>
<th>SEM7P</th>
<th>SEM8P</th>
<th>SEM9P</th>
<th>SEM10P</th>
<th>SEM11P</th>
<th>SEM12P</th>
<th>TOTAL</th>
<th>TOTALB</th>
<th>TOTALP</th>
</tr>
@foreach($entries1 as $entry)
<tr>
<td>{{ $entry->am }}</td>
<td>{{ $entry->st }}</td>
<td>{{ $entry->cs }}</td>
<td>{{ $entry->regyear }}</td>
<td>{{ $entry->regtermin }}</td>
<td>{{ $entry->csyear }}</td>
<td>{{ $entry->regtype }}</td>
<td>{{ $entry->regdescr }}</td>
<td>{{ $entry->status }}</td>
<td>{{ $entry->stgroup }}</td>
<td>{{ $entry->sem1 }}</td>
<td>{{ $entry->sem2 }}</td>
<td>{{ $entry->sem3 }}</td>
<td>{{ $entry->sem4 }}</td>
<td>{{ $entry->sem5 }}</td>
<td>{{ $entry->sem6 }}</td>
<td>{{ $entry->sem7 }}</td>
<td>{{ $entry->sem8 }}</td>
<td>{{ $entry->sem9 }}</td>
<td>{{ $entry->sem10 }}</td>
<td>{{ $entry->sem11 }}</td>
<td>{{ $entry->sem12 }}</td>
<td>{{ $entry->sem1b }}</td>
<td>{{ $entry->sem2b }}</td>
<td>{{ $entry->sem3b }}</td>
<td>{{ $entry->sem4b }}</td>
<td>{{ $entry->sem5b }}</td>
<td>{{ $entry->sem6b }}</td>
<td>{{ $entry->sem7b }}</td>
<td>{{ $entry->sem8b }}</td>
<td>{{ $entry->sem9b }}</td>
<td>{{ $entry->sem10b }}</td>
<td>{{ $entry->sem11b }}</td>
<td>{{ $entry->sem12b }}</td>
<td>{{ $entry->sem1p }}</td>
<td>{{ $entry->sem2p }}</td>
<td>{{ $entry->sem3p }}</td>
<td>{{ $entry->sem4p }}</td>
<td>{{ $entry->sem5p }}</td>
<td>{{ $entry->sem6p }}</td>
<td>{{ $entry->sem7p }}</td>
<td>{{ $entry->sem8p }}</td>
<td>{{ $entry->sem9p }}</td>
<td>{{ $entry->sem10p }}</td>
<td>{{ $entry->sem11p }}</td>
<td>{{ $entry->sem12p }}</td>
<td>{{ $entry->total }}</td>
<td>{{ $entry->totalb }}</td>
<td>{{ $entry->totalp }}</td>
</tr>
@endforeach
</table>
</body>
</html>
5) And the PagesController.php
<?php namespace App\Http\Controllers;
use Illuminate\Http\Request; //sos:: needed only for function emfanisi!!!!
use DB; //sos:: needed only for function emfanisi!!!!
class PagesController extends Controller
{
public function search()
{
return view('search');
}
}
passing data from controller to blade view laravel
route
Route::get('eventpage', 'EventController@index')->name('eventpage');
event Controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class EventController extends Controller
{
public function index(Request $request)
{
$id = $request->query('id');
$event = DB::table('events')->where('id',$id)->get();
$pics = DB::table('pictures')->where('event_id',$id)->get();
return view('pages.eventPage',compact('event','pics'));
}
}
blade code
@for($i = 1; $i < count($pics); $i++)
<li data-target="#carousel-example-generic" data-slide-to="{{ $i }}"></li>
@endfor
Passing data from controller to view in Laravel - Display the error message Undefined variable
The steps to pass data to subview is by passing the data from the parent (In your case it is the homepage) to the subview (recent-gallery) with the help of the blade syntax.
You must first pass the data to the Homepage from your function which initialize your homepage route and pass the image data for your subview.
public function viewHomePage()
{
$images = ImageGallery::all();
// passing your image data to the homepage not to your subview
return view('home', ['images' => $images]);
}
You do not have to have a separate route for your subview as it is a subview. Pass the data you passed along from your Homepage view function to the subview by blade syntax @include('partials/recent-gallery', ['images' => $images])
. Notice we have not routed a subview in web.php
or have a controller function to pass image
data.
home.blade.php
<!-- homepage content -->
@include('partials/recent-gallery', ['images' => $images])
Then you can access the image data inside your subview with the parameter $images
. You can check your passing data with dd()
method and passing the parameters dd($images)
so you can debug.
partials/recent-gallery.blade.php
@if($images->count())
@foreach($images as $image)
{{ $image->galley_image }}
@endforeach
@endif
How to pass data from controller to view in Laravel
If you add get()
to the end of your $category = Categories::where(['restorant_id' => $restorant_id]);
statement, you'll have an Eloquent collection returned:
$category = Categories::where(['restorant_id' => $restorant_id])->get();
Pass the $category
variable to your view as you are currently, consider renaming it to $categories
though just to infer that there could be multiple.
Then in your view
you can loop over the Category
results and access the name
property:
@forelse ($category as $cat)
{{ $cat->name }}
@empty
No categories.
@endforelse
Update
If you want to get items
by their category_id
, you can either do as you have done with $categories
:
$items = Items::where(['category_id' => $category_id])->get();
Alternatively, if you have an items
relationship on your Categories
model you can access them that way:
$category = Categories::with('items')
->where(['restorant_id' => $restorant_id])
->get();
The above will eager load the items
related to a category
which you can then access in your view, for example:
@forelse ($category as $cat)
{{ $cat->name }}
@foreach ($cat->items as $item)
{{ $item->name }}
@endforeach
@empty
No categories.
@endforelse
Laravel passing data from Controller to view without refreshing
Without refresh you need to implement a kind background task which pulls the data to frontend.
You could do this out of the box with https://laravel-livewire.com/ or implement it via vuejs.
LARAVEL 8 - cannot pass data from controller to view - undefined variable $test
You can't have multiple Route::get() function with the same route. So, if you need to call any extra function in your controller, do that in controller.
Route::get('/edit', [App\Http\Controllers\EditController::class, 'index']);
In EditController.php
public function index() {
// run the codes
// call other function.
$this->openEditBlade();
// return view
return view('edit')->with('tables',$tables);
}
Related Topics
How to Specify Composer Install Path
How to Add a Line Break Within Echo in PHP
Wamp/MySQL Errors Not in Correct Language
How to Convert Seconds to Time Format
How to Pass Arguments to My Function Through Add_Action
Proper Url Forming with a Query String and an Anchor Hashtag
Sqlstate[Hy000] [1698] Access Denied for User 'Root'@'Localhost'
How to Store an Array into MySQL
How to Check an Ip Address Is Within a Range of Two Ips in PHP
PHP Regular Expression for Strong Password Validation
PHP 5.4 - 'Closure $This Support'
How to Continue a Session from One Page to Another with PHP