如何在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?

dongren5293
dongren5293 StapsController::checkSubcategory($id)不是静态函数,所以你不能这样调用它。
大约 3 年之前 回复

6个回答

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.

douyue7408
douyue7408 另一个通知,它是条件语句所以你不应该使用{{}}它应该是@if(StapsController :: checkSubcategory($ row-> id))
3 年多之前 回复
doumeng1143
doumeng1143 当我dd()输出我看到它但问题是如何打印从刀片中的方法返回数组?
3 年多之前 回复
dongnong3019
dongnong3019 我已经删除了..
3 年多之前 回复
doubo4824
doubo4824 除掉 ; at ..id);}}
3 年多之前 回复
douhao2856
douhao2856 我仍然得到同样的错误。虽然我把它放在模型中..按照你指示的方式调用方法
3 年多之前 回复
duancong7358
duancong7358 在模型中定义它以充当类的方法。
3 年多之前 回复
drg5577
drg5577 如何调用$ row-> checkSubcategory()函数?
3 年多之前 回复
double2022
double2022 啊,57秒之前。
3 年多之前 回复

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



可能有一种方法可以轻松完成 - </ p>


  1. 自定义助手 功能</ li>
    </ ol>

    自定义帮助程序功能</ strong>
    我个人更喜欢使用自定义帮助程序功能,因为您可以在项目的任何位置调用此函数。 </ p>


    • app / Http </ strong>目录中,创建 helpers.php </ strong>文件并添加
      your函数。< / li>
    • composer.json </ strong>中,在自动加载块中,添加“files”:[“app / Http / helpers.php”] </ code>。< / li>
    • 运行composer dump-autoload </ strong>。</ li>
      </ ul>

      现在,无论你在帮助器中写什么。 php </ strong>文件,您可以轻松访问。 </ p>

      **注意:请记住,在 helpers.php </ strong>文件中,所有代码都需要基于函数。 不要在此文件中使用class。 </ p>
      </ div>

展开原文

原文

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
duanjigua5753
duanjigua5753 与您的查询。 这种方式Category :: CheckSubcategory-> get(); 请阅读此laravel.com/docs/5.4/eloquent#query-scopes
3 年多之前 回复
dragon202076
dragon202076 在哪里调用范围?
3 年多之前 回复

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.

doulingqiu4349
doulingqiu4349 请正确关闭foreach,删除其他部分,如果它没有任何意义或正确关闭它,
3 年多之前 回复
dongluxin6711
dongluxin6711 将你的@if(StapsController :: checkSubcategory($ row-> id))更改为@foreach(StapsController :: checkSubcategory($ row-> id)为$ sub)并检查
3 年多之前 回复
douxianwu2221
douxianwu2221 哎很抱歉,我忘记添加该行,但是在调用该方法后如何打印数据呢?
3 年多之前 回复
dongque1462
dongque1462 配对,你的代码只有一个foreach循环,你可以编辑问题并纠正那个缺少的foreach循环的位置
3 年多之前 回复
duanjiwang2927
duanjiwang2927 我想你没有在你的代码中放置foreach循环..
3 年多之前 回复
doucheng1944
doucheng1944 你还错过了一个foreach循环吗? @if(StapsController :: checkSubcategory($ row-> id))我认为这行是@foreach(StapsController :: checkSubcategory($ row-> id))和$ sub是这个foreach循环的实例,如果我是正确的我 错误
3 年多之前 回复
dongwen3410
dongwen3410 那么你将如何使用{{$ sub-> s_name}}
3 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