简体   繁体   English

如何在Laravel View中调用方法?

[英]How to call method inside 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 这是checkSubcategory方法

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? 但是在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. 如果checkSubcategory的逻辑在控制器本身中工作正常,则必须将其传输到模型中。 ie 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. 换句话说,在您的Category模型中定义此方法。

Place your function some other place, rather than controller. 将函数放置在其他位置,而不是控制器。 In your case, inside Category model, perhaps? 在您的情况下,也许在Category模型中?

Then, in View you'll able to use your function like this: 然后,在View中,您将可以像下面这样使用函数:

$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. 在您的app / Http目录中,创建一个helpers.php文件并添加您的功能。
  • Within composer.json , in the autoload block, add "files": ["app/Http/helpers.php"] . composer.json的autoload块中,添加"files": ["app/Http/helpers.php"]
  • Run composer dump-autoload . 运行composer dump-autoload

Now, whatever you write in your helpers.php file, you can easily access. 现在,无论您在helpers.php文件中编写什么内容,都可以轻松访问。

** Note: Remember that, in helpers.php file, all the codes need to be function based. **注意:请记住,在helpers.php文件中,所有代码都必须基于函数。 Don't use class in this file. 不要在此文件中使用class。

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. 在这种情况下,可以创建一个帮助程序文件并使用composer加载该文件,也可以使用查询范围。 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 ) @if({{StapsController::checkSubcategory($row->id);}})此行是错误的( 语法错误
  2. You have written two endforeach statements 您已经编写了两个endforeach语句
  3. Your if statement was closing after endforeach statement 您的if statementendforeach语句后关闭
  4. You have missing ul tag in in else statement 您在else statement missing ul标签

Check above I hope this helps. 检查上面,希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM