0

我在尝试理解 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>
4

3 回答 3

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(). 你必须关闭所有你的括号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')    );

?

于 2013-07-04T23:21:20.537 回答
0

返回值是 object 。您可以使用 toArray() 将对象返回到 array() 。当您获取值以显示带有刀片模板的元素时,例如 {{$post['id']}}

于 2014-04-04T03:18:25.607 回答
0

迁移:发布

<?php

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

类 CreatePostsTable 扩展迁移 {

/**
 * 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
于 2014-04-04T03:15:10.743 回答