なぜhasManyThroughは雄弁のドキュメントから動作しませんか?

代数:

エラーがここにある場合は実行できません「を通じて多くに」ドキュメントからの例?

countries
    id - integer
    name - string

users
    id - integer
    country_id - integer
    name - string

posts
    id - integer
    user_id - integer
    title - string

モデルの国

<?php

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

class Country extends Model
{
protected $fillable = [
      'id',  'name',
    ];
    //server error
   return $this->hasManyThrough(
            'App\Post', 'App\User',
           'country_id', 'user_id', 'id'

);

}

モデルユーザー がテーブル内の他の列がありますが、それらは、この例で使用されていません

<?php

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

class User extends Authenticatable
{
    protected $fillable = [
      'id',  'country_id', 'name'
    ];   

}

モデルのポスト

<?php

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

class Post extends Model
{
protected $fillable = [
      'id',  'user_id', 'title',
    ];

}

資源国

<?php

namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\JsonResource;

class Country extends JsonResource
{

    public function toArray($request)
    {
        return parent::toArray($request);

return [
      'id' => $this->id,
      'name' => $this->name,
      'title' => $this->title,

       ];

   }
}

コントローラの国

<?php

namespace App\Http\Controllers\Api;
use App\Country;
use App\Http\Resources\Country as CountryResource;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class CountryController extends Controller
{
public function index()
       {

    $countries = Country::with(['post'])->get();
    return CountryResource::collection($countries);
      }
}

ルータが正しく設定されている確認します

タスク:各国のショーテーブルIDでの3つの列(国)名(国)のタイトル(記事)-all

マニュアルは何かを説明しくしゃくしゃ

エデンダウリング・ミッチェル:

あなたはhasManyThroughメソッドに外部キーとカラム名を通過している場合は、指定する必要があり、両方の外部キーとの両方のローカルキーを。Laravelのドキュメントから:

return $this->hasManyThrough(
            'App\Post',
            'App\User',
            'country_id', // Foreign key on users table...
            'user_id', // Foreign key on posts table...
            'id', // Local key on countries table...
            'id' // Local key on users table...
        );

あなたの方法は、一度だけ「ID」を持っています。

また、この論理は、国のモデルの記事()という関数内にある必要があります。例えば:

Country.php

<?php

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

class Country extends Model
{
    protected $fillable = [
      'id',  'name',
    ];
    public function posts() {
           return $this->hasManyThrough(
            'App\Post', 'App\User',
           'country_id', 'user_id', 'id', 'id');
    }



}

次に、あなたの国コントローラーで:

<?php

namespace App\Http\Controllers\Api;
use App\Country;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class CountryController extends Controller
{
public function index()
       {

    $countries = Country::with('posts')->get();
    return $countries->toJson();
      }
}

私はCountryResourceの事を取り除くでしょう。それは(私は思う)やっているすべては、あなたがtoJsonに建て()メソッドで行うことができます、JSONにデータを変換しています。あなたはすべての列が返されたくない場合は、あなたがCountry.phpまたはPost.php編集して追加することができます(たとえば、多分あなたは示すタイムスタンプはいけない)protected $hidden = ['created_at','updated_at']上部になど。

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=15391&siteId=1