当发生点击提交错误时

when click submit error is occur

当点击提交数据时,数据成功保存并显示在数据库中,但在索引页面中出现错误,我认为不获取用户 ID

Trying to get property 'id' of non-object (View: C:\xampp\htdocs\ytl\resources\views\profile\index.blade.php)

这是index.blade.php文件

<form method="post", id="form", action="{{action('Profile\UserProfileController@index')}}" accept-charset="UTF-8">
            {{ csrf_field() }}


            <div class="row">

                <div class="col-md-6 mb-3 form-group">
                Exchange:<select name="exchange_id" id="exchange" class="form-control " onchange="myfunc()">
                <option value="">Select</option>
                @foreach($exchanges as  $key=>$val )
                    <option value="{{ $val->id }}">{{  $val->exchange }}</option>
                @endforeach
                 </select>

                    {{--{!! Form::label('exchange_id', 'Exchanges: ') !!}--}}
                    {{--{!! Form::select('exchange_id', ['' => 'Choose Options'] + $exchanges, null, ['class' => 'form-control', 'id' => 'exchange', 'name' => 'exchange_id'])!!}--}}
                </div>
                <div class="col-md-6 mb-3 form-group">
                Market<select name="market_id" id="market" class="form-control bindselect" >
                <option value="">Select</option>
                {{--@foreach($markets as  $key=>$val )--}}
                {{--<option value="{{ $val->id }}">{{  $val->market }}</option>--}}
                {{--@endforeach--}}
                </select>
                </div>
                <div class="col-md-6 mb-3 form-group">
                    Country:<select name="country_id" id="country" class="form-control " >
                        <option value="">Select</option>
                        @foreach($countries as  $key=>$val )
                        <option value="{{ $val->id }}">{{  $val->country }}</option>
                        @endforeach
                    </select>
                </div>

            <div class="col-md-6 mb-3 form-group">
                Company:<select name="brokerage_company_id" id="brokerage_company_id" class="form-control " >
                    <option value="">Select</option>
                    @foreach($brokerage_company as  $key=>$val )
                    <option value="{{ $val->id }}">{{  $val->brokerage_company }}</option>
                    @endforeach
                </select>
            </div>


            <div class="col-md-6 mb-3 form-group">
                {{--{!! Form::label('Intraday_Charge', 'Intraday Charge:') !!}--}}
                {{--{!! Form::text('Intraday_Charge', null, ['required' => 'required', 'class'=>'form-control number_only'])!!}--}}
                Intraday_charge: <input type="text" name="charge_intraday" class="form-control"><br>

            </div>
            <div class="col-md-6 mb-3 form-group">
                  Delivery_charge: <input type="text" name="charge_delivery" class="form-control"><br>
            </div>
            <div class="col-md-6 mb-3 form-group">
                Delivery_charge: <input type="text" name="charge_per_lot" class="form-control"><br>
            </div>
            <div class="col-md-6 mb-3 form-group">
                Delivery_charge: <input type="text" name="charge_per_order" class="form-control"><br>
            </div>
            <div class="mb-3 form-group">
                {{--{!! Form::submit('Add trade', ['class'=>'btn btn-success btn-lg']) !!}--}}
                <input type="submit" value="Submit">

            </div>
        </div>
        </form>

这是controller中的which 2 method。第一个索引和第二个存储方法

public function index(Request $request){
        $exchanges = Exchange::select('exchange','id')->get();
        $markets = Market::select('market','id')->get();
        $countries = Country::select('country','id')->get();
        $brokerage_company = BrokerageCompany::select('brokerage_company','id')->get();
        return view('profile.index', compact( 'exchanges','markets','countries','brokerage_company'));
    }


public function store(Request $request){

    $Input = $request->all();
    $user = Auth::user();
    $user->userprofile()->create($Input);
    $user_id = Auth::user()->id;

    $exchanges = Exchange::pluck('exchange','id')->all();
    $markets = Market::pluck('market','id')->all();
    $countries = Country::pluck('country','id')->all();
    $brokerage = BrokerageCompany::pluck('brokerage_company','id')->all();
    $user_profile = UserProfile::pluck('charge_intraday','charge_delivery','charge_per_lot','charge_per_order');

    return view('profile.index', compact( 'exchanges','markets','countries','brokerage','user_profile'));
}

因为 Laravel 5.2 pluck() Eloquent 构建器的方法 returns 来自给定列的值的集合。当您对该集合调用 all() 时,您只需从第一列中获取一组值。例如,当您调用

$exchanges = Exchange::pluck('exchange','id')->all();

$exchanges 将是一个数组,其中包含 exchange 列中的所有值 Exchange table。因此出现错误,因为您正在尝试访问此标量值的 id 属性。

我猜您是想限制从数据库中提取的列数。调用 select() 方法而不是 pluck():

$exchanges = Exchange::select('exchange','id')->get();