【问题标题】:Populating form with blade via controller通过控制器使用刀片填充表单
【发布时间】:2015-03-08 04:39:00
【问题描述】:

我有一个模型可以在我的数据库中获取所有“消息”:

public function edit($id)
    {
        $message = Message::find($id);

        return Redirect::back()
            ->with("message", $message);
    }

当我尝试dd($message) 时,我得到了我想要的。所以没有问题。 dd($message)的输出:

object(Message)#157 (20) { ["table":protected]=> string(11) "tblMessages" ["primaryKey":protected]=> string(10) "PK_message" ["connection":protected]=> NULL ["perPage":protected]=> int(15) ["incrementing"]=> bool(true) ["timestamps"]=> bool(true) ["attributes":protected]=> array(10) { ["PK_message"]=> int(31) ["FK_user"]=> int(1) ["title"]=> string(21) "Locaties hotel bekend" ["content"]=> string(90) "De locaties van de hotels zijn bekend. Team België: adres Team Frankrijk: adres ..." ["priority"]=> int(1) ["visible"]=> int(1) ["showFrom"]=> string(19) "2015-03-07 00:00:00" ["removeFrom"]=> string(19) "2028-03-07 00:00:00" ["created_at"]=> string(19) "2015-03-07 14:14:22" ["updated_at"]=> string(19) "2015-03-07 18:33:51" } ["original":protected]=> array(10) { ["PK_message"]=> int(31) ["FK_user"]=> int(1) ["title"]=> string(21) "Locaties hotel bekend" ["content"]=> string(90) "De locaties van de hotels zijn bekend. Team België: adres Team Frankrijk: adres ..." ["priority"]=> int(1) ["visible"]=> int(1) ["showFrom"]=> string(19) "2015-03-07 00:00:00" ["removeFrom"]=> string(19) "2028-03-07 00:00:00" ["created_at"]=> string(19) "2015-03-07 14:14:22" ["updated_at"]=> string(19) "2015-03-07 18:33:51" } ["relations":protected]=> array(0) { } ["hidden":protected]=> array(0) { } ["visible":protected]=> array(0) { } ["appends":protected]=> array(0) { } ["fillable":protected]=> array(0) { } ["guarded":protected]=> array(1) { [0]=> string(1) "*" } ["dates":protected]=> array(0) { } ["touches":protected]=> array(0) { } ["observables":protected]=> array(0) { } ["with":protected]=> array(0) { } ["morphClass":protected]=> NULL ["exists"]=> bool(true) }

我的问题: 如何使用刀片填充我的表单?

例如:

{{ Form::input('text', 'messageTitle', '', ['placeholder' => 'Title', 'id' => 'messageTitle', 'required' => 'required']) }} 

我知道第三个参数应该是值。但是如何测试上面的示例中是否设置了$message

消息模型:

<?php

use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;

class Message extends Eloquent implements UserInterface, RemindableInterface {

    use UserTrait, RemindableTrait;

    protected $table = 'tblMessages';
    protected $primaryKey = 'PK_message';

    public function teams()
    {
        return $this->belongsToMany('Team', 'tblMessages_tblTeams', 'FK_message', 'FK_team');
    }
}

我的看法:

{{ Form::open(array('action' => 'MessageController@store')) }}

    {{ Form::input('text', 'messageTitle', '', ['placeholder' => 'Title', 'id' => 'messageTitle', 'required' => 'required']) }}
    {{ Form::textarea('messageContent', '', array('required' => 'required', 'size' => '30x10', 'placeholder' => 'Message')) }}
    {{-- {{ Form::input('link', 'link', null, ['placeholder' => 'Link', 'id' => 'messageLink']) }} --}}

    {{ Form::input('tags', 'tags', '', ['placeholder' => "Tags (Seperate tags by a '-')", 'id' => 'messageTag']) }}

    Priority
    {{ Form::select('messagePriority', ["H" => "High", "R" => "Regular", "L" => "Low"], "R") }}
    Show from
    {{ Form::input('date', 'messageShowFrom', '', array('required' => 'required')) }}
    Hide from
    {{ Form::input('date', 'messageHideFrom', '', array('required' => 'required')) }}
    {{ Form::submit('Add') }}
{{ Form::close() }}

