php - Laravel 4 创建用数据库打开的表单

标签 php jquery css forms laravel

我在创建报价表时遇到问题。 页面显示很好,但是当您添加数据和发送表单后,页面出现错误。 拉维尔 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'
    ));

我的 Controller Controller /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');
    }

}

最佳答案

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

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

您必须为每个 Controller 操作执行此操作。

关于php - Laravel 4 创建用数据库打开的表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27198352/

相关文章:

php - 意外的其他情况(T_ELSE)

php - 不止一个 str_replace 实例破坏了 php 代码

html - 表格行边框半径不起作用 - 替代方案?

javascript - 调整元素大小

php - PDF X-1a :2001 format in PHP

php - 在 PHP 中将实例方法作为参数传递

javascript - JQuery - 使用 Flot Charts 从 php 绘制时间序列数据

javascript - Symfony Controller 返回的变量始终未定义

jquery - 按第二列对表进行排序 :jquery

html - 设置大小/长宽比的图像,这也是响应式的