/** * Display a listing of the resource. * * @return Response */ public function index() { if (Auth::user()->is_admin) { $songs = \App\Song::all(); return view('canciones.index', compact('songs')); } return redirect('inicio'); }
| and give it the controller to call when that URI is requested. | */ use App\Song; Route::get('/', function () { return view('welcome'); }); Route::get('songs', function () { ## 1. Variante um einen Datensatz zu erstellen $song = new Song(); $song->titel = 'Pump Up The Jam'; $song->save(); ## 2. Variante direkt ## Achtung im Model muss protected $fillable (white list) oder $guarded (black list) gesetzt sein $noch_ein_song = Song::create(['titel' => 'What a wonderful world']); return Song::all(); //alle songs //return Song::find(1); //song mit id 1 }); ## Anonyme Funktion mit Parametern innerhalb der URL Route::get('/Benutzer/{name}', function ($user) { return 'Hallo ' . $user; }); ## About mit einer einfachen View mit übergebenen Werten Route::get('/About', function () { ## Array Deklaration in vereinfachter Schreibweise seit PHP 5.4 $fw = ['name' => 'Laravel', 'version' => '5.1.11', 'notes' => 'Mein erster Eindruck ist <b>sehr gut</b>!']; ## Variante 1: Übergabe der Variable $fb mit with //return view ('about')->with('framework', $fw); ## Variante 2: Vereinfachte Übergabe //return view ('about')->withFramework($fw);
/** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index() { $songs = Song::all(); return view('songs.index', compact('songs')); }
public function index(Request $request) { return response()->json(Song::all()); }