duanbo2048 2016-04-22 20:39
浏览 7
已采纳

加载控制器内的其他型号

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?

  • 写回答

2条回答 默认 最新

  • duandai2178 2016-04-22 20:44
    关注

    This happens a lot due to namespacing added in Laravel 5+.

    You have a few options to remedy this. First, a use statement:

    use App\Ticket;
    

    Including this in your class declaration for TicketsController will allows Ticket::findOrFail() or any other Ticket:: function to be called without issue.

    As an added note, check the namespace in yout Ticket.php file. By default, a model's namespace should be:

    namespace App;
    

    Alternatively, if you only want say a single use in a different controller, you can specify the namespace before the function call. For example:

    $ticket = \App\Ticket::where("id", "=", $ticketId)->first();
    

    The issue is that with namespacing, php is trying to find a Ticket class in App\Http\Controllers, which (logically) there shouldn't be.

    Hope this helps!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题