Laravel非对象

时间:2018-08-09 21:59:36

标签: laravel laravel-5

我创建了一个简单的Laravel项目。在博客视图中,我具有页面索引(这里是最近5个博客的首页),编辑,显示和创建。现在,如果我从数据库中创建新博客(编辑/删除和显示/读取),则一切正常。但是我无法从站点创建新博客。看到问题了吗?

BlogControllor

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

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate($request, [
            'naslov'=>'Required',
            'slug'=>'Required|alpha_dash|min:5|max:255|unique:blogs,slug',
            'opis'=>'Required',
            'tekst'=>'Required',
            'upload_slike' => 'sometimes|image'
        ]);

        $blog = new Blog;
        $blog->naslov = $request->naslov;
        $blog->slug = $request->slug;
        $blog->opis = $request->opis;
        $blog->tekst = $request->tekst;

        //Sacuvaj novu sliku za blog post
        if ($request->hasFile('upload_slike')) {
            $image = $request->file('upload_slike');
            $filename = time() . '.' . $image->getClientOriginalExtension();
            $location = public_path('slike/' . $filename);
            Image::make($image)->resize(800, 400)->save($location);

            $blog->image = $filename;
        }

        $blog->save();
        return redirect('blog');
    }

路线

Route::resource('blog', 'BlogController');

索引页面上的按钮,用于创建新博客

<a href="{{ route('blog.create') }}" class="btn btn-success" style="margin-bottom: 25px;">Dodaj novu vest</a>

页面create.blade.php

@extends('layouts.bez-sidebar')

<script src="//cdn.tinymce.com/4/tinymce.min.js"></script>
    <script>
         tinymce.init({
            selector: 'textarea',
            plugins: 'link image',
            menubar: false
         });
    </script>

@section('content')
    {!! Form::open(['url'=>'blog','class'=>'form-horizontal', 'files' => true]) !!}
    <div class="">
        <div class="form-group">
            {!! Form::label('naslov', 'Naslov', ['class'=>'control-label col-md-2']) !!}
            <div class="col-md-10">
                {!! Form::text('naslov', null, ['class'=>'form-control', 'placeholder'=>'Unesi naslov']) !!}
                {!! $errors->has('naslov')?$errors->first('naslov'):'' !!}
            </div>
         <div class="form-group">
            {!! Form::label('slug', 'Alias:', ['class'=>'control-label col-md-2']) !!}
            <div class="col-md-10">
                {!! Form::text('slug', null, ['class'=>'form-control', 'required' => '', 'minlenght' => '5', 'maxlenght' => '255', 'placeholder'=>'Unesi alias link za post']) !!}
                {!! $errors->has('slug')?$errors->first('slug'):'' !!}
            </div>
        </div>
            <div class="form-group">
            {!! Form::label('opis', 'Opis', ['class'=>'control-label col-md-2']) !!}
            <div class="col-md-10">
                {!! Form::text('opis', null, ['class'=>'form-control', 'placeholder'=>'Ovde upisite kratak opis vesti']) !!}
                {!! $errors->has('opis')?$errors->first('opis'):'' !!}
            </div>
        </div>
        <div class="form-group">
            {!! Form::label('tekst', 'Tekst', ['class'=>'control-label col-md-2']) !!}
            <div class="col-md-10">
                {!! Form::textarea('tekst', null, ['class'=>'form-control', 'placeholder'=>'Ovde upisite celu vest']) !!}
                {!! $errors->has('tekst')?$errors->first('tekst'):'' !!}
            </div>
        </div>
        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                {{ Form::label('upload_slike', 'Ubacite sliku:')}}
                {{ Form::file('upload_slike') }}
            </div>
        </div>
        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                {!! Form::submit('Sačuvaj', ['class'=>'btn btn-primary']) !!}
            </div>
        </div>
    </div>
    {!! Form::close() !!}
@stop

这个错误

enter image description here

1 个答案:

答案 0 :(得分:0)

图像中的错误表明在打开要删除的表单标签时发生了错误。检查您截取的屏幕截图中的第5行。

您是否在创建屏幕上包括了删除功能;也许在扩展视图layouts.bez-sidebar中?如果是这样的话,那可能就是为什么$blog->id导致trying to get property of non object错误的原因。