Laravel中的表单提交错误:“找不到对象”

时间:2018-09-23 07:43:44

标签: php laravel

我正在尝试在Laravel中的数据库表中插入数据,但是在按下提交时,页面将重定向到“ localhost / create”,并且错误是“ 找不到对象”。我的代码如下。请帮助我为什么会发生此错误,以及从哪里可以获取解决此问题的帮助。

表单控制器代码

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use DB;

class form extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
        return view('form');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $sname = $request->input('sname');
        $fname = $request->input('fname');
        $gradyear = $request->input('gradyear');
        $phone = $request->input('phone');
        $email = $request->input('email');
        $paddress = $request->input('paddress');
        $prog = $request->input('prog');
        $job = $request->input('job');
        $org = $request->input('org');
        $position = $request('position');
        $data=array('sname'=>$sname,"fname"=>$fname,"gradyear"=>$gradyear,"phone"=>$phone,"email"=>$email,"paddress"=>$paddress,"prog"=>$prog,"job"=>$job,"org"=>$org,"position"=>$position);

        DB::table('form')->insert($data);
        echo "Record inserted successfully.<br/>";
        echo '<a href = "/insert">Click Here</a> to go back.';
    }


    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}

form.blade.php代码

@extends('master')

@section('content')
<div class="container"><br>
        <h1 class="text-success text-center">Student Registration Form</h1><br>
        <div class="col-md-offset-3 col-md-6 m-auto d-block">
            <form action="/create" method="post" onsubmit="return validation()">

                <div class="form-group">
                    <label>Student Name: </label>
                    <input type="text" name="sname" id="sname" class="form-control">
                    <span id="studenterror" class="text-danger font-weight-bold"></span>
                </div>

                <div class="form-group">
                    <label>Father Name: </label>
                    <input type="text" name="fname" id="fname" class="form-control">
                    <span id="fnameerror" class="text-danger font-weight-bold"></span>
                </div>

                <div class="form-group">
                    <label>Graduation Year: </label>
                    <input type="date" name="gradyear" id="gyear" class="form-control">
                    <span id="gyearerror" class="text-danger font-weight-bold"></span>
                </div>

                <div class="form-group">
                    <label>Phone: </label>
                    <input type="number" name="phone" id="phone" class="form-control">
                    <span id="phoneerror" class="text-danger font-weight-bold"></span>
                </div>

                <div class="form-group">
                    <label>Email: </label>
                    <input type="email" name="email" id="email" class="form-control">
                    <span id="emailerror" class="text-danger font-weight-bold"></span>
                </div>

                <div class="form-group">
                    <label>Postal Address: </label>
                    <input type="text" name="paddress" id="paddress" class="form-control">
                    <span id="addresserror" class="text-danger font-weight-bold"></span>
                </div>

                <div class="form-group">
                    <label>Program: </label>
                    <select class="form-control" name="prog">
                        <option value="msc">MSc</option>
                        <option value="bs">BS</option>
                        <option value="mphil">MPhil</option>
                        <option value="phd">PHD</option>
                    </select>
                </div>

                <div>
                    <label>Job</label><br>
                    <button type="button" name="job" value="yes" class="btn btn-info" data-toggle="collapse" data-target="#org">Yes</button>
                    <button type="button" name="job" value="no" class="btn btn-info" data-toggle="collapse" data-target="demo">No</button>

                    <div id="org" class="collapse">
                        <div class="form-group">
                            <label>Organization: </label>
                            <select class="form-control" name="org">
                                <option value="higher_edu">Higher Education</option>
                                <option value="soft_eng">Software House</option>
                                <option value="hardware_ind">Hardware Industry</option>
                                <option value="other">Other</option>
                            </select>
                        </div>
                        <label>Position</label>
                        <input type="text" name="position" class="form-control" id="position">

                    </div>
                </div><br>
                <div>
                    <input type="hidden" name="_token" value="{{csrf_token()}}">
                    <input type="submit" name="Submit" class="btn btn-lg col-md-offset-3 col-md-6 m-auto d-block">
                </div>


            </form>

        </div>
</div>
<h4 align="center">---------------------------------------------------<br>
    First Task of Project Learning</h4>


<br>
<a href="{{url('/plearning')}}" class="btn btn-primary">Back to Home</a>
@endsection

web.php代码

<?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', function () {
    return view('welcome');
});
Route::get('/plearning','HomeController@index');
Route::get('/form','form@index');
Route::post('create','form@create');


//for create
Route::get('create','testing@index');
Route::post('store','testing@store');

3 个答案:

答案 0 :(得分:1)

Route::post('create','form@create');

更改为

Route::post('create', 'form@store');


 public function store(Request $request){

 $data = [
      'sname' => $request->sname,
      'fname'=> $request->fname,
      'gradyear' => $request->gradyear,
      'phone' => $request->phone,
      'email' => $request->email,
      'paddress' => $request->paddress,
      'prog' => $request->prog,
      'job' => $request->job,
      'org' => $request->org,
      'position' => $request->position,
 ];

 DB::table('form')->insert($data);
 echo "Record inserted successfully.<br/>";
 echo '<a href = "/insert">Click Here</a> to go back.';
}

答案 1 :(得分:0)

def AverageWithThreshold(myList, threshold, emptyResult = None):
    newList = [item for item in myList if item >= threshold]
    if len(newList) == 0: return emptyResult
    return sum(newList) / len(newList)

更改为

print('%.2f' % (AverageWithThreshold(list1, 50, 0)))

答案 2 :(得分:0)

public interface ICollectableParent<out T> : ICollectableRelatedMonoBehaviour where T : Collectable
{
    // This is allowed - T is used as a return type
    T GetChild();
    // This is *not* allowed - T is used as a parameter
    void SetChild(T child);
}

更改为

Route::post('create','form@create');

并在form.blade.php

Route::post('create', 'form@store');

更改为

<form action="/create" method="post" onsubmit="return validation()">

希望这会有所帮助。

编辑: 如果你写     路线:: post('create','form @ store')-> name('create);

然后查看

<form action="{{ route('create') }}" method="post" onsubmit="return validation()">

或者如果您写     路线:: post('create','form @ store'); 然后查看

<form action="{{ route('create') }}" method="post" onsubmit="return validation()">

查看详细信息:Laravel Url Generation