【问题标题】:Eloquent storing form dataEloquent 存储表单数据
【发布时间】: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


    【解决方案1】:

    分析这些关系

    Oppgaver x OppgaverRutiner x Rutiner

    Oppgaver (hasMany) OppgaverRutiner and 
    Rutiner (hasMany)  OppgaverRutiner too
    

    反过来

    OppgaverRutiner (belongsTo) Oppgaver
    OppgaverRutiner (belongsTo) Rutiner
    

    安萨特 x Rutiner

    Ansatt (hasMany) Rutiner
    

    反过来

    Rutiner (belongsTo) Ansatt 
    

    Malinger x MalingerRutiner x Rutiner

    在表 MalingerRutiner 中缺少表 Rutiner 的外键!!!!!

    Malinger (hasMany) MalingerRutiner and
    Rutiner (hasMany) MalingerRutiner too
    

    逆向

    MalingerRutiner (belongsTo)  Malinger
    MalingerRutiner (belongsTo) Rutiner
    

    Basseng x MalingerRutiner

    Basseng (hasMany) MalingerRutiner
    

    逆向

    MalingerRutiner (belongsTo)  Basseng
    

    你的模型中有这个逻辑吗?

    【讨论】:

    • 嘿,是的,这将是我模型中的逻辑,MalingerRutiner 中 Rutiner 的外键是 id 行,这与 Rutiner 中的相同。
    • @user249494,一个好的做法是:Database normalizationThird normal form
    猜你喜欢
    • 2019-04-07
    • 2015-12-07
    • 2011-08-18
    • 1970-01-01
    • 1970-01-01
    • 2021-07-17
    • 2018-02-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多