我正在尝试从工作站存储库中复制一个工作站并将其添加到我最喜欢的存储库中。我在laravel rest API中。谢谢你的帮助!
这是我的控制器:
class FavoriteController extends Controller
{
private $favoriteRepository;
private $stationRepository;
public function __construct(FavoriteRepository $favoriteRepository, StationRepository $stationRepository)
{
$this->favoriteRepository = $favoriteRepository;
$this->stationRepository = $stationRepository;
}
public function store(int $station_id)
{
$favorite = array();
$favorite[] = $this->stationRepository->findByField("id", $station_id);
$this->favoriteRepository->create($favorite);
return response()->json($favorite, 201);
}
}
以下是收藏夹的数据库:
public function up()
{
Schema::create('favorites', function (Blueprint $table) {
$table->string('name');
$table->string('city');
$table->foreign('city')->references('name')->on('cities');
$table->integer('station_id')->unsigned();
$table->foreign('station_id')->references('id')->on('stations')->onDelete('cascade');
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
#$table->boolean('is_private');
});
}
这是我最喜欢的模特
class Favorite extends Model
{
protected $fillable = ['station_id', 'user_id', 'updated_at', 'name', 'city'];
public $timestamps = false;
}
我的回购中有这两种方法:
function model()
{
return "App\\Station";
}
答案 0 :(得分:0)
试试这个
public function store($station_id)
{
$favorite = $this->stationRepository->where("id", $station_id)->first()->toArray();
$this->favoriteRepository->create($favorite);
return response()->json($favorite, 201);
}
}