从 Laravel 中的 json 响应中删除 ip

Remove ip from json response in Laravel

我在 Laravel 休息 api,如下所示:

{
    "id": 17,
    "title": "Devnet",
    "slug": "devnet",
    "content": "sfdf",
    "technology_id": 1,
    "image": "http://127.0.0.1:8000/uploads/posts/1570907475IMG_20171229_123822.jpg|uploads/posts/1570907475IMG_20171229_133927.jpg|uploads/posts/1570907475IMG_20180319_124721.jpg",
    "link": "https://www.somelink.com/in/test/",
    "deleted_at": null,
}

我需要从图像中的所有 responses.For 示例中删除本地 ip (127.0.0.1:8000) 地址我有 3 个文件,但在我提供的代码中你可以看到 3 个链接,其中只有一个有完整路径。

实际上在数据库中,他们没有像这样 uploads/posts/image_name.jpg 这样没有本地 ip 的完整 path.All 发布到数据库。在模型创建中,我检查了 dd 我所有的图片都没有本地 ip (127.0.0.1:8000)。只有 uploads/posts/image_name.jpg.

我如何将数据存储到控制器中的数据库:

    $images = array();
    if ($files = $request->file('image')) {
        foreach ($files as $file) {
            $name =  "uploads/posts/" . time() . $file->getClientOriginalName();
            $file->move("uploads/posts", $name);
            $images[] = $name;
        }
    }

    // validating in here ..
    if I dd($images) in here it show me 3 array of images without local ip.

    $project= Project::create([
        "title" => $request->title,
        "content" => $request->content,
        'image' =>  implode("|", $images),
        "technology_id" => $request->technology_id,
        "slug" => str_slug($request->title),
        "tags" => "required",
        "link" => $request->link
    ]);

也是我的主要 Controller 其中 returns json 没什么特别的。

public function index(){
    $result = Project::with('something','something')->get();
    return response()->json($result);
}

项目模型

class Project extends Model
{
    use SoftDeletes;

    protected $fillable = [
        "title","content","image","technology_id","link","slug"
    ];

    public function getImageAttribute($image){
        return asset($image);
    }
    protected $dates = ["deleted_at"];
}

这个accessor function是你的罪魁祸首:

public function getImageAttribute($image){
    return asset($image);
}

asset 函数 returns an absolute URL,而且它不知道如何处理内爆的图像数组。