Laravel属性非对象错误

时间:2013-07-04 22:00:28

标签: php laravel laravel-4

我在尝试理解laravel时遇到了一个小麻烦,需要一些帮助。

我有一个“约会”模型,控制器和各种视图。

型号:     

class Appointment extends Eloquent {

     protected $primaryKey = 'appointment_ID';
     protected $fillable = array('puser_ID', 'apt_date', 'apt_time', 'purpose');
     public $timestamps = false;


}

控制器:     

class AppointmentsController extends BaseController{

    public $restful = true;

    public function getIndex(){
        return View::make('appointments.index')
        ->with('title','Docket: Appointments & Scheduling')
        ->with('appointments', Appointment::OrderBy('apt_time')->get());
    }

    public function getView($appointment_ID){
        return View::make('appointments.view')
        ->with('title', 'Appointment View')
        ->with('appointment', Appointment::find($appointment_ID));

    }
    public function getNew(){
        return View::make('appointments.index')
        ->with('title', 'Add A New Appointment');
    }
}

“添加新约会”视图代码:

extends('layouts.default')

@section('content')
    <h1>Add A New Appointment</h1>

    {{ Form::open(array('url'=>'appointments/create', 'method'=>'post')) }}

<p>
    {{Form::label('puserID', 'User ID')}}<br/>
    {{Form::text('puser_ID'}}
</p>

<p>
    {{Form::label('aptDate', 'Appointment Date')}}<br/>
    {{Form::text('apt_date')}}
</p>        

<p>
    {{Form::label('aptTime', 'Contact Number 1')}}<br/>
    {{Form::text('aptTime'}}
</p>

<p> 
    {{Form::label('purpose', 'Purpose of Visit')}}<br/>
    {{Form::text('purpose'}}
</p>    

<p> {{Form::submit('Add Appointment')}}</p>

{{ Form::close() }}

@endsection

和我的Routes.php文件(更新):

Route::get('appointments',array('as'=>'appointments','uses'=>'AppointmentsController@getIndex'));

Route::get('appointments/{appointment_ID}',array('as'=>'appointment','uses'=>'AppointmentsController@getView'));

Route::get('appointments/new',array('as'=>'newAppointment','uses'=>'AppointmentsController@getNew')    );

我试图使用索引视图中的按钮从“索引”视图(列出所有约会)转到“添加新约会”视图:

<p>{{ HTML::linkRoute('newAppointment', 'New Appointment') }}</p>

但是当我从索引视图中按下“新建约会”按钮时,代码正在抛出“试图获取非对象的属性”错误。

请帮忙。

默认/布局按要求查看:

<!DOCTYPE html>
<html>
<head>
<title>{{$title}}</title>
</head>
<body>
    @if(Session::has('message'))
        <p style="color: green;">{{ Session::get('message') }}</p>
    @endif

    @yield('content')
</body>
</html>

错误讯息:

ErrorException
Trying to get property of non-object
open: /Library/WebServer/Documents/laravel-master/app/storage/views/5258152a378521b65c6fd9801aedd9fd
<?php $__env->startSection('content'); ?>

    <h1><?php echo e($appointment->puser_ID); ?></h1>
    <p><small><?php echo e($appointment->apt_date); ?></small></p>
    <p><small><?php echo e($appointment->apt_time); ?></small></p>
    <p><small><?php echo e($appointment->purpose); ?></small></p>

3 个答案:

答案 0 :(得分:0)

在getNew()中的视图,不应该是索引的不同视图?例如:View::make('appointments.new')

View::make('appointments.index')个实例
public function getNew(){
    return View::make('appointments.new')
    ->with('title', 'Add A New Appointment');
}

修改

Form::text()中有sintax错误。你必须关闭所有Form::text()中的括号。如果此操作无法解决,请发布您的layout/default视图。

编辑2

不应该

Route::get('appointments/new',array('as'=>'newAppointment','uses'=>'AppointmentsController@getNew')    );

实例:

Route::get('appointments/new',array('as'=>'newAppointment','uses'=>'UsersController@getNew')    );

答案 1 :(得分:0)

迁移:发布

<?php

使用Illuminate \ Database \ Schema \ Blueprint; 使用Illuminate \ Database \ Migrations \ Migration;

类CreatePostsTable扩展了Migration {

/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::create('posts', function(Blueprint $table)
    {
        $table->increments('id');
        $table->string('title');
        $table->string('read_more');
        $table->string('content');
        $table->unsignedInteger('comment_count');
        $table->timestamps();
        $table->engine = 'MyISAM';
    });
    DB::statement('ALTER TABLE posts ADD FULLTEXT search(title, content)');
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
   Schema::table('posts', function(Blueprint $table){
       $table->dropIndex('search');
       $table->drop();
   });
}

} 控制器:

public function show($id)
{
    $post = Post::find($id)->toArray();
    return View::make('detail', compact('post'));
}

显示:detail.blade.php

@extends("layouts.master")

@section('content')
{{$post['id']}}
{{$post['title']}}
@stop

答案 2 :(得分:0)

价值回报是对象。您可以使用toArray()将对象返回到array()。当您获取值以显示包含{{$ post ['id']}}

等刀片模板的元素时