-1

Admin Controller

public function edit_portal_sub(Request $request)
    {
        $portal=Oex_portal::where('id',$request->id)->get()->first();
        $portal->name=$request->name;
        $portal->email=$request->email;
        $portal->mobile_no=$request->mobile_no;
        if($request->password!='')
            $portal->password=$request->password;
        $portal->update();
        echo json_encode(array('status'=>'true','message'=>'Portal Successfully Updated','reload'=>url('admin/manage_portal')));
    }

web.php(route)

Route::post('/admin/edit_portal_sub','Admin@edit_portal_sub');

edit_portal.blade(form)

@extends('layouts.app')
@section('title','Edit Portal')
@section('content')
    <!-- Content Wrapper. Contains page content -->
    <div class="content-wrapper">
        <!-- Content Header (Page header) -->
        <div class="content-header">
            <div class="container-fluid">
                <div class="row mb-2">
                    <div class="col-sm-6">
                        <h1 class="m-0 text-dark">Edit Portal</h1>
                    </div><!-- /.col -->
                    <div class="col-sm-6">
                        <ol class="breadcrumb float-sm-right">
                            <li class="breadcrumb-item"><a href="#">Home</a></li>
                            <li class="breadcrumb-item active">Edit Portal</li>
                        </ol>
                    </div><!-- /.col -->
                </div><!-- /.row -->
            </div><!-- /.container-fluid -->
        </div>
        <!-- /.content-header -->
        <section class="content">
            <div class="container-fluid">
                <div class="row">
                    <div class="col-12">
                        <!-- Default box -->
                        <div class="card">
                            <div class="card-body">
                                <form action="{{ url('admin/edit_portal_sub') }} " class="database_operation">
                                    <div class="row">
                                        <div class="col-sm-12">
                                            <div class="form-group">
                                                <label>Enter Name</label>
                                                {{ csrf_field() }}
                                                <input type="hidden" name="id" value="{{ $portal_info->email }}">
                                                <input type="text" value="{{ $portal_info->name }}" name="name" required="required" placeholder="Enter Name" class="form-control">
                                            </div>
                                        </div>
                                        <div class="col-sm-12">
                                            <div class="form-group">
                                                <label>Enter Email</label>
                                                <input type="text" value="{{ $portal_info->email }}" name="email" required="required" placeholder="Enter Email" class="form-control">
                                            </div>
                                        </div>
                                        <div class="col-sm-12">
                                            <div class="form-group">
                                                <label>Enter Mobile No</label>
                                                <input type="text" value="{{ $portal_info->mobile_no }}" name="mobile_no" required="required" placeholder="Enter Mobile No" class="form-control">
                                            </div>
                                        </div>
                                        <div class="col-sm-12">
                                            <div class="form-group">
                                                <label>Enter Password</label>
                                                <input type="password" name="password"  placeholder="Enter Password" class="form-control">
                                            </div>
                                        </div>
                                        <div class="col-sm-12">
                                            <div class="form-group">
                                                <button class="btn btn-primary"> Update</button>
                                            </div>
                                        </div>
                                    </div>
                                </form>
                            </div>
                            <!-- /.card-body -->

                            <!-- /.card-footer-->
                        </div>
                        <!-- /.card -->
                    </div>
                </div>
            </div>
        </section>
    </div>
@endsection

when am update data am face that error Error

message: "Creating default object from empty value"
trace: [{file: "C:\xampp\htdocs\project\online_exm_sys\app\Http\Controllers\Admin.php", line: 243,…},…]
0: {file: "C:\xampp\htdocs\project\online_exm_sys\app\Http\Controllers\Admin.php", line: 243,…}
1: {function: "edit_portal_sub", class: "App\Http\Controllers\Admin", type: "->"}

when am update data and inspect the page i show error i cant understad how to solve it...plz help am new laravel user ...what is its best solution am stuck for this error 2 days..i try every possible solution

  • I didn't understood the question completely. Can you please try to be more specific? You can use response()->json by the way instead of json_encode. And can you please try including the entire error stack? – Helper Jul 15 '20 at 08:00

2 Answers2

1

I am not sure, but you can replace those two lines :

$portal=Oex_portal::where('id',$request->id)->get()->first();
$portal->update();

With

$portal=Oex_portal::find($request->id); //you can use findorfail as well
$portal->save();

besides, you are supplying the email instead of id in the view:

 <input type="hidden" name="id" value="{{ $portal_info->email }}">
Ahmed
  • 36
  • 3
0

you are using the update method on the model without passing any data into it. When you directly modify a models attribute with $portal->name = "test"; you then need to use the save method $portal->save(); when using the update you need to do like so.

$portal = Oex_portal::where('id', $request->id)->get()->first();
if ($portal) {
    $data = [
        'name' => $request->name,
        'email' => $request->email,
        'mobile_no' => $request->mobile_no,
    ];

    if ($request->password != '') {
        $data['password'] = $request->password;
    }

    $portal->update($data);
}

The above will cause a mass assignment exception without adding them to the fillable fields in the model. you are best to keep what you have and just throw a save instead of an update.

p.s. whats with your models name havning an underscore? why not OexPortal

Michael Mano
  • 3,339
  • 2
  • 14
  • 35