I'm trying to follow a small laravel tutorial.
In my application I have a controller called, HomeController.
In my HomeController, I have the followinf index function
public function index()
{
try {
//get autheticated users selected organisation
$organisation = $this->getSelectedOrganisationByUser(Auth::user());
//set application timezone
$this->setApplicationTimezone($organisation);
$this->setSiteConnection($organisation);
$this->setGAConnection($organisation);
if ($organisation->disabled) {
Auth::logout();
return redirect(route('login'));
}
//sales today
$sum='23';
return view('dashboard.index')
->with('organisation',$organisation,'sum',$sum);
} catch (\Throwable $exception) {
\Sentry\captureException($exception);
}
}
Here I'm trying to send this $sum value to my blade and display it. But, Every time i tried to display this value on my blade I get $sum is undefined error.
I tried to echo this on my blade as follows,
{{ $sum }}
What changes should I make, in order to display that on my blade.
I tried hard, but yet to figure out a solution
You need to pass the variables as array. Try this:
return view('dashboard.index', ['organisation' => $organisation, 'sum' => $sum]);
or another way is by using compact() like this
return view('dashboard.index', compact('organisation', 'sum'));
The method with() from /Illuminate/View/View.php accepts only two parameters.
To send multiple variable, you can
//Call with() for each variable
return view('dashboard.index')
->with('organisation',$organisation)
->with('sum',$sum);
//use an array as second parameter of view
return view('dashboard.index', ['organisation' => $organisation,'sum' => $sum]);
//use compact to create the array using the variable name
return view('dashboard.index', compact('organisation', 'sum'));
//use an array as first parameter in with (using compact or not)
return view('dashboard.index')->with(compact('organisation', 'sum'));
Related
hello guys im having a problem with passing variable from my controller to views, as it does not identify its variable, here is my code:
RegisterController.php
use App\angkatan;
public function index()
{
$select = Angkatan::all();
return view('/auth/register')->with('name', $select);
}
My Route
web.php
Route::get('register', 'RegisterController#index');
and my view
register
#foreach($name as $ps)
#endforeach
the error say
Undefined variable: name (0)
im very thankful if anyone can help
You are just passing the wrong way $select variable to your view.
when you use Illuminate\View\View::with method you should pass an associative array which as key => value pairs
return view('/auth/register')->with(['name' => $select]);
You can also use compact which allows to pass variable which are accessible inside of the scope of your controller to the view and the string passed as argument to that function will be the name of variable accessible inside of the view file
$select = Angkatan::all();
return view('/auth/register', compact('select'));
You can not pass the variable in this way to the view. You have to pass an array in the second parameter of the with() method - it should be something like this:
return view('greeting', ['name' => 'James']);
return view('/auth/register', ['name' => $select]);
you can pass a second massive parameter to the view,
and also in with method, you need to pass massive as I remember.
I have the following route in my laravel project
Route::get('/',[
'uses' => 'MakeController#index'
]);
Controller
class MakeController extends Controller{
public function index(){
$makes = MakeType::all();
return View::make('Index', $makes);
// tried this
// return view('Index',compact('makes'));
}
}
index.blade.php
<select>
<option>Select Make</option>
#foreach($makes as $make)
<option>{{$make->name}}</option>
#endforeach
</select>
Problem:
The problem is when I try to load the index page, it shows me the following error
Use of undefined constant makes - assumed 'makes' (this will throw an Error in a future version of PHP) (View: /customers/6/1/4/alle-voertuigen.nl/httpd.www/resources/views/Index.blade.php)
I've visit all the possible links, and tried different ways of doing it, but nothing is working with me.
This is what it is showing when I do dd($makes), in attributes I have the name column
Please help me, thanks
Use with in your controller.
return View::make('Index')->with(compact('makes'));
Should work.
If you use this way to return a view, you must use with to add parameters :
return View::make('Index')->with('makes', $makes);
I will advise to use the view helper, I don't know what version of Laravel you use :
return view('Index', ['makes' => $makes]);
Just only thing sure, it can't work if you don't pass variables as an array (with compact or ['myVariable' => $var])
class MakeController extends Controller {
public function index() {
$makes = MakeType::all();
return View::make('index', $makes);
// tried this
// return view('index',compact('makes'));
}
}
its index not Index
In Laravel, there is a function return back();, which returns the user to the previous page. Is it possible to return back(); more than once within one function to return the user back twice or several times? I tried
public function ....()
{
return back();
return back();
}
but it doesn't seem to work.
No, but you could use session system to save URLs of 2-3-4 pages back. Use Session:: facade or session() helper for shorter syntax:
$links = session()->has('links') ? session('links') : [];
$currentLink = request()->path(); // Getting current URI like 'category/books/'
array_unshift($links, $currentLink); // Putting it in the beginning of links array
session(['links' => $links]); // Saving links array to the session
And to use it:
return redirect(session('links')[2]); // Will redirect 2 links back
it works for me Redirect::to($request->request->get('http_referrer'))
ok I have my route to my controller (for future crud maybe angular use)
public function Dashboard_Clicks()
{
$DBClicks = DB::table('TotalClicks')->select('Total_Clicks')->get();
return view::('dashboard.pages')->with('$DBClicks', Total_Clicks);
do I use view composer? or another clean simple way to call this in?
}
this has one result a number 45454
I want to be able to get this result in my view like so.
<h1 class="clicks"><strong>{{ $DBClicks->Total_Clicks }} </strong> </h1>
You can try this: (Laravel 5)
return view('dashboard.pages', ['Total_Clicks' => $DBClicks]);
As far as I can recall there are two ways of passing data from controller to views first is compacting the variable in your controller and it will available as it is in your view. An example for the same is following:
$variable = 'somedata';
$array = ['some' => 'data'];
return view('viewName', compact('variable', 'array'));
//now in your view you can access {{$variable}} and #foreach($array as $something)
Second is with :
return view('viewName')->with(['first_var' => $variable, 'first_array' => $array])
//now in your view you can access {{$first_var}} and #foreach($first_array as $array)
I am trying to access a functions variable from another function in the same class. i am fairly new to the concept and I can get it to work in another function but when I try to create it's own function I get an Trying to get property of non-object I know what that means but it's confusing as to what needs to be returned in my function since it does work in my other function.
Function getting the error
public function getEditTotal($id) {
$techs = $this->technician();
$tech = $techs->tech;
var_dump($tech); die;
return View::make('report.edit', array('pageTitle' => 'Edit Report Total', 'id' => $id, 'tech' => $tech));
}`
The function I am trying to call
public function technician() {
$tech = DB::table('technician')
->get();
return $tech;
}
I had that same $tech variable in this function and it worked perfectly fine if I called $this->setComplete($id) instead.
Returned statement in the setComplete($id) function
return View::make('report.total', array('pageTitle' => 'Reporting', 'id' => $id, 'tech' => $tech, 'status' => $status));
I am sure it's just the way it's being returned since that variable is being returned in setComplete($id) in the array. I just don't know how to strictly call it in the technician() function.
When you call $techs = $this->technician(); you are setting the $techs to be whatever the value of the $tech variable in the technician function. That is going to be the result of DB::table('technician')
->get();
Theoretically this should be an array of objects where each object represents one row in the technician table.
If you want to know what's going on, add a var_dump($tech) inside the your technician() function, just prior to the return $tech statement.
Since you indicate it is working as expected, you're getting an array of objects. I'm not sure what you want to do with those, but inside the controller:
foreach ($techs as $tech) {
echo $tech->somefieldInTech;
}
or perhaps
echo $techs[0]->somefieldInTech;
So to be clear, in your laravel template, you might want to pass the entire $techs and foreach through it in the template, although from your code it's not clear what you need to do with the data.