[英]Laravel HasMany not returning any results - Doesn't even query?
I'm new to Laravel and I've been pulling my hair out for the last couple of hours.我是 Laravel 的新手,过去几个小时我一直在拔头发。 I have an existing website which I'm trying to convert over to Laravel, the first step is building an API for it.
我有一个现有的网站,我正在尝试将其转换为 Laravel,第一步是为其构建一个 API。
I have a Campaign model, and each Campaign has Posts.我有一个活动 model,每个活动都有帖子。 So there should be a OneToMany relationship between them.
所以它们之间应该存在 OneToMany 关系。
Here is my code so far:到目前为止,这是我的代码:
Campaigns.php活动.php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Campaigns extends Model
{
// Define the table name for this model
protected $table = 'campaign_accounts';
protected $primaryKey = 'id';
}
CampaignPosts.php竞选帖子.php
namespace App;
use Illuminate\Database\Eloquent\Model;
class CampaignPosts extends Model
{
// Define the table name for this model
protected $table = 'campaign_posts';
protected $primaryKey = 'id';
}
CampaignsController.php CampaignsController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DB;
use App\CampaignPosts;
DB::enableQueryLog();
class CampaignsController extends Controller
{
public function index()
{
return \App\Campaigns::all();
}
public function show($id)
{
$campaigns = \App\Campaigns::find($id);
$campaignposts = $campaigns->campaignPosts;
dd(
$campaignposts
);
return array(
'status' => 'OK',
'data' => array(
'campaign' =>$campaigns,
'posts' => $campaignposts
)
);
}
// Define relationship to CampaignPost
public function campaignPosts()
{
// First key is foreign key, second key is local key
return $this->hasMany('App\CampaignPosts', 'accountid', 'accountid');
}
}
CampaignPostsController.php CampaignPostsController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Campaigns;
class CampaignPostsController extends Controller
{
// Define relationship to CampaignPost
public function campaigns()
{
// First key is foreign key, second key is local key
return $this->belongsTo('App\Campaigns', 'accountid', 'accountid');
}
}
When I navigate to /campaigns/ all the campaigns display fine, however when I navigate to campaigns/{id} I only get the result for the campaign itself, I can't get anything from the underlying Campaign Posts.当我导航到 /campaigns/ 时,所有广告系列都显示正常,但是当我导航到campaigns/{id} 时,我只能获得广告系列本身的结果,我无法从底层的广告系列帖子中获得任何信息。
I tried to use the DB Query Logger, and it looks like there isn't even a query being performed against the campaign_posts table, nothing is happening...我尝试使用 DB Query Logger,看起来甚至没有针对campaign_posts 表执行查询,什么都没有发生……
Any thoughts?有什么想法吗? This is driving me insane.
这让我发疯。
Thanks!谢谢!
relation methods should move to self models.关系方法应该转移到自我模型。
namespace App;
use Illuminate\Database\Eloquent\Model;
class Campaigns extends Model
{
// Define the table name for this model
protected $table = 'campaign_accounts';
protected $primaryKey = 'id';
// Define relationship to CampaignPost
public function campaignPosts()
{
// First key is foreign key, second key is local key
return $this->hasMany('App\CampaignPosts', 'accountid', 'accountid');
}
}
and和
namespace App;
use Illuminate\Database\Eloquent\Model;
class CampaignPosts extends Model
{
// Define the table name for this model
protected $table = 'campaign_posts';
protected $primaryKey = 'id';
// Define relationship to CampaignPost
public function campaigns()
{
// First key is foreign key, second key is local key
return $this->belongsTo('App\Campaigns', 'accountid', 'accountid');
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.