HomeController.php第23行中的ErrorException:未定义的变量:home

时间:2017-02-18 22:47:57

标签: php laravel

我使用的是Laravel 5.1 在我创建我的家后,我有问题看到我的index.html视图,可以看到我的数据库保存数据,但我无法在我的网络应用程序上看到它。 我上周一直在使用Laravel 5.3,现在回到5.1实际上比以前更难了......我不知道问题是否在重定向到$ home的视图

我的HomeController:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Flash;
use File;
use App\Home;

class HomeController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $homes = Home::all();

        return view('home.index')->with("homes", $home);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('home.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {

        $data = $request->all();
        $image = $request->file('image');

        if($image)
        {
            $data['image'] = $image->getClientOriginalName();
        }

        $home = new Home($request->all());
        $home->fill($request->all());
        $home->image = $data['image'];
        $home->save();

        if($home->image){
            $path = public_path() . '/image/homes/' . $home->id;
            if( ! File::exists($path)) {
                File::makeDirectory($path, 0775, true, true);
            }
            $imageName = $home->image;
            $request->file('image')->move($path, $imageName);

        }
        flash('Se ha agregado un nuevo slider con exito', 'success');
        return redirect()->route('home.index');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $home = Home::find($id);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $home = Home::find($id);

        return view('home.edit')->with('home', $home);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $data = $request->all();
        $image = $request->file('image');

        if($image)
        {
            $data['image'] = $image->getClientOriginalName();
        }

        $home = new Image($request->all());
        $home->fill($request->all());
        $home->image = $data['image'];
        $home->save();

        if($home->image){
            $path = public_path() . '/image/homes/' . $home->id;
            if( ! File::exists($path)) {
                File::makeDirectory($path, 0775, true, true);
            }
            $imageName = $home->image;
            $request->file('image')->move($path, $imageName);

        }


        Flash::info("El slider ha sido editado con exito!");
        return redirect()->route('home.index');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $path = '/image/homes' . $id;
        $home = Home::find($id);
        $this->deleteFile($path . '/' . $home->image);
        $home->delete();

        Flash::error('El slider ha sido eliminado con exito!');
        return redirect()->route('home.index');
    }
}

1 个答案:

答案 0 :(得分:0)

非常简单,这一个:

$homes = Home::all();

return view('home.index')->with("homes", $home);

$homes更改为$home,或将$home更改为$homes。由你决定。