awaistoor awaistoor - 1 month ago 17
reST (reStructuredText) Question

Laravel Eloquent relationship not working in restful api

I have a simple one to many relationship in eloquent database with two tables. They are working fine in my normal controllers. But when I use them in my Restful controller, it shows me error of Class 'App/TVChannel' not found. Everything is working fine in other controllers. Here are the model classes:

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');
}}


Here is the code from my normal controller where i am using ajax request to load more dramas

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'
]);
}
}


And here is that function where i am getting that error:

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');
}

}


Please tell me where i am going wrong or what more should i do.
Thanks.

Answer

You have a typo in Drama.php file TvChannel should be TVChannel, since the model name is TVChannel. change

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

to

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

The previous code may work on Windows but won't work on Linux since Linux is case sensitive.

Comments