【问题标题】:Using a function from a model in a controller/view in Laravel在 Laravel 的控制器/视图中使用模型中的函数
【发布时间】:2013-07-05 01:46:57
【问题描述】:

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

型号: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');

           }

控制器:FansController.php

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

        }

查看:

@foreach($fanartists as $fanartist)

{{$fanartist}}

@endforeach

当我运行它时,我得到了错误:

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

感谢您的帮助和建议。

更新:

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

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

            @endforeach

我得到错误:

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

更新 2:

运行:

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

我得到以下输出:

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

【问题讨论】:

  • 错误说明一切都尝试转储 $fanartists,看看它包含什么?它是一个物体吗??
  • hmm,当我尝试时它不会运行页面:
  • 不知道你在做什么??只需在您的视图中尝试&lt;?print_r($fanartists);?&gt; 并评论其他所有内容
  • 当我运行它时,我得到:网站在检索 crowdtest.dev:8888/fans/home 时遇到错误。它可能因维护而停机或配置不正确。

标签: sql controller laravel laravel-4 models


【解决方案1】:

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

【讨论】:

  • 好的,现在我在视图中收到错误:[2013-07-07 18:17:30] log.ERROR: exception 'ErrorException' with message 'Invalid argument provided for foreach( )' 在 /Applications/MAMP/htdocs/crowdsets/laravel-master/app/storage/views/2ed7fc8952dab08cf4cb4f4e3d40d1ab:99
  • 错误可见。因为 $fanartists 为空。在你的 fan_likes() 函数中你没有返回任何东西
  • 好的,是的,这就是问题所在。我用一个新错误更新了我的问题:我无法访问 {{$fanartist->artists.id}} 或任何其他列...
【解决方案2】:

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

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

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

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

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

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

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

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

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

【讨论】:

    猜你喜欢
    • 2013-09-24
    • 2014-12-28
    • 1970-01-01
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    • 2014-12-24
    • 2013-10-16
    • 1970-01-01
    相关资源
    最近更新 更多