2017-09-15 89 views
0

我有一个Laravel项目,我创建了两个表格(产品和类别),并通过一对多关系将它们相互关联。一个类别有很多产品。我创建了一个名为ProductsController的CRUD控制器,并将图像存储在Laravel应用程序中名为images in public的文件夹中。在Laravel中检索特定类别的产品5.4

当我尝试检索特定类别的存储图像并在视图中显示时,问题就开始了。 请协助。

分类模型

class Category extends Model 
{ 
    protected $fillable = ['name']; 

    public function products(){ 
     return $this->hasMany('App\Product'); 
    } 

} 

产品型号

class Product extends Model 
{ 
    protected $fillable = ['name', 'desctiption' , 'size', 'category_id']; 

    public function category() 
    { 
     return $this->belongsTo(Category::class); 
    } 


} 

的ProductsController

public function store(Request $request) 
    { 

     $formInput=$request->except('image'); 

     //validation 
     $this->validate($request,[ 
      'name'=>'required', 
      'size'=>'required', 
      'description' => 'required|min:12', 
      'price'=>'required', 
      'category_id' => 'required', 
      'image'=>'image|mimes:png,jpg,jpeg,gif|max:100' 
     ]); 

     //Instantiate a new Project called Product 
     $product = new Product; 

     //Add the products to the Object 
     $product->description = $request->description; 
     $product->name = $request->name; 
     $product->price = $request->price; 
     $product->category_id = $request->category_id; 
     $product->size = $request->size; 

     //Save all the items to the database 
     $product->save(); 


     //image upload and save in folder in our app 
     $image=$request->image; 
     if($image){ 
      $imageName=$image->getClientOriginalName(); 
      $image->move('images',$imageName); 
      $formInput['image']=$imageName; 
     } 

     Session::flash('message' , $request->name . '  has been successfully added'); 

     Product::create($formInput); 
     return redirect()->route('product.create'); 
    } 


    public function show($id) 
     { 

      $items = Category::find(6)->products()->where('images' , true)->get(); 

      return view('front.index', compact('items')); 
     } 

Front.index视图

@foreach($items as $item) 
     <img src="{{ url('images', $item->image) }}"> 
@endforeach 
+0

是否存储在本地文件夹和数据库中的图像 – RamAnji

+0

@RamAnji它们存储在公共/图像我Laravel应用程序.. – Martin

+0

@Patwan检查下面详细的答案 – Sletheren

回答

0

尝试,而不是

Product::create($formInput); 

$product->image = $imageName; 
$product->save(); 
+0

下面的代码保存图像的本地文件夹:$图像= $请求 - >图片;如果($ image){ $ imageName = $ image-> getClientOriginalName(); $ image-> move('images',$ imageName); $ formInput ['image'] = $ imageName; } – Martin

+0

是的,在移动图像之后,您应该将图像名称保存到相关产品的“图像”字段。 Create方法将在数据库中插入新行。 –

+0

好吧,我有你的观点,那我怎么去拉动图像OS在视图 – Martin

0

首先检查,如果图像/public/images文件夹 如果是这样下是可用的,尝试访问他们这样说:

<img src="{{ asset('images/'.$item->image) }}"> 

注:

如果该图像不会保存在数据库中,因为您使用的是Product :: create,并且不在$ fillable数组中包含图像,所以它不能成为质量分数gned!

注2:

您可以将图像上传到公共文件夹,以便它们能够从公共目录中使用此功能,我创建访问,你可以用2个参数您的验证后调用它(从公用文件夹请求和相对路径的图像文件到您想要保存它),它会返回您可以在数据库中仍然存在的文件名:

public function upload_image($request['image'], $folder='images'){ 
    $filename = time().'.'.$image->getClientOriginalExtension(); 
    $image->move(public_path($folder), $filename); 
    return $filename; 
} 

希望它能帮助:)

+0

仍然无法正常工作,我真的想知道是否正在找到特定类别的图像导致所有其他数据是存储在数据库中,而图像存储在应用程序中... – Martin

+0

在你的数据库中,什么是'图像'列?我猜你会在那里找到图像名称,如果不尝试将图像添加到$ fillable并尝试再次保留这些值,并且可以在/ public/images中找到图像吗? @Patwan – Sletheren

+0

因为如你所知,我们在数据库中保存的唯一东西是文件名,我们将图像保存在公共文件夹中,以便我们可以从那里访问 – Sletheren

1

strore图像中的本地文件夹

$imageNames = time().'.'.$request->image->getClientOriginalExtension(); 
    $request->image->move(public_path('uploads'), $imageNames); 

第一变化;产品::创建($的formInput) ;至

$oroduct=new Product; 
$product->image= $imageName; 

和@foreach($项目为$项目) 图像)}}“> @endforeach到

<img src="{{ URL::to('/uploads/'.$items->image) }}"/> 

上传本地文件夹,其中u保存images..in乌尔情况下可能是它不同

相关问题