You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel 8升级后Embed包Embed::create()方法未定义问题求助

问题解决:Embed v4.x 移除 create() 方法后的替代方案

Embed v4.x 版本确实移除了静态方法 Embed::create(),这是官方API的迭代变更。你需要改用实例化类并调用get()方法的方式来获取链接元信息,具体修改步骤如下:

代码修改

将原代码中的:

$info = Embed::create($url);

替换为:

// 若已导入命名空间 use Embed\Embed; 可直接实例化
$embed = new Embed();
$info = $embed->get($url);

完整修改后的代码片段

try {
    // 替换原create方法为实例化后调用get
    $embed = new Embed();
    $info = $embed->get($url);

    $qr_code->user_id = auth()->user()->id;
    $qr_code->redirect_to_url = $url;
    $qr_code->title = $info->title;
    $qr_code->description = $info->description;
    $image = ($info->image !== null) ? $info->image: \Czim\Paperclip\Attachment\Attachment::NULL_ATTACHMENT;
    $qr_code->image = $image;
    $qr_code->icon = $info->providerIcon;

    $qr_code->save();

    if ($sl == null) {
        $qr_code->qr = new \SplFileInfo(public_path(\DNS2D::getBarcodePNGPath($qr_code->getUrl(), 'QRCODE', 70, 70, [0, 0, 0])));
        $qr_code->save();
    }

    return redirect('dashboard/qr')->with('message', $msg);

} catch (\ErrorException $e) {
    $validator->getMessageBag()->add('redirect_to_url', $e->getMessage());
} catch (\Embed\Exceptions\InvalidUrlException $e) {
    $validator->getMessageBag()->add('redirect_to_url', $e->getMessage());
}

补充说明

  • 原代码中获取元信息的属性($info->title、$info->description等)无需修改,返回的$info对象结构与v3版本保持一致。
  • 异常捕获逻辑也不需要调整,InvalidUrlException等原有异常类仍可正常使用。

内容的提问来源于stack exchange,提问作者Lucas MTL

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 10:04:54