Route::get('/user', 'UserController@index');
https://laravel.com/docs/5.6/routing - 3rd paragraph
hi
i want to call a controller function through my route.
Route::get('listcomp',function(){ $page_title="Invoice Management App - List of company"; $companyall=Contapp::viewallcomp();
i want to know how i can do this?
i want to create a pagination for this.
Route::get('/user', 'UserController@index');
https://laravel.com/docs/5.6/routing - 3rd paragraph
// ContappController.php
public function index()
{
$companyall = Contapp::paginate(10); // 10 per page
return view('path-to-view', compact('companyall'));
}
// you view file.
@foreach ( $companyall as $company)
{{ $company->name }}
@endforeach
// to display pagination links
{{ $companyall->links() }}
actually in your code blade will not find its controller because it will display more like Undefined variable: companyall
Use your route to call the method in the controller (see my message above)
Write the method as @Sergiu17 showed you, and as you can see the last line call your blade view.
public function index()
{
$companyall = Contapp::paginate(10); // 10 per page
return view('listcomp', ['companyall' => $companyall, 'page_title' => 'Invoice Management App - List of company']);
}
and then, companyall will be available in view.
:) no its a same error always coming. let me give you a folder structure
app/Http/Controller/ContApp.php -> lets say we are having that code.
now in my blade view it cant determine that variable $companyall. We have to open the app class and link to its method.
Undefined variable: companyall (View: D:\web\htdocs\invoiceapp\invoiceapp\resources\views\companies\listcomp.blade.php)
my route file look like this
Route::get('listcomp',function(){ $page_title="Invoice Management App - List of company";
if( Session::has('myusername') )
{
return view('companies.listcomp',compact('page_title'));
}
else{
return redirect('index')->with('status','Please login to access this app');
}
});
@wontoneesaju delete everything and start from scratch.
// route
Route::get('/list-companies', 'CompaniesController@index')->name('list.companies.index');
// CompaniesController.php index method.
public function index()
{
$companies = Company::paginate(10); // 10 per page
return view('companies.list-companies', compact('companies'));
}
// list-companies.blade.php
@foreach ( $companies as $company)
{{ $company->name }}
@endforeach
// to display pagination links
{{ $companies->links() }}
This is what you need, good variable names, good controller name, and good model name. And everything is working, the variable is available.
Route::get('/list-companies','CompaniesController@index')->name('list.companies.index.);
this syntax is wrong....name('list.companies.index.); one comma missing. Can you tell me list.companies.index? how this line comes?
thats only specify the route name for the controller action i think
Route::get('/list-companies','CompaniesController@index')->name('list.companies.index');
naming a route provide a shortcut to use it afterwards. https://laravel.com/docs/5.6/routing#named-routes
i just changed my code yesterday with this way. And the logic works well.
public static function viewallcomp(){
$companyall=DB::table('company')->paginate(10);
if(Session::has('myusername')){
return view('companies.listcomp', ['companyall' => $companyall,'page_title'=>'Invoice Management App - List of company']);
}
else{
return redirect('index')->with('status','Please login to access this app');
}
}
and in route
Route::get('listcomp','CompanyController@viewallcomp');
and same like this way
// list-companies.blade.php @foreach ( $companies as $company) {{ $company->name }} @endforeach
// to display pagination links {{ $companies->links() }}
Good job!
Please or to participate in this conversation.