如何在laravel View中调用方法?

I have controller which will load the view part

public function homePage(){
    $categories = Category::all();
    $products = Product::all();
    return view('cart.index',compact('categories','products'));
}

Here is the View (basically checking for subcategory if there it will show otherwise skip)

<ul class="list-group list-group-bordered list-group-noicon uppercase">
    <li class="list-group-item active">
        @foreach($categories as $row)
            <a class="dropdown-toggle" href="#">{{$row->name}}</a>
            @if({{StapsController::checkSubcategory($row->id)}})
                @foreach($subcategories as $sub)
                    <ul>                                        
                        <li><a href="#"><span class="size-11 text-muted pull-right">(123)</span> {{$sub->s_name}}</a></li>          
                    </ul>
                @endforeach 
            @else
                <li class="list-group-item"><a href="#"><span class="size-11 text-muted pull-right">(189)</span> {{$row->name}}</a></li>
            @endif
        @endforeach

    </li>
</ul>   

Here is the checkSubcategory method

public function checkSubcategory($id){
    $category = Category::find($id);
    $id = $category->id;
    $subcategories = DB::table ('subcategories')
        ->join('categories_subcategories','subcategories.id','=','categories_subcategories.subcategory_id')
        ->join('categories','categories_subcategories.category_id','=','categories.id')
        ->where('categories.id','=',$id)
        ->select('subcategories.name as s_name ')
        ->get(); 

    return $subcategories;
}

But i got syntax Error in line of calling method in View? what could be the error ..is there any mistake calling method in view part?

If the logic of checkSubcategory works fine in the controller itself, you have to transport it to the model. i.e make it model's method so from the view you could able to do something like the following:

....
<a class="dropdown-toggle" href="#">{{$row->name}}</a>
                                        @if({{$row->checkSubcategory($row->id);}})
                                        <ul>
....

In other words, define this method in your Category model.

Place your function some other place, rather than controller. In your case, inside Category model, perhaps?

Then, in View you'll able to use your function like this:

$row->checkSubcategory($row->id)

No need to make function static.

Use Relationship like this and call through object.

public function checkSubcategory()
{
    return $this->hasMany('App\subcategories');
}

There could be one way to do that easily-

  1. Custom Helper Function

Custom Helper Function Personally I prefer you to use custom helper function because you can call this function anywhere in your project.

  • Within your app/Http directory, create a helpers.php file and add your functions.
  • Within composer.json, in the autoload block, add "files": ["app/Http/helpers.php"].
  • Run composer dump-autoload.

Now, whatever you write in your helpers.php file, you can easily access.

** Note: Remember that, in helpers.php file, all the codes need to be function based. Don't use class in this file.

Its not a good practice to call your methods inside a view. In that case, either create a helper file and load the file using composer, or you can use query scope. Give it a try. Using query scope

First add this in your category model.

function scopeCheckSubcategory(){
      DB::raw('(SELECT subcategories.name as s_name
       FROM `subcategories`
       INNER JOIN categories_subcategories on categories_subcategories.subcategory_id = subcategories.id 
       AND categories_subcategories.category_id = categories.id') AS s_name'
      )
}

Now call the scope

$categories = Category::CheckSubcategory->get();

Now check in your blade file

@if($row->s_name)
...
...
@endif

There are several issues in your blade, here is corrected version,

<ul class="list-group list-group-bordered list-group-noicon uppercase">
    <li class="list-group-item active">
        @foreach($categories as $row)
            <a class="dropdown-toggle" href="#">{{$row->name}}</a>
            <ul>
                @if(StapsController::checkSubcategory($row->id)) 
                    <li>
                        <a href="#">
                            <span class="size-11 text-muted pull-right">(123)</span> {{$sub->s_name}}
                        </a>
                    </li>
                @else
                    <li class="list-group-item">
                        <a href="#">
                            <span class="size-11 text-muted pull-right">(189)</span> {{$row->name}}
                        </a>
                    </li>
                @endif
            </ul>
        @endforeach 
    </li>
</ul> 
  1. @if({{StapsController::checkSubcategory($row->id);}}) this line is wrong(syntax error)
  2. You have written two endforeach statements
  3. Your if statement was closing after endforeach statement
  4. You have missing ul tag in in else statement

Check above I hope this helps.