模型,数据库错误在Laravel [英] Model, Database Error in Laravel

查看:335
本文介绍了模型,数据库错误在Laravel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Laravel 4中建立一个应用程序,以帮助我学习和改进使用PHP,Blade和Laravel构建应用程序。应用程序的关键功能是消息传递,我正在尝试通过GitHub上提供的包添加到应用程序。该软件包是CMYGYR的Laravel-Messenger: - 通过主键查找模型或抛出异常)



如果这是唯一的错误得到,你的代码工作正常。你的数据库可能只是空的。使用数据库播放器填充行,并捕获代码中的错误:

  public function show($ id)
{
try {
$ thread = Thread :: findOrFail($ id);
} catch(\Illuminate\Database\Eloquent\ModelNotFoundException $ e){
//在这里抛出一些错误
return View :: make('messenger.invalid-thread' );
}
$ userId = Auth :: user() - > id;
$ users = User :: whereNotIn('id',$ thread-> participantUserIds($ userId)) - > get();
$ thread-> markAsRead($ userId);
return View :: make('messenger.show',compact('thread','users'));
}

在相关注释中,是否运行需要使用数据包的数据库迁移你已经上市了?尝试 php artisan migrate --package = cmgmyr / messenger ;但是如果您没有获得缺少表错误,我怀疑您已经做到了这一点。


I'm building an app in Laravel 4 to help me learn and improve at building apps with PHP, Blade, and Laravel. The app's key functionality is messaging, which I'm trying to add to the app with a package available on GitHub. The package is CMYGYR's Laravel-Messenger: https://github.com/cmgmyr/laravel-messenger. The problem I'm having is that I'm receiving an error related to a database query being made by one of the models.

I believe I have my Controller properly configured, because the __construct(), index, and create functions work fine. It's in the show function of my MessagesController where a findOrFail method is the start of the problem:

I believe I have the Thread model correctly configured to interact with it's corresponding table on the database:

But then I get this ModelNotFoundException error when the show function's Thread::findOrFail($id) statement queries the threads table:

However, when I use MySQL Workbench to look through my thread table's rows I can see the information the Controller's show function should be correctly pulling:

I appreciate any help on this issue.


So I've done some additional work on my project trying to figure out the issues, and after some testing I've found that the issue definitely is not that the entries in my database don't exist. It's certainly that the Model (in this case Thread) isn't being found for whatever reason.

So the findOrFail statement in the show function of my MessagesController was the main culprit when I started testing. To check out how my app was responding to the changes, I observed what happened in my browser and in a development console that I hooked up to my app for these sort of issues. I tried 3 separate solutions which I'll explain and provide screenshots for. Warning: this is not for the faint of heart.

1) $thread = DB::select('select * from threads where id = id', []);

The problem with this statement (which I wrote to replace $thread = Thread::findOrFail($id);) is it returns an array, which can't call the Thread model's function. Despite executing queries on the database correctly, it can't be used to call the participantsUserIds function that's in the Thread model.

2) $thread = Thread::find($id);

The problem with this find statement is that it returns null, because the problem is with the model not being found.

2) $thread = DB::table('threads')->where('id', $id)->first();

This statement returns a stdClass object, which (despite being an object with all the correct data in it) can't call any of the Thread model's methods because it's not a Thread model itself.

If you've read this far you're an angel. As always any help from the S.O. community is greatly appreciated .

解决方案

Your issue isn't that the autoloader hasn't been updated, but that you're querying for an entry in your database that doesn't exist.

You're using the Eloquent::findOrFail() method. If a Thread with your specified $id isn't found in the database, findOrFail() throws the ModelNotFoundException (see: http://laravel.com/api/4.2/Illuminate/Database/Eloquent/Builder.html#method_findOrFail - "Find a model by its primary key or throw an exception.")

If that's the only error you're getting, your code is working fine. Your database is probably just empty. Use a database seeder to populate the rows, and catch the error in your code:

public function show($id) 
{
    try {
        $thread = Thread::findOrFail($id);
    } catch(\Illuminate\Database\Eloquent\ModelNotFoundException $e) {
        // throw some error here
        return View::make('messenger.invalid-thread');
    }
    $userId = Auth::user()->id;  
    $users = User::whereNotIn('id', $thread->participantsUserIds($userId))->get();  
    $thread->markAsRead($userId);  
    return View::make('messenger.show', compact('thread', 'users'));  
}

On a related note, have you run the database migrations necessary to use the package you've listed? Try php artisan migrate --package=cmgmyr/messenger; but I suspect you've already done this if you're not getting a missing table error.

这篇关于模型,数据库错误在Laravel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