phplaravellaravel-4laravel-blade

Populating form with blade via controller


I have a model which gets all 'messages' in my database:

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

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

When I try dd($message), I get what I want. So there is no problem. Output of 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) }

My question: How can I populate my form with it using blade?

For example:

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

I know the third parameter should be the value. But how can I test if $message is set inside the example above?

Message Model:

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

My view:

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

Routes:

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

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

Store:

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

Solution

  • Do use withInput() instead with().

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

    and blade:

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

    You should be good to go.


    After discussion in comments.

    First of all; don't return Redirect from edit method return View instead

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

    Model looks fine; routes too except you might add patterns on top;

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

    edit view "priority" so you don't need to check for this in store() method

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

    store method

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

    To answer your question tho you want to use ternary operation

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