我有一个模型可以获取所有消息'在我的数据库中:
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();
}
答案 0 :(得分:0)
请使用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
编辑视图&#34;优先级&#34;所以你不需要在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';)