我收到此錯誤信息,如果我嘗試在郵遞員跑我的帖子:Laravel BadMethodCallExeption查詢生成器發佈
BadMethodCallException
調用未定義的方法照亮\數據庫\查詢\生成器::文章()
我的路線是這樣的:
Route::middleware('auth:api')->group(function()
{
Route::get('posts', ['as' => 'posts', 'uses' => 'Api\[email protected]']);
}
和控制器這樣的:
class PostController extends Controller
{
public function index()
{
$posts = Auth::client()->posts()->get();
dd($posts);
return response()->json(['data' => $posts], 200, [], JSON_NUMERIC_CHECK);
}
}
我的客戶模型:
class Client extends Model implements AuthenticatableContract,
AuthorizableContract,
CanResetPasswordContract
{
use Authenticatable, Authorizable, CanResetPassword, HasApiTokens, Notifiable;
protected $table = 'clients';
protected $fillable = ['name', 'email', 'password'];
protected $hidden = ['password', 'remember_token'];
public function posts()
{
return $this->hasMany(Post::class);
}
}
我的Post模型:
namespace App;
use App\Client;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
public function client()
{
return $this->belongsTo(Client::class);
}
}
如果我傾倒這樣的:$posts = Auth::user()->get();
我得到一個輸出,但我想獲得後輸出。
您是否定義了用戶與發佈模型之間的關係? –