非对象laravel的ErrorException属性

时间:2017-01-24 21:38:00

标签: php laravel-5.3

我想在主索引上创建一个按钮,以重定向到另一个视图。

index.blade.php:

@extends('layouts.default')

@section('content')

<div class="row">
    <div class="col-lg-12 margin-tb">
        <div class="pull-left">
            <h2>Meniu management locatari</h2>
        </div>
        <div class="pull-right">
            <a class="btn btn-success" href="{{ route('itemCRUD.create') }}"> Creaza intrare noua</a>
        </div>
        <div class="pull-right">
            <a class="btn btn-success" href="{{URL::to('itemCRUD/finalu') }}"> Insert excel</a>
        </div>
    </div>
</div>

@if ($message = Session::get('success'))
    <div class="alert alert-success">
        <p>{{ $message }}</p>
    </div>
@endif

<table class="table table-bordered">
    <tr>
        <th>no</th>
        <th>apartament</th>
        <th>nume</th>
        <th>persoane</th>
        <th>mp</th>
        <th>comentariu</th>
        <th width="280px">Actiune</th>
    </tr>
@foreach ($items as $key => $item)
<tr>
    <td>{{ ++$i }}</td>
    <td>{{ $item->apartament }}</td>
    <td>{{ $item->nume }}</td>
    <td>{{ $item->persoane }}</td>
    <td>{{ $item->mp }}</td>
    <td>{{ $item->comentariu }}</td>
    <td>
        <a class="btn btn-info" href="{{ route('itemCRUD.show',$item->id) }}">Arata</a>
        <a class="btn btn-primary" href="{{ route('itemCRUD.edit',$item->id) }}">Edit</a>
        {!! Form::open(['method' => 'DELETE','route' => ['itemCRUD.destroy', $item->id],'style'=>'display:inline']) !!}
        {!! Form::submit('Sterge', ['class' => 'btn btn-danger']) !!}
        {!! Form::close() !!}
    </td>
</tr>
@endforeach
</table>

{!! $items->render() !!}

@endsection

但是当我按插入Excel格式按钮将我重定向到此视图时

finalu.blade.php:

@extends('layouts.default')

@section('content')


<h1>Helooo</h1>


@endsection

我将收到此ErrorException:

  

bcebe4d151d0a3c5c8008307bf179799c7fba86b.php第20行中的ErrorException:   试图获取非对象的属性(查看:/var/www/html/admintmhelp/resources/views/ItemCRUD/show.blade.php)

这也是我的show.blade.php:

@extends('layouts.default')

@section('content')

<div class="row">
    <div class="col-lg-12 margin-tb">
        <div class="pull-left">
            <h2> Arata intrarea</h2>
        </div>
        <div class="pull-right">
            <a class="btn btn-primary" href="{{ route('itemCRUD.index') }}"> Inapoi</a>
        </div>
    </div>
</div>

<div class="row">

    <div class="col-xs-12 col-sm-12 col-md-12">
        <div class="form-group">
            <strong>apartament:</strong>
            {{ $item->apartament }}
        </div>
    </div>

    <div class="col-xs-12 col-sm-12 col-md-12">
        <div class="form-group">
            <strong>nume:</strong>
            {{ $item->nume }}
        </div>
    </div>

    <div class="col-xs-12 col-sm-12 col-md-12">
        <div class="form-group">
            <strong>persoane:</strong>
            {{ $item->persoane }}
        </div>
    </div>

    <div class="col-xs-12 col-sm-12 col-md-12">
        <div class="form-group">
            <strong>mp:</strong>
            {{ $item->mp }}
        </div>
    </div>

    <div class="col-xs-12 col-sm-12 col-md-12">
        <div class="form-group">
            <strong>comentariu:</strong>
            {{ $item->comentariu }}
        </div>
    </div>

</div>

@endsection

这是我的控制者:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Item;

class ItemCRUDController extends Controller
{

/**
 * Display a listing of the resource.
 *
 * @return \Illuminate\Http\Response
 */
public function index(Request $request)
{
    $items = Item::orderBy('id','DESC')->paginate(5);
    return view('ItemCRUD.index',compact('items'))
        ->with('i', ($request->input('page', 1) - 1) * 5);
}

/**
 * Show the form for creating a new resource.
 *
 * @return \Illuminate\Http\Response
 */
public function create()
{
    return view('ItemCRUD.create');
}
public function finalu()
{
    return view('ItemCRUD.finalu');
}
/**
 * Store a newly created resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return \Illuminate\Http\Response
 */
public function store(Request $request)
{
    $this->validate($request, [
        'apartament' => 'required',
        'nume' => 'required',
        'persoane' => 'required',
        'mp' => 'required',
    ]);

    Item::create($request->all());
    return redirect()->route('itemCRUD.index')
                    ->with('success','Intrare inserata cu succes');
}

/**
 * Display the specified resource.
 *
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function show($id)
{
    $item = Item::find($id);
    return view('ItemCRUD.show',compact('item'));
}

/**
 * Show the form for editing the specified resource.
 *
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function edit($id)
{
    $item = Item::find($id);
    return view('ItemCRUD.edit',compact('item'));
}

/**
 * Update the specified resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function update(Request $request, $id)
{
    $this->validate($request, [
        'apartament' => 'required',
        'nume' => 'required',
    ]);

    Item::find($id)->update($request->all());
    return redirect()->route('itemCRUD.index')
                    ->with('success','Intrare actualizata cu succes');
}

/**
 * Remove the specified resource from storage.
 *
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function destroy($id)
{
    Item::find($id)->delete();
    return redirect()->route('itemCRUD.index')
                    ->with('success','Item deleted successfully');
}
}

路线设置如下:

Route::resource('itemCRUD','ItemCRUDController');

ErrorException可能是什么原因,以及如何避免这种情况?

1 个答案:

答案 0 :(得分:0)

因为您正在使用资源控制器itemCRUD/finalu总是要路由到itemCRUD/show/{id}

要解决此问题,您可能需要添加其他路线。

Route::get('itemCRUD.finalu','ItemCRUDController@finalu');
Route::resource('itemCRUD','ItemCRUDController');