Why my blade have Undefined Variable - Laravel 4.2 - php

This is on my routes.php
Route::get('registration/verify/{confirmation}', ['as'=>'verify', 'uses'=>'HomeController#verify']);
Route::get('login', ['as'=>'login', 'uses'=>'HomeController#getLogin']);
and on my blade is this
then on my HomeController.php where the error belong
public function verify($confirmation)
{
$user = User::where('activation_code', '=', $confirmation)->first();
return Redirect::route('login')
->withInput(['email' => $user->email])
->with('fuck', 'wtf');
}
and I got error like this
Undefined variable: fuck (View: C:\Program Files (x86)\Ampps\www\tridg\local\app\views\auth\login.blade.php)
I don't know where I been wrong, I'm so confident that this is correct.
EDIT1:
I even tried this
public function verify($confirmation)
{
$user = User::where('activation_code', '=', $confirmation)->first();
// $user->active = 1;
// $user->save();
return Redirect::route('login', ['fuck'=>'wtf'])
->withInput(['email' => $user->email]);
}

Nevermind, The answer is {{ Session::get('var_name') }} not {{ $var_name }}

Related

Undefined variable $user->username (Laravel 5.7)

I can't get the data from the database. Getting an error:
ErrorException (E_ERROR)
Undefined variable: user
(View:/Users/alex/Desktop/sites/tj/resources/views/user/submissions.blade.php)
Controller:
public function __construct()
{
$this->middleware('auth', ['except' => ['getById',
'getByUsername', 'submissions', 'comments', 'showSubmissions',
'showComments']]);
}
and
public function showSubmissions($username)
{
$user = new UserResource(
User::withTrashed()->where('username', $username)->firstOrFail(),
true
);
$submissions = SubmissionResource::collection(
Submission::whereUserId($user->id)
->withTrashed()
->orderBy('created_at', 'desc')
->simplePaginate(15)
);
return view('user.submissions', compact('user', 'submissions'));
}
View:
{{ $user->username }}
API:
Route::get('/user', 'UserController#getByUsername');
I need get information about user (username).
What is the problem and where is the error?
Based on your comment you have this route:
Route::get('/submission', function () {
return view('user.submissions');
});
When you are loading this view, you are not passing the user object to it. Then when the view is running, it is trying to access a variable that does not exist.
To fix this, you need to pass a variable to the view you are loading. For example, you could do something like this:
Route::get('/submission', function () {
return view('user.submissions', ['user' => auth()->user()]);
});
Note that you can change how you get the user instance depending on your use case. I am just getting the authenticated user to demonstrate the principle.

Undefined variable: names in Laravel 5.6 app?

I am going to count some table column values using following controller function,
public function showcategoryname()
{
$names = Vehicle::groupBy('categoryname')->select('id', 'categoryname', \DB::raw('COUNT(*) as cnt'))->get();
return view('_includes.nav.usermenu')->withNames($names);
}
then my route is,
Route::get('_includes.nav.usermenu', [
'uses' => 'VehicleController#showcategoryname',
'as' => '_includes.nav.usermenu',
]);
and my usermenu blade file is include with other blade files like this,
div class="col-md-3 ">
#include('_includes.nav.usermenu')
</div>
and usermenu blade view is,
#foreach($names as $name)
{{ $name->categoryname }} ({{ $name->cnt }})
#endforeach
in my url like this
http://localhost:8000/_includes.nav.usermenu
this is working fine. but when i visit other pages include usermenu blade it is generated following error,
Undefined variable: names (View: C:\Users\banda\Desktop\dddd\resources\views\_includes\nav\usermenu.blade.php) (View: C:\Users\banda\Desktop\dddd\resources\views\_includes\nav\usermenu.blade.php)
how can fix this problem?
it's clear that you are just using showcategoryname() method in _includes.nav.usermenu route not in every routes so it can't recognize that variable, it's better to use a global variable in all routes
so in app\Providers\AppServiceProviders.php in boot function use this code to have that variable in all routes:
view()->composer('*', function ($view) {
$names = Vehicle::groupBy('categoryname')->select('id', 'categoryname', \DB::raw('COUNT(*) as cnt'))->get();
$view->with('names', $names);
});
this code runs before any code or controller! actually is feature of boot function!
You can insert this code into boot function in App\Providers\AppServiceProvider class
public function boot(){
$names = Vehicle::groupBy('categoryname')->select('id', 'categoryname', \DB::raw('COUNT(*) as cnt'))->get();
View::share('names', $names);
}

