2017-09-15 90 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烏爾情況下可能是它不同

相關問題