Undefined variable Laravel - php

I'm trying to make a dropdown list but the code don't recognize the array($comunas), telling that's a undefined variable
This is the controller
class ComunasController extends BaseController {
public function mostrarComunas()
{
$comunas = Comuna::all();
return View::make('pymes.crear', array('comunas' => $comunas));
}
The route
Route::get('pymes/crear', array('uses' => 'ComunasController#mostrarComunas'));
The View
{{Form::select('Comuna', $comunas->Nombre_Comuna, $comunas->Id_Comuna)}}

You should try something like this:
$comunas = Comuna::lists('Nombre_Comuna', 'Id_Comuna');
return View::make('pymes.crear', array('comunas' => $comunas));
In the view:
{{ Form::select('Comunas', $comunas, Input::old('Comunas')) }}

Related

Laravel Undefined variable: intent Facade\Ignition\Exceptions\ViewException

I tried looking for all the possible solutions none of it worked and this is very basic trying to send data from a controller to view in Laravel.
Paymentcontroller
public function payment() {
$plans =[
'Basic' => "Monthly"
];
$intent = $user->createSetupIntent();
return view('pages.subscription', compact('intent', 'plans'));
}
PageController
public function index(string $page)
{
if (view()->exists("pages.{$page}")) {
return view("pages.{$page}");
}
return abort(404);
}
View pages.subscription
<div>
{{ $intent }}
</div>
route
Route::get('{page}', ['as' => 'page.index', 'uses' => 'PageController#index']);
Route::get('/subscription', 'PaymentController#payment');
This makes the page work but doesn't display the data
Move Route::get('/subscription', 'PaymentController#payment'); before Route::get('{page}',.... (it should be your last route in the list).
Currently when you call /subscription endpoint you are calling PageController#index, but it doesn't contain logic of your PaymentController#payment and doesn't pass any data to view.

Cannot pass full array from controller in laravel to a view using redirect()

I am unable to solve passing of array issue
below is my function in controller
public function fetchData($id)
{
$id=base64_decode(urldecode($id));
prod_detail=ProductDetail::select('prod_id','supplier_id','price','open_stock','discount_rate','min_order_level')->where('prod_id','=',$id)->get();
return redirect()->route('prod_d_view', compact($prod_detail));
}
below is my route
Route::get('/product_view', function(){
return view('/admin/product_d_mgt');
})->name('prod_d_view');
below is my error
Undefined variable: prod_detail (View: \admin\product_d_mgt.blade.php)
I am unable to pass the full array from one controller using redirect()->route() to another view
Maybe you can use something like this:
In your controller function:
...
return Redirect::to('product_view')->with('prod_detail', $prod_detail);
And in your product_view.blade.php file (in resources/view directory):
#if(Session::has('prod_detail'))
#foreach (Session::get('prod_detail')as $key => $value)
{{ $value->ColumnName }}
{{ $value->ColumnName2 }}
#endforeach
#endif
It has typo. Missing $ symbol before variable name prod_detail.
correct version:
public function fetchData($id)
{
$id = base64_decode(urldecode($id));
$prod_detail=ProductDetail::select('prod_id','supplier_id','price','open_stock','discount_rate','min_order_level')->where('prod_id','=',$id)->get();
return redirect()->route('prod_d_view', compact($prod_detail));
}

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);
}

Sending route parameter in form

I am trying create form (defined like this: link) but I dont know what is $user->id in syntax
echo Form::open(array('route' => array('route.name', $user->id)))
When I use it I have error:
Undefined variable: user (View: ...)
Could anyone explain ?
You probably should pass your user to the view:
public function index()
{
return view('your-view-name')->with('user', Auth::user());
}

Categories