How to solve the "Undefined variable" error in Laravel?

I am using Laravel, and I got an error:
Undefined variable: getFormTest (View: C:\xampp\htdocs\survey\resources\views\tambahformtest.blade.php)
That error references to this view:
<input value="{{ $getFormTest[0]->ms_test }}">
I have put $getFormTest in my controller:
public function TambahFormTest()
{
$ms_id = FormTest::max('ms_id');
$getFormTest = FormTest::Select('ms_test')->where('ms_id', '=', $ms_id)->get();
return view('tambahformtest', $getFormTest);
}
When returning a view in laravel, you have to pass an array of params.
return view ('myView', ['param1' => $v1, 'param2', $v2]);
then in your view
#if(isset($param1)
{{ $params->property }}
#endif
You should take a use of compact method of php
public function TambahFormTest()
{
$ms_id = FormTest::max('ms_id');
$getFormTest = FormTest::Select('ms_test')->where('ms_id', '=', $ms_id)->get();
return view('tambahformtest', compact('getFormTest'));
}
This would be sent to view as - ['getFormTest' => $getFormTest]
Hope this helps

Passing arguments from route through view via controller doesn't work

I'm creating a Laravel 4 webapp and got the following route:
Route::get('products/{whateverId}', 'ProductController#index');
This is my index-function in ProductController:
public function index($whateverId)
{
$products = Product::all();
$data['whateverId'] = $whateverId;
return View::make('products', compact('products'), $data);
}
In my view, this returns the following error:
<p>Product: {{ $data['product'] }}</p>
ErrorException
Undefined variable: data (View: /Users/myuser/webapp/app/views/products.blade.php)
return View::make('products', compact('products'), "data"=>$data);
(or compact('data'))
Try passing it as:
$data['whateverId'] = $whateverId;
$data['products'] = Product::all();;
return View::make('products', $data);
And you'll have acces to it as
{{ foreach($products as ...) }}
and
{{ $whateverId }}
Or you can
$products = Product::all();
$data['whateverId'] = $whateverId;
return View::make('products')
->with('products', $products)
->with('whateverId', $whateverId);

Laravel Error : ErrorException Missing argument 1?

I am using Laravel 4 and I am getting ERROR: when I visit admin/profile/: Missing argument 1 for AdminController::getProfile()
My AdminController code :
public function getProfile($id) {
if(is_null($id)) Redirect::to('admin/dashboard');
$user = User::find($id);
$this->layout->content = View::make('admin.profile', array('user' => $user));
}
My routes.php :
Route::controller('admin', 'AdminController');
My admin/profile (blade) view :
#if(!is_null($user->id))
{{ $user->id }}
#endif
How could I fix this? I want when they go to admin/profile without ($id) to redirect to dashboard.
You told Laravel that your getProfile method has one parameter:
public function getProfile($id) {
}
If you want to a request to succeed, you have to pass it in your URL:
http://appdev.local/admin/profile/1
If you want to see it fail (redirect to dashboard), you'll have to add a default value to your function argument:
public function getProfile($id = null) { ... }
But you better add this value to it anyway, since you can have bots (or even people) trying to access that route without the parameter.
Your view is too generic too, you have to check if your $user is set:
#if(isset($user) && !is_null($user->id))
{{ $user->id }}
#endif
As noted in the comments, the line
if(is_null($id)) Redirect::to('admin/dashboard');
Must have a return:
if(is_null($id)) return Redirect::to('admin/dashboard');
About sharing the user to your layout, the problem is that your getProfile($id) is already passing a $user to your view, so what you could do is to add this to your __construct():
if (Auth::check())
{
$user = Auth::getUser();
View::share('loggedUser', $user);
}
And in your view:
#if(isset($user) && !is_null($user->id))
{{ $user->id }}
#else
{{ $loggedUser->id }}
#endif
About the user not found problem, you have many options, this is one:
public function getProfile($id) {
if (is_null($id))
{
return Redirect::to('admin/dashboard');
}
if ($user = User::find($id))
{
$this->layout->content = View::make('admin.profile', array('user' => $user));
}
else
{
return Redirect::to('admin/dashboard')->withMessage('User not found');
}
Try setting a default null value to $id like this :
public function getProfile($id = null) {
...
}

Categories