doujiu3768 2013-09-19 14:31
浏览 13
已采纳

Laravel 4模型控制器视图

how can i use this in the model

now example 1 this work just fine... on the controller

$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);

but i want to get that from the model my model is

class Radio extends Eloquent
{


public static $timestamps = true;


}

i want my model to look like

class Radio extends Eloquent {

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

   }
}

now how can i pass this model into my controller and view and call the varible as i do right now $songs->title on my view

the part i dont really get is how can i call the model function on the controller and parse it into the view something like this maybe

$songs = Radio::getSonginfo();

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

now when i call this on my view {{ $songs->title}} i get undefined variable $songs.

any help Thanks.

sorry for my rusty english.

  • 写回答

1条回答 默认 最新

  • dongtang5776 2013-09-19 16:09
    关注

    you need to return the variable in your model.

    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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度