wontoneesaju's avatar

calling controller function in route

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.

0 likes
11 replies
Sergiu17's avatar
// 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() }}
wontoneesaju's avatar

actually in your code blade will not find its controller because it will display more like Undefined variable: companyall

Vilfago's avatar

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.

wontoneesaju's avatar

:) 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)

wontoneesaju's avatar

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



}

});

Sergiu17's avatar

@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.

wontoneesaju's avatar

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

wontoneesaju's avatar

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

Please or to participate in this conversation.