【问题标题】:Cant insert into a table using laravel无法使用 laravel 插入表格
【发布时间】:2014-12-11 17:41:22
【问题描述】:

我有这段代码,当我尝试访问 laravel.dev/cats/create 时出现一个页面:

Whoops, looks like something went wrong.

这是我的 route.php 代码(重要部分):

Route::model('cat', 'Cat');

Route::get('cats/create', function() {
    $cat = new Cat;
    return View::make('cats.edit')
        ->with('cat', $cat)
        ->with('method', 'post');
});

Route::post('cats', function(){
    $cat = Cat::create(Input::all());
    return Redirect::to('cats/' . $cat->id)
        ->with('message', 'Successfully created page!');
});
Route::get('cats/{id}', function($id) {
    $cat = Cat::find($id);
    return View::make('cats.single')
        ->with('cat', $cat);
});

这是我的猫/edit.blade.php 代码:

@extends('master')
@section('header')
<a href="{{('cats/'.$cat->id.'')}}">&larr; Cancel </a>
    <h2>
    @if($method == 'post')
        Add a new cat
    @elseif($method == 'delete')
        Delete {{$cat->name}}?
    @else
        Edit {{$cat->name}}
    @endif
</h2>
@stop

@section('content')
    {{Form::model($cat, array('method' => $method, 'url'=>
    'cats/'.$cat->id))}}

@unless($method == 'delete')
    <div class="form-group">
        {{Form::label('Name')}}
        {{Form::text('name')}}
    </div>
    <div class="form-group">
        {{Form::label('Date of birth')}}
        {{Form::text('date_of_birth')}}
    </div>
    <div class="form-group">
        {{Form::label('Breed')}}
        {{Form::select('breed_id', $breed_options)}}
    </div>
    {{Form::submit("Save", array("class"=>"btn btn-default"))}}
@else
    {{Form::submit("Delete", array("class"=>"btn btn-default"))}}
@endif
    {{Form::close()}}
@stop

我现在不知道问题出在哪里,我有相同的编辑和删除代码,但那些人工作正常,但这个没有!

这是调试器报告的错误:

Trying to get property of non-object (View: D:\Xampp\htdocs\laravel\app\views\cats\single.blade.php)

<?php echo e($cat->name); ?>

错误提及那一行。

感谢您的帮助:))))

编辑: 这是single.blade.php:

@extends('master')
@section('header')
<?php// dd($cat); ?>
<a href="{{url('/')}}">Back to overview</a>
<h2>
    {{{$cat->name}}}
</h2>
<a href="{{url('cats/'.$cat->id.'/edit')}}">
    <span class="glyphicon glyphicon-edit"></span> Edit
</a>
<a href="{{url('cats/'.$cat->id.'/delete')}}">
    <span class="glyphicon glyphicon-trash"></span> Delete
</a>
Last edited: {{$cat->updated_at}}
@stop
@section('content')
    <p>Date of Birth: {{$cat->date_of_birth}} </p>
    <p>
        @if($cat->breed)
            Breed:
            {{link_to('cats/breeds/' . $cat->breed->name,
            $cat->breed->name)}}
        @endif
    </p>
@stop

我不知道为什么当我去猫/创建时我传递给 single.blade.php 我认为这是我出错的地方但不知道我在代码中的哪个地方这样做

【问题讨论】:

  • app/config/app.php打开调试,你会得到一个有用的错误信息
  • 谢谢兄弟,很有用
  • 你能发布整个错误吗?
  • 查看你的数据,在视图的开头尝试 dd($cat)。看看什么是空的。
  • 问题是你显示完全不同的文件。该消息是关于single.blade.php 而不是edit.blade.php 文件。您可能在某处包含此single.blade.php 文件。您应该将其内容添加到问题中

标签: php laravel laravel-4 eloquent blade


【解决方案1】:

问题出在:

Route::get('cats/{id}', function($id) {
    $cat = Cat::find($id);
    return View::make('cats.single')
        ->with('cat', $cat);
});

$id似乎没有找到记录,所以$cat在这里是null,在Blade中不起作用。

您应该检查当前的 url 是什么($id 值是什么)并确保表中有这样的 $id 记录。

【讨论】:

  • 那么为什么调试器会从 single.blade.php 得到错误?
  • @Nabialek 当我在single.blade.php 的第一个中使用dd($cat) 时,它只返回NULL,你想向你展示代码的哪一部分?
  • @ArmanKuroKy 你还没有在你使用View::make('cats.single.blade.php')...的地方展示这段代码
  • 我发现了错误,我认为 cats\{id}cats\create 有冲突并创建称为 id,我得到错误我将 cats\create 移动到高于 cats\{id} 并且一切正常
猜你喜欢
  • 2018-11-18
  • 2014-04-17
  • 1970-01-01
  • 1970-01-01
  • 2012-04-15
  • 2017-04-17
  • 1970-01-01
  • 2019-03-20
  • 1970-01-01
相关资源
最近更新 更多