【问题标题】:Laravel 4 model controller viewLaravel 4 模型控制器视图
【发布时间】:2013-09-24 16:57:52
【问题描述】:

如何在模型中使用它

现在示例 1 可以正常工作...在控制器上

$songs = DB::table('songlist')
        ->join('historylist', 'songlist.id', '=', 'historylist.songID')
        ->select('songlist.*', 'historylist.*')
        ->orderBy('historylist.date_played', 'DESC')
        ->first();

return View::make('currentsong')
             ->with('songs', $songs);

但我想从我的模型中得到它

class Radio extends Eloquent
{


public static $timestamps = true;


}

我希望我的模型看起来像

类 Radio 扩展了 Eloquent {

public static function getSonginfo()
{
 $songs = DB::table('songlist')
        ->join('historylist', 'songlist.id', '=', 'historylist.songID')
        ->select('songlist.*', 'historylist.*')
        ->orderBy('historylist.date_played', 'DESC')
        ->first();

   }
}

现在我如何将这个模型传递到我的控制器并查看并调用变量,就像我现在所做的一样 $songs->title 在我的视图中

我真正不明白的部分是如何在控制器上调用模型函数并将其解析到视图中,可能是这样的

$songs = Radio::getSonginfo();

return View::make('currentsong')->with('songs', $songs);

现在,当我在视图 {{ $songs->title}} 上调用它时,我得到未定义的变量 $songs。

任何帮助谢谢。

对不起,我生疏的英语。

【问题讨论】:

标签: php laravel laravel-4


【解决方案1】:

您需要返回模型中的变量。

public static function getSonginfo()
{
 $songs = DB::table('songlist')
        ->join('historylist', 'songlist.id', '=', 'historylist.songID')
        ->select('songlist.*', 'historylist.*')
        ->orderBy('historylist.date_played', 'DESC')
        ->first();

  return $songs;
}

【讨论】:

    猜你喜欢
    • 2013-07-05
    • 1970-01-01
    • 1970-01-01
    • 2013-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多