簡體   English   中英

在Laravel的控制器/視圖中使用模型中的函數

[英]Using a function from a model in a controller/view in Laravel

我正在嘗試從放置在模型的函數中的查詢訪問數據。 我試圖在控制器中調用該函數,然后將數據發送到視圖。 到目前為止,我一直沒有成功。 這是代碼:

型號: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:/ Applications / MAMP / htdocs / crowdsets / laravel-master / app / storage / views / 2ed7fc8952dab08cf4cb4f4e3d40d1ab:100中的消息'試圖獲取非對象的屬性'的異常'ErrorException'

更新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

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

如果您在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() }}}

(以上內容是從工作代碼中復制和編輯的-如果我輸入過錯,則為apols)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM