通过控制器使用 blade 填充表单

Populating form with blade via controller

我有一个模型可以获取我数据库中的所有 'messages':

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) }

我的问题: 如何使用 blade?

用它填充我的表单

例如:

{{ 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();
    }

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

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

和blade:

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

你应该可以开始了。


经过评论讨论。

首先;不要 return 从编辑方法重定向 return 改为查看

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';)