未定义的变量:播放列表(查看:C:\ xampp \ htdocs \ laravel53 \ resources \ views \ pages \ playlist \ index.blade.php)

时间:2017-02-03 23:54:17

标签: laravel crud laravel-5.4

我试图制作一个小问题,但我发现了这个错误:

未定义的变量:播放列表(查看:C:\ xampp \ htdocs \ laravel53 \ resources \ views \ pages \ playlist \ index.blade.php)

创造正在运作......但索引没有......

我正在使用laravel 5.4

这是我的控制器:

<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Http\Requests;
use Auth,
    Response,
    Storage,
    Session,
    Redirect,
    Validator,
    Image,
    Input;
use App\Playlist;

class PlaylistsController extends Controller {

    public function index() {

          $playlists = Playlist::all();
          return View('pages.playlist.index')->withPlaylists($playlists);

    }

    public function create() {
        return view('pages.playlist.create');
    }

    public function store(\Illuminate\Http\Request $request) {

        $this->validate($request, [
        'title' => 'required',
        'description' => 'required',
        'authors' => 'required',
        'date' => 'required',
        'url' => 'required',
        'menssage' => 'required',

        ]);
        $input = $request->all();

        Playlist::create($input);


        Session::flash('flash_message', 'Video/img successfully added!');
         return view('admin');

    }
    public function show($id) {

        $playlists = Playlist::findOrFail($id);

        return view('pages.playlist.show')->withPlaylits($playlists);

    }

    public function edit($id) {

        $playlists = Playlist::findOrFail($id);

        return view('pages.playlist.edit')->withPlaylits($playlists);

    }

    public function update(Request $request, $id) {

        $playlists = Playlist::findOrFail($id);

        $this->validate($request, [
        'title' => 'required',
        'description' => 'required',
        'authors' => 'required',
        'date' => 'required',
        'url' => 'required',
        'menssage' => 'required',
        ]);

        $input = $request->all();

    $playlists ->fill($input)->save();

    Session::flash('flash_message', 'Task successfully added!');

    return redirect()->back();

}


    public function destroy($id) {


      $playlists = Playlist::findOrFail($id);

    $playlists ->delete();

    Session::flash('flash_message', 'Task successfully deleted!');

    return redirect()->route('playlist.index');
}

}

我的索引:

<!DOCTYPE html>

<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        @section('content')

<h1>Task List</h1>
<p class="lead">Here's a list of all your tasks. <a href="{{ route('playlist.create') }}">Add a new one?</a></p>

<hr>

   @foreach($playlists as $playlist) 

    <h3>{{ $playlist->title }}</h3>
    <p>{{ $playlist->description}}</p>
    <p>{{ $playlist->authors}}</p>
    <p>{{ $playlist->date}}</p>
    <p>{{ $playlist->url}}</p>
    <p>{{ $playlist->menssage}}</p>

    @endforeach 

    <p>
        <a href="{{ route('playlist.show', $playlist->id) }}" class="btn btn-info">View Task</a>
        <a href="{{ route('playlist.edit', $playlist->id) }}" class="btn btn-primary">Edit Task</a>
    </p>
    <hr>


@stop

        @extends('layouts.master')

@section('content')


    </body>
    </html>

2 个答案:

答案 0 :(得分:0)

控制器

public function index() {

      $playlists = Playlist::all();
      return View('pages.playlist.index')->with(['data', $playlists]);

}

索引刀片中,只需了解,删除与刀片相关的代码。

<?php 
print_r($data);
?>


你可以简单地做

public function index() {

      $playlists = Playlist::all();
      return view('pages.playlist.index',['data'=>$playlists]);

}

并在索引裁剪刀片中执行print_r($data)并查看结果,然后相应地循环显示结果。从索引刀片中删除所有内容,因为您正在那里扩展主刀片。因此,删除所有内容(HTML),只需在索引刀片中写入一行代码,即print_r($ data)。

答案 1 :(得分:0)

你应该在你的视图中写下这个

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        @section('content')

<h1>Task List</h1>
<p class="lead">Here's a list of all your tasks. <a href="{{ route('playlist.create') }}">Add a new one?</a></p>
<hr>
   @foreach($playlists as $playlist) 

    <h3>{{ $playlist->title }}</h3>
    <p>{{ $playlist->description}}</p>
    <p>{{ $playlist->authors}}</p>
    <p>{{ $playlist->date}}</p>
    <p>{{ $playlist->url}}</p>
    <p>{{ $playlist->menssage}}</p>

    <p>
        <a href="{{ route('playlist.show', $playlist->id) }}" class="btn btn-info">View Task</a>
        <a href="{{ route('playlist.edit', $playlist->id) }}" class="btn btn-primary">Edit Task</a>
    </p>
@endforeach 
    <hr>
@stop
        @extends('layouts.master')
@section('content')
    </body>
    </html>

在你的控制器方法中你写这个

public function index() {
          $playlists = Playlist::all();
          return View('pages.playlist.index', comapct('playlists'));
    }

这将解决您的问题