Laravel rest api,获取与上传/发布中存储的数据库中的数据相关联的图像的完整链接
我正在从事Laravel项目,遇到了一个问题。我希望我的ApiController.php上的函数能够为我提供POST表中图像的完整链接,存储在uploads/posts中。 所以我尝试了这个方法,我怎样才能得到这个结果呢Laravel rest api,获取与上传/发布中存储的数据库中的数据相关联的图像的完整链接,laravel,rest,eloquent,laravel-7,laravel-response,Laravel,Rest,Eloquent,Laravel 7,Laravel Response,我正在从事Laravel项目,遇到了一个问题。我希望我的ApiController.php上的函数能够为我提供POST表中图像的完整链接,存储在uploads/posts中。 所以我尝试了这个方法,我怎样才能得到这个结果呢 //ApiController <?php namespace App\Http\Controllers; use Illuminate\Support\Arr; use Illuminate\Http\Request; use
//ApiController
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Arr;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use Carbon\Carbon;
use App\Category;
use App\Post;
class ApiController extends Controller
{
//
public function getposts(){
$post = Post::select('post_title','post_content','category_id','image')
->with('category')
->get();
$categories=Category::all();
return response()->json($post, 200, [], JSON_UNESCAPED_UNICODE);
}
}
//ApiController
您可以简单地访问所有项目,如下所示:
foreach ($post as $p) {
$p->image = url($p->image);
}
或更优雅的方式-您可以直接在查询中提取图像数据:
Post::select('post_title','post_content','category_id', \DB::raw('CONCAT("'.url('/').'/", image) as image'))->get();
这是我的工作,非常感谢@Alex Post::select('Post_title','Post_content','category_id',\DB::raw('CONCAT('.url('/')。/“,image)as image'))->get();
foreach ($post as $p) {
$p->image = url($p->image);
}
Post::select('post_title','post_content','category_id', \DB::raw('CONCAT("'.url('/').'/", image) as image'))->get();