Laravel Eloquent的关系不适用于宁静的api

时间:2016-10-17 10:34:23

标签: php rest laravel eloquent laravel-5.2

我在两个表的雄辩数据库中有一个简单的一对多关系。它们在我的普通控制器中工作正常。但是当我在Restful控制器中使用它们时,它会向我显示Class' App / TVChannel'未找到。在其他控制器中一切正常。以下是模型类:

TVChannel.php

namespace App;
use Illuminate\Database\Eloquent\Model;

class TVChannel extends Model {

protected $table = 'tv_channel';
protected $fillable = ['name', 'img', 'hex_color', 'total_views', 'user_id', 'created_ip'];

public function user() {
    return $this->belongsTo('App\User', 'user_id');
}

public function dramas() {
    return $this->hasMany('App\Dramas', 'channel_id');
}
}

Dramas.php

namespace App;

use Illuminate\Database\Eloquent\Model;
class Dramas extends Model {

protected $table = 'dramas';
protected $fillable = ['name', 'img', 'total_views', 'channel_id', 'user_id', 'created_ip'];

public function user() {
    return $this->belongsTo('App\User','user_id');
}

public function channel() {
    return $this->belongsTo('App\TvChannel', 'channel_id');
}

public function episodes(){
    return $this->hasMany('App\Episodes', 'drama_id');
}}

这是来自我的普通控制器的代码,我使用ajax请求来加载更多的剧集

 public function loadMoreDrama(Request $request) {
    $all_dramas = Dramas::latest()->limit(10)->offset($request->input('offset'))->get();
    if (count($all_dramas) > 0) {
        $ajaxString = '';
        foreach ($all_dramas as $drama) {
            $ajaxString .= '<li class="collection-item avatar" id="delete-drama-' . $drama->id . '">
                                <img src="' . asset($drama->img) . '" class="circle">
                                <span class="title" id="d-title-' . $drama->id . '">' . $drama->name . '</span>
                                <p id="d-forselect-' . $drama->id . '" selectid="' . $drama->channel->id . '">' . $drama->channel->name . '</p>
                                <a href="#" onClick="myddropdown(' . $drama->id . ')" "id="d_dropdown_menu_' . $drama->id . '" data-activates="d_dropdown_' . $drama->id . '" data-alignment="right" class="custom-dropdown waves-effect waves-circle red-text dropdown-button secondary-content"><i class="material-icons">more_vert</i></a>
                                <ul id="d_dropdown_' . $drama->id . '" class="dropdown-content">
                                    <li><a href="#" class="grey-text text-darken-4" onClick="updateDrama(' . $drama->id . ', \'' . $drama->img . '\')">Update</a></li>
                                    <li class="divider"></li>
                                    <li><a href="#" class="grey-text text-darken-4" onClick="deleteDrama(' . $drama->id . ', \'' . $drama->img . '\')">Delete</a></li>
                                </ul>
                            </li>';
        }
        return Response::json([
                    'success' => true,
                    'msg' => $ajaxString
        ]);
    } else {
        return Response::json([
                    'success' => false,
                    'msg' => 'No Drama to show'
        ]);
    }
}

这是我得到错误的函数:

public function getLatestDramas(Request $request)
{
    $drama = Dramas::latest()
        ->limit(10)
        ->offset($request->input('offset'))
        ->get();
    if ($drama->count() > 0) {
            $channels = array();
        foreach($dramas as $d){
            $channels[]= $d->channel;
        }
        return $this->export(TRUE, $channels);
    } else {
        return $this->export(FALSE, 'no drama to show right now');
    }

}

请告诉我哪里出错了或者我该怎么做。 感谢。

1 个答案:

答案 0 :(得分:1)

您在Drama.php文件中有拼写错误TvChannel应该是TVChannel,因为型号名称是TVChannel。 变化

public function channel() { return $this->belongsTo('App\TvChannel', 'channel_id'); }

public function channel() { return $this->belongsTo('App\TVChannel', 'channel_id'); }

以前的代码可能适用于Windows,但由于Linux区分大小写,因此无法在Linux上运行。