我想展示我刚刚上传的图片,只展示给已上传的图片。例如,用户表包含jack,Emily和John,所以如果jack要上传文件,图像将直接显示在他下面,但我不知道该怎么做?
控制器:(我如何存储图像)
import UIKit
import CoreData
class ItemDetailsVC: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
@IBOutlet weak var storePicker: UIPickerView!
@IBOutlet weak var titleField: CustomTextField!
@IBOutlet weak var priceField: CustomTextField!
@IBOutlet weak var detailsField: CustomTextField!
var stores = [Store]()
override func viewDidLoad() {
super.viewDidLoad()
storePicker.delegate = self
storePicker.dataSource = self
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
let store = stores[row]
return store.name
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return stores.count
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
// update
}
func getStores() {
let fetchRequest: NSFetchRequest<Store> = Store.fetchRequest()
do {
self.stores = try context.fetch(fetchRequest)
self.storePicker.reloadAllComponents()
} catch {
// some code
}
}
@IBAction func savePressed(_ sender: UIButton) {
let item = Item(context: context)
if let title = titleField.text {
item.title = title
}
if let price = priceField.text {
item.price = (price as NSString).doubleValue
}
if let details = detailsField.text {
item.details = details
}
item.toStore = stores[storePicker.selectedRow(inComponent: 0)] error: cannot assign value of type 'Store' to value of type NSSet?
}
view.blade.php
public function store1(Request $request){
$this->validate($request, [
'file' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
]);
if ($request->hasFile('file')) {
$image = $request->file('file');
$name = $image->getClientOriginalName();
$size = $image->getClientSize();
$destinationPath = public_path('/images');
$image->move($destinationPath, $name);
$userImage = new UserImage;
$userImage->name = $name;
$userImage->size = $size;
//dd($userImage);
$userImage->save();
}
我看到人们在他们的blade.php中使用了这个,但我不知道$ model是什么:
@foreach ($data as $object)
<b>Name: </b>{{ $object->name }}<br><br>
@endforeach
Upload.blade.php(这是我的上传页面,用户将上传他们的图片)
<img src="{{ asset('public/images/' . $model->image) }}">
答案 0 :(得分:1)
有很多方法可以做到。
您可以使用Eloquent
或使用Laravel的query builder
。
在您的控制器中,您应该获得用户上传的所有图像。
Query builder
方法:
//don't forget the namespace
`use DB;`
//in your function write this.
$images = DB::table('user_images')
->join('users', 'users.id', '=', 'user_images.user_id')
->where('users.id', '=', $id)
->get();
//use dd($images) to verify that the variable $images has data
//send $images in your view
在你的视图中写一个foreach循环,如下所示:
@foreach($images as $image)
<img src="{{ asset('public/images/' . $image->name ) }}">
@endforeach