Christian Arroyo
Christian Arroyo

Reputation: 49

Laravel 4 model controller view

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.

Upvotes: 0

Views: 1493

Answers (1)

Laurence
Laurence

Reputation: 60068

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

Upvotes: 1

Related Questions