【问题标题】:Laravel 4 creating form open with databaseLaravel 4 创建用数据库打开的表单
【发布时间】:2015-01-27 16:26:43
【问题描述】:

我在创建报价单时遇到问题。 页面显示很好,但是当您添加数据并发送表单后,页面会出错。 拉拉维尔 4 enter link description here 我的路线:

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'
    ));

我的控制器 控制器/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');
}


}

我的看法 浏览量/个人资料/优惠

        <h1>Offerts</h1>

    <ul>
        @foreach($offers as $offer)
            <li>
                {{ HTML::image($offer->image, $offer->title, array('width'=>'50')) }}
                {{ $offer->title }} -
                {{ Form::open(array('url'=>'profile/offers/destroy', 'class'=>'form-inline')) }}
                {{ Form::hidden('id', $offer->id) }}
                {{ Form::submit('delete') }}
                {{ Form::close() }} -

                {{ Form::open(array('url'=>'profile/offers/toggle-availability', 'class'=>'form-inline')) }}
                {{ Form::hidden('id', $offer->id) }}
                {{ Form::select('availability', array('1'=>'In Stock', 'O'=>'Out of Stock'), $offer->availability)}}
                {{ Form::submit('Update') }}
                {{ Form::close() }}


            </li>
        @endforeach
    </ul>

    <h2>Create new offers</h2><hr>

    @if($errors->has())
    <div id="form-errors">
        <p>the following errors have occurred:</p>

        <ul>
            @foreach($errors->all() as $error)
                <li>{{ error }}</li>
            @endforeach
        </ul>
    </div>
    @endif


    {{ Form::open(array('url'=>'profile/offers', 'method' => 'POST', 'files'=>true)) }}
    <p>
        {{ Form::label('category_id', 'Category') }}
        {{ Form::select('category_id', $categories) }}

    </p>

    <p>
      {{ Form::label('title') }}
     {{ Form:: text('title', null, array('class' => 'form-control', 'required' => '')) }}
    </p>
    <p>
        {{ Form::label('description') }}
        {{ Form:: text('description', null, array('class' => 'form-control', 'required' => '')) }}
    </p>
    <p>
        {{ Form::label('price') }}
        {{ Form::text('price', null, array('class'=>'form-price')) }}

    </p>

    <p>
        {{ Form::label('image', 'Choose an image') }}
        {{ Form::file('image') }}
    </p>



    {{ Form::submit('Create offers', array('class'=>'secondary-cart-btn')) }}
    {{ Form::close() }}

我的模型/Offer.php

<?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');
    }

}

【问题讨论】:

  • 填表后不会自动添加数据库记录

标签: php jquery css forms laravel


【解决方案1】:

您没有 POST 请求的路由,也没有正确设置其余控制器操作的路由,因此出现 404 异常。除非明确指定 GET,否则表单提交的方法为 POST。您还必须正确设置所需的控制器操作。 Laravel 不会知道 profile/offers/destroy 应该路由到 postDestroy() 除非你告诉它。

Route::post('/profile/offers/destroy', array(
       'as' => 'profile-destroy',
       'uses' => 'OffersController@postDestroy'
));

您必须为您的控制器操作执行此操作。

【讨论】:

  • 我的问题在于 {{ Form::submit('Create offer', array('class'=>'secondary-cart-btn')) }} 我添加了 Route::get ('/profile/offers', array('as' => 'profile-offers', 'uses' => 'OffersController@offers' )); Route::get('/profile/offers/destroy', array('as' => 'profile-destroy', 'uses' => 'OffersController@postDestroy' )); Route::get('/profile/offers/create', array('as' => 'profile-create', 'uses' => 'OffersController@postCreate' ));它不会改变任何东西
  • Route::get('/profile/offers/destroy', array( 'as' =&gt; 'profile-destroy', 'uses' =&gt; 'OffersController@postDestroy' )); 更改为 Route::post('/profile/offers/destroy', array( 'as' =&gt; 'profile-destroy', 'uses' =&gt; 'OffersController@postDestroy' )); 。我编辑了我的答案。您的表单路由应该是 POST,因此创建的路由也应该是 POST
  • 大致删除工作,但我在将列表添加到数据库时遇到问题。 postCreate 不起作用
  • 您的创建表单正在路由到 postDestroy()。更改刀片视图中的 url:{{ Form::open(array('url'=&gt;'profile/offers/create', 'method' =&gt; 'POST', 'files'=&gt;true)) }}
  • 谢谢,一切正常;)只有一个问题。当我添加它时,它会添加 png 图像就可以了,但是当我添加 jpg 图像时,它会返回一条消息,指出有问题...
猜你喜欢
  • 2014-09-06
  • 2015-02-02
  • 1970-01-01
  • 1970-01-01
  • 2013-03-15
  • 1970-01-01
  • 1970-01-01
  • 2017-12-21
  • 2014-08-13
相关资源
最近更新 更多