Laravel:类型错误:参数1传递给boolen给出

时间:2018-02-04 01:35:05

标签: php laravel laravel-5.5 php-7.1

首次深入了解Laravel并首先使用API​​部分。

另请注意,我使用Cyvelnet/Laravel5Fractal包以及Laravel/Passport

每次我调用API的/announcements部分,特别是一个对象(我希望你称之为api/announcements/1)时,我会一直收到这个jiggy错误:

Type error: Argument 1 passed to Flarify\Transformers\AnnouncementTransformer::transform() 
must be an instance of
Flarify\Announcement, boolean given, called in C:\wamp64\www\flarify.dev
\vendor\league\fractal\src\Scope.php on line 407

具体指向我的AnnouncementTransformer类中的这一行/函数:

public function transform(Announcement $announcement)

这是我的AnnouncementTransformer类:

namespace Flarify\Transformers;

use Flarify\Announcement;
use League\Fractal;
use League\Fractal\TransformerAbstract;
use League\Fractal\Resource\Collection;
use League\Fractal\Resource\Item;


class AnnouncementTransformer extends TransformerAbstract
{
    /**
     * List of resources possible to include
     *
     * @var array
     */
    protected $availableIncludes = [];

    /**
     * List of resources to automatically include
     *
     * @var array
     */
    protected $defaultIncludes = [];

    /**
     * Transform object into a generic array
     *
     * @var $resource
     * @return array
     */
    public function transform(Announcement $announcement)
    {
        return [

            'id' => $announcement->id,
            'user_id' => $announcement->user_id,
            'title' => $announcement->title,
            'message' => $announcement->message,
            'created_at' => $announcement->created_at->format('d M Y'),
            'updated_at' => $announcement->updated_at->format('d M Y'),

        ];
    }

    public function includeCreator(Announcement $announcement)
    {
        return $this->item($announcement->user_id, new UserTransformer());
    }
}

我的宣传模式:

namespace Flarify;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;


/**
 * Class Announcement
 * @package Flarify
 *
 * @property int id
 * @property int user_id
 * @property string title
 * @property string message
 * @property Carbon created_at
 * @property Carbon updated_at
 *
 */
class Announcement extends Model
{
    protected $table = 'announcements';

    public function announcementCreator()
    {
        return $this->belongsTo(User::class, 'user_id', 'id');
    }
}

还有我的AnnouncementController以防万一:

namespace Flarify\Http\Controllers;

use Cyvelnet\Laravel5Fractal\Facades\Fractal;
use Flarify\Http\Requests\Anouncements\StoreAnnouncementRequest;
use Flarify\Http\Requests\Anouncements\UpdateAnnouncementRequest;
use Flarify\Transformers\AnnouncementTransformer;
use Flarify\Announcement;
use Illuminate\Http\Request;
use Flarify\Http\Controllers\Controller;

class AnnouncementController extends Controller
{
    public function index()
    {
        $announcement = Announcement::all();

        return Fractal::includes('user')->collection($announcement, new AnnouncementTransformer());
    }

    public function store(StoreAnnouncementRequest $request)
    {

        $announcement = new Announcement;
        $announcement->user_id = 1;
        $announcement->title = $request->title;
        $announcement->message = $request->messages;

        $announcement->save();

        return Fractal::includes('user')->collection($announcement, new AnnouncementTransformer());
    }

    public function show(Announcement $announcement)
    {


        return Fractal::includes('user')->collection($announcement, new AnnouncementTransformer());
    }

    public function update(UpdateAnnouncementRequest $request, Announcement $announcement)
    {
        $announcement->title = $request->get('title', $announcement->title);
        $announcement->description = $request->get('description', $announcement->description);

        $announcement->save();

        return Fractal::includes('user')->collection($announcement, new AnnouncementTransformer());
    }

    public function destroy(Announcement $announcement)
    {
        $announcement->delete();

        return response(null, 200);
    }
}

同样,这是我的第一个Laravel项目,我将我在PHP OOP中学到的东西一次性完成(MVC)。

我做错了什么?我错过了什么吗?

另外,这是我的第一篇文章! (哇噢?)

0 个答案:

没有答案