在 Laravel 的控制器/视图中使用模型中的函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17514590/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 08:06:12  来源:igfitidea点击:

Using a function from a model in a controller/view in Laravel

sqlcontrollerlaravellaravel-4models

提问by user1072337

I am trying to access data from a query that I have placed within a function in a model. I am trying to call the function within a controller, and then send along that data to a view. So far, I have been unsuccessful. Here is the code:

我正在尝试从放置在模型函数中的查询访问数据。我试图在控制器中调用该函数,然后将该数据发送到视图。到目前为止,我一直没有成功。这是代码:

Model: Fanartist.php

模型:Fanartist.php

public function fan_likes() {
        $fan_likes = DB::table('fanartists')
                    ->join('artists', 'fanartists.fan_id', '=', 'artists.id')
                    ->where('fanartists.fan_id', '=', Auth::user()->id)
                    ->select('artists.id', 'artists.stage_name', 'artists.city', 'artists.state', 'artists.image_path', 'artists.description');

           }

Controller: FansController.php

控制器:FansController.php

public function getHome() {
            return View::make('fans.home')
            ->with('fans', Fan::all())
            ->with('fanartists', Fanartist::fan_likes());

        }

View:

看法:

@foreach($fanartists as $fanartist)

{{$fanartist}}

@endforeach

When I run this, I get the error:

当我运行这个时,我收到错误:

Non-static method Fanartist::fan_likes() should not be called statically, assuming $this from incompatible context

Thank you for your help and suggestions.

感谢您的帮助和建议。

UPDATE:

更新:

New error. I am returning the view, but now, trying to run this:

新错误。我正在返回视图,但是现在,尝试运行它:

@foreach($fanartists as $fanartist)
                {{$fanartist->artists.id}}

            @endforeach

I get the error:

我收到错误:

log.ERROR: exception 'ErrorException' with message 'Trying to get property of non-object' in /Applications/MAMP/htdocs/crowdsets/laravel-master/app/storage/views/2ed7fc8952dab08cf4cb4f4e3d40d1ab:100

log.ERROR:异常“ErrorException”,消息“试图获取非对象的属性”在 /Applications/MAMP/htdocs/crowdsets/laravel-master/app/storage/views/2ed7fc8952dab08cf4cb4f4e3d40d1ab:100

UPDATE 2:

更新 2:

running:

跑步:

@foreach($fanartists as $fanartist)
<?php var_dump($fanartist); ?>          
            @endforeach

I get the following output:

我得到以下输出:

NULL array(6) { [0]=> string(10) "artists.id" [1]=> string(18) "artists.stage_name" [2]=> string(12) "artists.city" [3]=> string(13) "artists.state" [4]=> string(18) "artists.image_path" [5]=> string(19) "artists.description" } bool(false) string(10) "fanartists" array(1) { [0]=> object(Illuminate\Database\Query\JoinClause)#201 (3) { ["type"]=> string(5) "inner" ["table"]=> string(7) "artists" ["clauses"]=> array(1) { [0]=> array(4) { ["first"]=> string(17) "fanartists.fan_id" ["operator"]=> string(1) "=" ["second"]=> string(10) "artists.id" ["boolean"]=> string(3) "and" } } } } array(1) { [0]=> array(5) { ["type"]=> string(5) "Basic" ["column"]=> string(17) "fanartists.fan_id" ["operator"]=> string(1) "=" ["value"]=> string(1) "1" ["boolean"]=> string(3) "and" } } NULL NULL NULL NULL NULL NULL

采纳答案by Trying Tobemyself

Fanartist::fan_likes()is not supposed to be called statically, but if you want to call it statically then change your fan_likes function to static. for your example

Fanartist::fan_likes()不应静态调用,但如果您想静态调用它,请将 fan_likes 函数更改为静态。对于你的例子

public static function fan_likes() {
    $fan_likes = DB::table('fanartists')
                ->join('artists', 'fanartists.fan_id', '=', 'artists.id')
                ->where('fanartists.fan_id', '=', Auth::user()->id)
                ->select('artists.id', 'artists.stage_name', 'artists.city', 'artists.state', 'artists.image_path', 'artists.description');
return $fan_likes;
       }

回答by jacanterbury

if you have a non-static function like this in your MyClass controller:

如果您的 MyClass 控制器中有这样的非静态函数:

    public function my_func( ) {
            // do stuff
    return ( $stuff ) ;
}

then in your controller, you can call it with code like this:

然后在您的控制器中,您可以使用如下代码调用它:

$local_obj = MyClass::findOrFail( $myobj_id );
$rval = $local_obj->my_func();

if you want to call it from your view, I think you need to pass the object to the view similar to this:

如果你想从你的视图中调用它,我认为你需要将对象传递给类似于这样的视图:

$data['my_obj'] = $local_obj;
return View::make('view_name.show')->with('data',$data);

and then to use in the view use something like this:

然后在视图中使用这样的东西:

{{{$data['my_obj']->my_func() }}}

(above is copied and edited from working code - apols if i've made a typo )

(以上是从工作代码中复制和编辑的 - 如果我打错了 apols )