I'm trying to experiment and do my first steps with Laravel 5 implementing a basic help desk app, whose main models are Ticket and Message.
I first defined the endpoint for the addition of a new message in an existing ticket with this route:
Route::post('tickets', 'TicketsController@store');
And the store() method within TicketsController:
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$messageTxt = $request->get('message');
$ticket = Ticket::findOrFail($request->get('ticket_id'));
$message = new Message();
$message->text = $messageTxt;
$ticket->messages()->save($message);
return Redirect::action('TicketsController@show', $request->get('ticket_id'));
}
Note that both models have a one to many relationship defined through the messages() method in this case. This was working without any problem. But I then realized this resource method would be more appropriate to create a brand new ticket, not a ticket message.
So when someone sends the form to create a new ticket message, this is the route I defined instead:
Route::post('messages', 'MessagesController@store');
Next I moved all the code above within TicketsController::store() to MessagesController::store(), but then I got this error:
FatalErrorException in MessagesController.php line 40: Class 'App\Http\Controllers\Ticket' not found
Line 40 corresponds to the request for the ticket with the ticket id. It looks like it has something to do with the Ticket model not being loaded here because it doesn't belong in this controller by convention, and it perfectly worked within TicketsController. I'm not of this sure though.
What should I do then to be able to use the Ticket model within the MessagesController?