I'm new to Laravel and I've been pulling my hair out for the last couple of hours. I have an existing website which I'm trying to convert over to Laravel, the first step is building an API for it.
I have a Campaign model, and each Campaign has Posts. So there should be a OneToMany relationship between them.
Here is my code so far:
Campaigns.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
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
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
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.
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...
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');
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.