这是路线代码
Route::resource('/catalog', 'CatalogController');
CatalogController.php
public function store(Request $request)
{
$this->validate($request,[
'cat_name'=>'required',
'cat_desc'=>'required',
'short_name'=>'required|min:0|integer',
'category_image'=>'required'
]);
$categories=new Category;
$title=$request->get('cat_name');
$desc=$request->get('cat_desc');
$short_name=$request->get('short_name');
$categories['cat_name']=$title;
$categories['cat_desc']=$desc;
$categories['short_name']=$short_name;
if($request->file('image')){
$file=$request->file('image');
$imgname=$file->getClientOriginalName();
$file->move('images',$imgname);
}
$categories['category_image']=$imgname;
$categories->save();
return redirect('/catalog');
}
Controller代码有问题吗?获取数据没有问题,我已经通过返回每一行来检查它们。但每次我尝试插入数据时,它都会返回到同一个创建页面。
这是create.blade.php的代码
<div class="row">
<div class="col-lg-20">
<h2 style="color: lightslategrey; padding-left: 30px;">Create Category</h2>
{!!Form::open(['method'=>'POST', 'action'=>'CatalogController@store','files'=>true ])!!}
{{ csrf_field() }}
<div class="form-group row" style="padding-left: 60px; padding-top: 30px; width: 1350px;">
<label for="cat_name" class="col-2 col-form-label">Category Title :</label>
<input class="form-control" name ="cat_name" type="text" placeholder="Enter Title" required>
</div>
<div class="form-group row" style="padding-left: 60px; padding-top: 10px; width: 1350px;">
<label for="cat_desc" class="col-2 col-form-label">Category Description :</label>
<textarea class="form-control" name ="cat_desc" rows= "3" type="text" placeholder="Enter Description" required></textarea>
</div>
<div class="form-group row" style="padding-left: 60px; padding-top: 10px; width: 1350px;">
<label for="short_name" class="col-2 col-form-label">Short Name</label>
<input class="form-control" name ="short_name" type="text" placeholder="Enter Short Name" required>
</div>
<div class="form-group row" style="padding-left: 60px; padding-top: 10px; width: 1350px;">
<label for="image" class="col-2 col-form-label">Upload Image:</label>
<input class="form-control" name ="image" type="file" placeholder="Choose a Image" required>
</div>
<br/>
<br/>
<div class="form-group ">
<div class="col-lg-8 col-lg-offset-5">
<button class = 'btn btn-success' type="submit" style="width: 200px;">Add Category</button>
</div>
</div>
{!! Form::close() !!}
</div>
</div>
我不知道是什么问题。相同的代码在其他控制器中运行。但这不起作用。数据库连接和模型没有问题。
答案 0 :(得分:0)
您似乎正在尝试将模型用作数组,但它是一个对象,因此箭头符号是正确的方法。 (https://laravel.com/docs/5.5/eloquent#retrieving-models)
public function store(Request $request)
{
$this->validate($request,[
'cat_name'=>'required',
'cat_desc'=>'required',
'short_name'=>'required|min:0|integer',
'category_image'=>'required'
]);
$category = new Category;
$category->cat_name = $request->get('cat_name');
$category->cat_desc = $request->get('cat_desc');
$category->short_name = $request->get('short_name');
$file = $request->file('image');
$imageName = $file->getClientOriginalName();
$file->move('images', $imgname);
$category->category_image = $imageName;
$category->save();
return redirect('/catalog');
}
我已经为您更新了您的代码段,并且还简化了一下。对于变量命名,您应该更精确,例如: $ categories(在您的代码中)只代表一个类别。处理图像不必在条件语句中完成,如果您在验证中声明它是必需的话。