Php Laravel 4使用数据库创建打开的表单
我在创建报价单时遇到问题。 页面显示得很好,但当您添加数据并发送表单后,页面会给出错误信息。 拉维尔4号 我的路线:Php Laravel 4使用数据库创建打开的表单,php,jquery,css,forms,laravel,Php,Jquery,Css,Forms,Laravel,我在创建报价单时遇到问题。 页面显示得很好,但当您添加数据并发送表单后,页面会给出错误信息。 拉维尔4号 我的路线: Route::get('/user/{username}', array( 'as' => 'profile-user', 'uses' => 'ProfileController@user' )); Route::get('/profile/offers', array( 'as' => 'profile-offers', 'uses'
Route::get('/user/{username}', array(
'as' => 'profile-user',
'uses' => 'ProfileController@user'
));
Route::get('/profile/offers', array(
'as' => 'profile-offers',
'uses' => 'OffersController@offers'
));
Route::post('/profile/offers', array(
'as' => 'profile-offers',
'uses' => 'OffersController@postDestroy' ));
Route::post('/profile/offers/create', array(
'as' => 'profile-create',
'uses' => 'OffersController@postCreate'
));
我的控制器
controllers/OffersController.php
<?php
class OffersController extends BaseController {
public function __construct() {
$this->beforeFilter('csrf', array('on'=>'post'));
$this->beforeFilter('user');
}
public function Offers() {
$offers = array();
foreach(Category::all() as $category) {
$categories[$category->id] = $category->name;
}
//return View::make('offers.index')
return View::make('profile.offers')
->with('offers', Offer::all())
->with('categories', $categories);
}
public function postCreate() {
$validator = Validator::make(Input::all(), Offer::$rules);
if($validator->passes()){
$offer = new Offer;
$offer->category_id = Input::get('category_id');
$offer->title = Input::get('title');
$offer->description = Input::get('description');
$offer->price = Input::get('price');
$image = Input::file('image');
$filename = date('Y-m-d-H:i:s').'.'.$image->getClientOriginalName();
$path = public_path('img/offers/' . $filename);
Image::make($image->hetRealPath())->resize(468, 249)->save('public/img/offers/'.$filename);
$offer->image = 'img/offers/'.$filename;
$offer->save();
//return Redirect::route('profile-user', Auth::user()->username);
return Redirect::to('profile.offers.create') //
->with('global', 'Dodano ogloszenie');
}
//return Redirect::route('profile-user', Auth::user()->username);
return Redirect::to('profile.offers')
->with('global', 'Cos poszlo nie tak')
->withErrors($validator)
->withInput();
}
public function postDestroy(){
$offer = Offer::find(Input::get('id'));
if ($offer){
File::delete('public/'.$offer->image);
$offer->delete();
//return Redirect::route('profile-user', Auth::user()->username);
return Redirect::to('profile.offers.destroy')
->with('global', 'Skasowano ogłoszenie');
}
}
public function postToggleAvailability() {
$offer = Offer::find(Input::get('id'));
if ($offer){
$offer->availability = Input::get('availability');
$offer->save();
//return Redirect::route('profile-user', Auth::user()->username);
return Redirect::to('profile.offers')->with('global', 'Zaktualizowano')
->with('global', 'Zaktualizowano');
}
//return Redirect::route('profile-user', Auth::user()->username);
return Redirect::to('profile.offers')->with('global', 'zle ogloszenie')
->with('global', 'Zaktualizowano');
}
}
您没有POST请求的路由,并且没有为控制器的其余操作正确设置路由,因此出现404异常。除非明确指定要获取,否则表单提交具有POST
方法。您还必须正确设置所需的控制器操作。Laravel将不知道profile/offers/destroy
应该路由到postdtroy()
,除非您告诉它
Route::post('/profile/offers/destroy', array(
'as' => 'profile-destroy',
'uses' => 'OffersController@postDestroy'
));
每个控制器操作都必须这样做。在完成formi操作后,数据库记录不会自动添加,问题出在{{{Form::submit('Create offers',array('class'=>'secondary-cart-btn'))}我添加了Route::get('/profile/offers',array)('as'=>'配置文件提供','uses'=>'OffersController@offers路由::get('/profile/offers/destroy',数组('as'=>'profile destroy','uses'=>'OffersController@postDestroy);路由::get('/profile/offers/create',数组('as'=>'profile create','uses'=>'OffersController@postCreate);它不会更改任何内容更改Route::get('/profile/offers/destroy',array('as'=>'profile destroy','uses'=>'OffersController@postDestroy“));
toRoute::post(“/profile/offers/destroy”,数组('as'=>'profile destroy','uses'=>'OffersController@postDestroy“));
。我编辑了我的答案。您的表单路由应该是POST,因此用于创建的表单也应该是POST,但我在将列表添加到数据库时遇到问题。POST create不起作用您的创建表单路由到POSTDESTORY()。更改刀片视图中的url:{{Form::open(array('url'=>'profile/offers/create','method'=>'POST','files'=>true))}
谢谢,一切正常;)只有一个问题。当我添加它时,它添加了png图像,这是正常的,但添加jpg图像时,它会返回一条消息,说明有问题。。。
<?php
class Offer extends Eloquent {
protected $fillable = array('category_id,', 'title', 'description', 'price', 'availability', 'image');
public static $rules = array(
'category_id'=>'required|integer',
'title'=>'required|min:2',
'description'=>'required|min:20',
'price'=>'required|numeric',
'availability'=>'integer',
'image'=>'required|image|mimes:jpge,jpg,bmp,png,gif'
);
public function category() {
return $this->belongsTo('Category');
}
}
Route::post('/profile/offers/destroy', array(
'as' => 'profile-destroy',
'uses' => 'OffersController@postDestroy'
));