我有一个Image
模型,它通过控制器保存数据,但它不显示数据,请问是什么问题。
模特
namespace App;
use App\Event\NotifyPhoto;
use Illuminate\Database\Eloquent\Model;
class Image extends Model
{
public $table = "images";
protected $guarded = [];
public $timestamps = false;
public function property_user()
{
return $this->belongsTo('App\PropertyUser');
}
public function facility(){
return $this->belongsTo('App\Facility');
}
}
图像控制器
namespace App\Http\Controllers;
use App\Http\Requests\ImageRequest;
use App\Property;
use App\PropertyUser;
use Illuminate\Http\Request;
use App\Facility;
use App\Image;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\Carbon;
class ImageController extends Controller
{
public function create()
{
dd(Image::all());//This comes back empty
return view('settings.photos');
}
public function store(Request $request)
{
$property_users = PropertyUser::where('user_id', '=', Auth::user()- >id)->get();
foreach ($property_users as $property_user) {
$id = $property_user->property_id;
}
$rules = [
'file' => 'required',
'description' => 'required',
'tag' => 'required'
];
$request->file('file');
if (!empty($request->file('file'))) {
$file_count = count($request->file('file'));
} else $file_count = null;
foreach (range(0, $file_count) as $index) {
$rules['file.' . $index] = 'image|mimes:jpeg,gif,webp,bmp,png|max:2048';
}
$validator = Validator::make(Input::all(), $rules);
if ($validator->fails()) {
return back()
->withErrors($validator)
->withInput();
} else {
$files = $request->file('file');
$description = $request->input('description');
$tag = $request->input('tag');
$i = 0;
foreach ($files as $file) {
$i++;
$file1 = $file->move(public_path() . '/upload_images/', $file->getClientOriginalName());
$url1 = $url = URL::to("/") . '/upload_images/' . $file->getClientOriginalName();
$image = Image::create([
'filename' => $url,
'description' => $description,
'facility_id' => 16, //$new_fac->id ?? $f->id,
'created_at' => Carbon::now(),
'updated_at' => Carbon::now(),
'tag' => $tag
]);
dd($image);//This displays the files saved and their ids in the table
]
}
return redirect('settings/photos');
}
}
}
答案 0 :(得分:2)
尝试将此添加到您的App/Image
:
protected $fillable = [
'filename',
'description',
'facility_id',
'created_at',
'updated_at',
'tag',
];
https://laravel.com/docs/5.6/eloquent#mass-assignment
另外,您能否确认正在向数据库中添加新行?