I've created a page to edit a message, I can edit my message and save in my database. After the save, I return View::make("messages.index");
But if I look at the URL, it is: http://localhost:8000/update/42 instead of http://localhost:8000/messages
Did I do something wrong? (I assume I made a mistake in my routes.php, but I can't find it)
Routes:
Route::get('/', array('as' => '/', function()
{
return View::make('index');
}));
Route::get("Messages", function()
{
return View::make("Messages.index");
});
Route::resource('messages', 'MessageController');
Route::post('messages/{messages}', 'MessageController@destroy');
Route::put('update/{messages}', array('as' => 'putMessage', 'uses' => 'MessageController@update'));
Route::get('messages/{messages}/edit', array('as' => 'editMessage', 'uses' => 'MessageController@edit'));
Route::post('messages', array('as' => 'storeMessage', 'uses' => 'MessageController@store'));
MessageController:
<?php
class MessageController extends \BaseController {
public function index()
{
$messages = Message::join('tblMessages_tblTeams', 'tblMessages.PK_message', '=', 'tblMessages_tblTeams.FK_message')
->join('tblTeams', 'tblMessages_tblTeams.FK_team', '=', 'tblTeams.PK_team')
->where('PK_team', 1)
->orderBy('created_at', 'DESC')
->get();
return View::make("messages.index")->withMessages($messages);
}
public function create()
{
//
}
// HARD CODED WILL BE REPLACED WHEN ABLE TO LOG IN
// FK_user => Auth::id()->FK_team
public function store()
{
$date = new DateTime;
// INSERT IN DATABASE
$message = new Message;
$message->FK_user = 1;
$message->priority = Input::get("messagePriority");
$message->title = Input::get('messageTitle');
$message->content = Input::get('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,3));
return Redirect::back();
}
public function show($id)
{
$deleteMessage = Message::findOrFail($id);
$deleteMessage->visible = 0;
$deleteMessage->save();
return Redirect::back();
}
public function edit($id)
{
$message = Message::findOrFail($id);
$showFrom = $message->showFrom;
$removeFrom = $message->removeFrom;
$showFrom = str_replace(' 00:00:00', '', $showFrom);
$removeFrom = str_replace(' 00:00:00', '', $removeFrom);
return View::make('messages.index')
->withMessage($message)
->with("showFrom", $showFrom)
->with("removeFrom", $removeFrom);
}
public function update($id)
{
$date = new DateTime;
$editMessage = Message::findOrFail($id);
$editMessage->FK_user = 1;
$editMessage->priority = Input::get("messagePriority");
$editMessage->title = Input::get('messageTitle');
$editMessage->content = Input::get('messageContent');
$editMessage->visible = true;
$editMessage->showFrom = Input::get('messageShowFrom');
$editMessage->removeFrom = Input::get('messageHideFrom');
$editMessage->updated_at = $date;
$editMessage->save();
return View::make("messages.index");
}
public function delete($id)
{
//
}
public function destroy($id)
{
dd('destroy');
}
}
That is the way it works because your form has been submitted to update/42
.
You can return by redirecting like below to have the URL you want the user to be at.
return Redirect::to('/Messages');
and if you want to carry any values to that view you can use
->withInput();
Also refer this for more information