检索 Laravel 5.4 中特定类别的产品
Retrieving a product of a specific category in Laravel 5.4
我有一个 Laravel 项目,其中我创建了两个表(产品和类别)并通过一对多关系将它们互连。一个类别有很多产品。我创建了一个名为 ProductsController 的 CRUD 控制器,并将图像存储在一个名为 images inside public in Laravel app.
的文件夹中
当我尝试检索特定类别的存储图像并显示在视图中时,问题就出现了。
请协助。
类别模型
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
首先检查 /public/images
文件夹下是否有图片
如果是这样,请尝试以这种方式访问它们:
<img src="{{ asset('images/'.$item->image) }}">
注:
如果图像未保存在数据库中,那是因为您使用的是 Product::create 并且未将图像包含在 $fillable 数组中,因此无法批量分配!
注2:
您可以将图像上传到 public 文件夹,这样就可以使用我创建的这个函数从 public 目录访问它们,您可以在验证后使用 2 个参数调用它 (来自请求的图像文件和从 public 文件夹到您要保存它的位置的相对路径) 它将 return您可以保存在数据库中的文件名:
public function upload_image($request['image'], $folder='images'){
$filename = time().'.'.$image->getClientOriginalExtension();
$image->move(public_path($folder), $filename);
return $filename;
}
希望对您有所帮助:)
试试看
Product::create($formInput);
类似
$product->image = $imageName;
$product->save();
在本地文件夹中存储图片
$imageNames = time().'.'.$request->image->getClientOriginalExtension();
$request->image->move(public_path('uploads'), $imageNames);
第一次更改;Product::create($formInput);到
$oroduct=new Product;
$product->image= $imageName;
和@foreach($items as $item)
图片)}}”>
@endforeach 到
<img src="{{ URL::to('/uploads/'.$items->image) }}"/>
上传是您保存图像的本地文件夹..在您的情况下可能不同
我有一个 Laravel 项目,其中我创建了两个表(产品和类别)并通过一对多关系将它们互连。一个类别有很多产品。我创建了一个名为 ProductsController 的 CRUD 控制器,并将图像存储在一个名为 images inside public in Laravel app.
的文件夹中当我尝试检索特定类别的存储图像并显示在视图中时,问题就出现了。 请协助。
类别模型
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
首先检查 /public/images
文件夹下是否有图片
如果是这样,请尝试以这种方式访问它们:
<img src="{{ asset('images/'.$item->image) }}">
注:
如果图像未保存在数据库中,那是因为您使用的是 Product::create 并且未将图像包含在 $fillable 数组中,因此无法批量分配!
注2:
您可以将图像上传到 public 文件夹,这样就可以使用我创建的这个函数从 public 目录访问它们,您可以在验证后使用 2 个参数调用它 (来自请求的图像文件和从 public 文件夹到您要保存它的位置的相对路径) 它将 return您可以保存在数据库中的文件名:
public function upload_image($request['image'], $folder='images'){
$filename = time().'.'.$image->getClientOriginalExtension();
$image->move(public_path($folder), $filename);
return $filename;
}
希望对您有所帮助:)
试试看
Product::create($formInput);
类似
$product->image = $imageName;
$product->save();
在本地文件夹中存储图片
$imageNames = time().'.'.$request->image->getClientOriginalExtension();
$request->image->move(public_path('uploads'), $imageNames);
第一次更改;Product::create($formInput);到
$oroduct=new Product;
$product->image= $imageName;
和@foreach($items as $item) 图片)}}”> @endforeach 到
<img src="{{ URL::to('/uploads/'.$items->image) }}"/>
上传是您保存图像的本地文件夹..在您的情况下可能不同