我有一个模型可以获取数据库中的所有“消息”:
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();
}
请使用
withInput()
代替 with()
。
return Redirect::back()->withInput();
和刀片:
{{ Form::text('messageTitle', '', ['placeholder' => 'Title', 'id' => 'messageTitle', 'required' => 'required']) }}
你应该可以走了。
首先;不要从编辑方法返回重定向,而是返回视图
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();
}
回答你的问题,你想使用三元运算
var_dump(isset($message) ? $message : 'message is not set';)