【发布时间】:2014-04-25 23:53:13
【问题描述】:
我目前正在使用 laravel 4 开发一个 Web 应用程序。该应用程序的本质是将表单数据存储在数据库中。
由于表单输入框中的名称不同,我的主要问题是在例程表中存储不同的值,因为我只有一个值字段并通过 id 将该值与指定的任务链接在一起。我还没有找到一种方法可以在例程中有效地为每个字段存储一行。
数据库看起来像这样(忽略 Arduino 和 Endringslogg):
HverTimeController.php:
class HverTimeController extends BaseController
{
public $restful = true;
public function getHvertime()
{
return View::make('hvertime')
->with('title', 'Hvert Time');
}
public function postInsert()
{
//insert logic
return Redirect::route('hvertime')
->with('message', 'Lagret i databasen!');
}
}
routes.php
Route::get('bassengweb/hvertime', array('as' => 'hvertime', 'uses' => HverTimeController@getHvertime'));
Route::post('bassengweb/insertHT', array('uses' => 'HverTimeController@postInsert'));
雄辩的模型 (尽管上图中的名称是挪威语,但我已经在数据库中将它们更改为英语,如下面的代码所示)
Emp.php(图中的 Ansatt)
class Emp extends Eloquent
{
protected $table = 'emps';
protected $fillable = array(
'user_name', 'first_name',
'last_name', 'email',
'password', 'user_type');
public function routines()
{
$this->hasMany('Routine', 'emp_id', 'id');
}
}
Routine.php(图中的Rutiner)
class Routine extends Eloquent
{
protected $table = 'routines';
protected $fillable = array('date', 'time', 'value', 'emp_id');
public function emps()
{
$this->belongsTo('Emp', 'emp_id', 'id');
}
public function tasks()
{
$this->belongsToMany('Task', 'task_routine', 'routine_id', 'task_id');
}
public function measurements()
{
$this->belongsToMany('Measurement', 'measure_routine', 'routine_id', 'measure_id');
}
}
Measurement.php(图中的 Målinger 表)
class Measurement extends Eloquent
{
protected $table = 'measurements';
protected $fillable = array('title');
public function routines()
{
$this->belongsToMany('Routine', 'measure_routine');
}
}
Task.php(图中的Oppgaver表)
class Task extends Eloquent
{
protected $table = 'tasks';
protected $fillable = array('title');
public function routines()
{
$this->belongsToMany('Routine', 'task_routine', 'task_id', 'routine_id');
}
}
hvertime.blade.php
@extends('layouts.default')
@section('content')
<h1>HVER TIME</h1>
@if($errors->has())
<ul>
{{ $errors->first('badendeTime', '<li>:message</li>') }}
{{ $errors->first('temp', '<li>:message</li>') }}
</ul>
@endif
{{ Form::open(array('url' => 'bassengweb/insertHT', 'method' => 'POST')) }}
<table>
<tr>
<td>{{ Form::label('badendeTime', 'Badende per Time:') }}</td>
<td>{{ Form::text('badendeTime', Input::old('badendeTime')) }}</td>
</tr>
<tr>
<td>{{ Form::label('temp', 'Temperatur:') }}</td>
<td>{{ Form::text('temp', Input::old('temp')) }}</td>
</tr>
<tr>
<td><hr/></td>
<td>{{ Form::submit('Lagre') }}</td>
</tr>
</table>
{{ Form::close() }}
@stop
非常感谢任何帮助,谢谢!
【问题讨论】:
标签: php mysql laravel laravel-4 eloquent