路线:

Route::get('/', array('as' => '/', function()
{
    return View::make('index');
}));

Route::resource('messages', 'MessageController');

商店:

public function store()
    {
        $date = new DateTime;
        $priority = Input::get("messagePriority");

        // CHECKING PRIORITY
        switch($priority)
        {
            case "R":
                $priority = 1;
                break;
            case "H":
                $priority = 0;
                break;
            case "L":
                $priority = 2;
                break;
        }

        // CHANGES INVISIBLE LINE BREAKS TO VISIBLE ONES
        $text = Input::get('messageContent');
        $messageContent = trim($text); 
        $messageContent = nl2br($messageContent); 

        // INSERT IN DATABASE
        $message = new Message;
        $message->FK_user = 1;
        $message->priority = $priority;
        $message->title = Input::get('messageTitle');
        $message->content = $messageContent;
        $message->visible = true;
        $message->showFrom = Input::get('messageShowFrom');
        $message->removeFrom = Input::get('messageHideFrom');
        $message->created_at = $date;
        $message->updated_at = $date;
        $message->save();

        $message->teams()->attach(array(1,2,3));

        return Redirect::back();
    }

【问题讨论】:

    标签: laravel laravel-4 blade


    【解决方案1】:

    请使用withInput() 而不是with()

    return Redirect::back()->withInput();
    

    和刀片:

    {{ Form::text('messageTitle', '', ['placeholder' => 'Title', 'id' => 'messageTitle', 'required' => 'required']) }}
    

    你应该很高兴。


    在 cmets 讨论后。

    首先;不要从编辑方法返回重定向而是返回视图

    public function edit($id)
    {
        $message = Message::findOrFail($id); //Find or fail, you don't need to check anything this throws 404 you can catch the exception manually if you like with try - catch syntax.
    
        return View::make('view-name-here-for-editin')->withMessage($message);
    }
    

    模型看起来不错;路线也是如此,除非您可以在顶部添加模式;

    Route::pattern('id', '\d+'); //id can be only numerical value
    

    编辑视图“优先级”,因此您无需在 store() 方法中检查此内容

    {{ Form::select('messagePriority', ["0" => "High", "1" => "Regular", "2" => "Low"], "1") }}
    

    存储方法

    //you are not validating you should!
    public function store()
    {
        //$date = new DateTime; no need and use carbon instead Carbon::now(); looks better ;)
        $priority = Input::get("messagePriority");
    
        // CHANGES INVISIBLE LINE BREAKS TO VISIBLE ONES
        $text = Input::get('messageContent');
        $messageContent = trim($text); 
        $messageContent = nl2br($messageContent); 
    
        // INSERT IN DATABASE
        $message = new Message;
        $message->FK_user = 1;
        $message->priority = $priority;
        $message->title = Input::get('messageTitle');
        $message->content = $messageContent;
        //$message->visible = true; //values that are default set in mysql itself (phpmyadmin set default for column)
        $message->showFrom = Input::get('messageShowFrom');
        $message->removeFrom = Input::get('messageHideFrom');
        //created_at and updated_at are set by laravel automatically
        $message->save();
    
        $message->teams()->attach(array(1,2,3));
    
        return Redirect::back();
    }
    

    回答您想使用的问题ternary operation

    var_dump(isset($message) ? $message : 'message is not set';)
    

    【讨论】:

    • withInput() 留空,不留任何内容。
    • 请包含更多代码,在此处复制您的整个模型和控制器(在两个箱子中)laravel.io/bin 并编辑原始帖子。
    • 以不同的方式称呼它-&gt;with('my_model_message', $message);,在您的视图中您有变量$my_model_message
    • 因为您发送的是一条消息,请检查单数与复数。
    • 请参阅“回答您的问题...”部分,这就是您检查变量是否已设置的方式。我很困惑什么是我需要休息的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-19
    • 2019-02-06
    • 1970-01-01
    • 1970-01-01
    • 2021-12-22
    • 2012-03-26
    相关资源
    最近更新 更多