ErrorException in ProfileController.php line 98: Creating default object from empty value

Published 2 months ago by dzthe

how to solve this error ??? pls help thnks

my controller it said that i have error in my controller???

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\User;
use Auth;
class ProfileController extends Controller

 public function update(Request $request , $id)
    {

        
    $users= user::find($id);
        $users->name= request('name');
        $users->email= request('email');
        $users=save();
        return redirect('user.profile');
    
    }

edit blade


                                  <div id="edit-profile" class="tab-pane">
                                    <section class="panel">                                          
                                          <div class="panel-body bio-graph-info">
                                              <h1> Profile Info</h1>
                                              <form class="form-horizontal" method="post" action="update&<?php echo $users ->id ?>" role="form">   
                                                {{ csrf_field() }}                                               
                                                  <div class="form-group">
                                                      <label class="col-lg-2 control-label">NAME</label>
                                                      <div class="col-lg-6">
                                                          <input type="text" class="form-control" name="name" id="name" value="<?php echo $users ->name ?>" placeholder=" ">
                                                      </div>
                                                  </div>
                                                

                                                  <div class="form-group">
                                                      <label class="col-lg-2 control-label">Email</label>
                                                      <div class="col-lg-6">
                                                          <input type="text" class="form-control" name="email" id="email" value="<?php echo $users ->email ?>" placeholder=" ">
                                                      </div>
                                                  </div>


                                                  <div class="form-group">
                                                      <div class="col-lg-offset-2 col-lg-10">
                                                          <button type="submit" class="btn btn-primary">Save</button>


Best Answer (As Selected By dzthe)
tykus

Are these typos, or do you prefer this URI pattern for some reason?

Route::get('/edit&{id}',[email protected]');
Route::post('/update{id}',[email protected]');
<form action="update&<?php echo $users ->id ?>">

The form action includes a & character, but it is not included in the route - so your wildcard {id} will include the & as well as the id. Change the route definition to match the form action:

Route::post('/update&{id}',[email protected]');
tykus
tykus
2 months ago (316,190 XP)

This $users=save(); ?

$users->save();

dzthe

@tykus change it already and its still not working, i still get the same error ErrorException in ProfileController.php line 98: Creating default object from empty value

this error still point out at

$users->name= request('name');

?

tykus
tykus
2 months ago (316,190 XP)

Your assignment $users is null becasue there is no record with the id you are providing. Use findOrFail($id) so that you throw a ModelNotFoundException in this scenario.

dzthe

@tykus how to implement the finOrFail($id) code? uhm but obviously the id is valid tho i tried to update the user profile code where the user can update his/her new information?

dzthe

@tykus btw its really not supposed to said the id is not found? when i just to need to update the data using the same id. the id should work properly, i think maybe i missed something from the coding? which is cant figure it out what did i do wrong

tykus
tykus
2 months ago (316,190 XP)
 public function update(Request $request , $id)
{   
    $users= User::findOrFail($id); 

    // For a sanity check, die and dump here temporarily
    dd($users);

    $users->name= request('name');
    $users->email= request('email');
    $users->save();

    return redirect('user.profile');
}

If the dd($users) is null, then the id isn't valid. I don't know what id is being given to the update method because I don't know your routes or form action

dzthe

@tykus

route

Route::get('/profile',[email protected]');
Route::get('/edit&{id}',[email protected]');
Route::post('/update{id}',[email protected]');

my edit blade

@extends('user.userlayout')
@section('content')

<!-- edit-profile -->
                                  <div id="edit-profile" class="tab-pane">
                                    <section class="panel">                                          
                                          <div class="panel-body bio-graph-info">
                                              <h1> Profile Info</h1>

                                              <form class="form-horizontal" method="POST" action="update&<?php echo $users ->id ?>" role="form">   
                                                {{ csrf_field() }}                                               
                                                  <div class="form-group">
                                                      <label class="col-lg-2 control-label">NAME</label>
                                                      <div class="col-lg-6">
                                                          <input type="text" class="form-control" name="name" id="name" value="<?php echo $users ->name ?>" placeholder=" ">
                                                      </div>
                                                  </div>
                                                

                                                  <div class="form-group">
                                                      <label class="col-lg-2 control-label">Email</label>
                                                      <div class="col-lg-6">
                                                          <input type="text" class="form-control" name="email" id="email" value="<?php echo $users ->email ?>" placeholder=" ">
                                                      </div>
                                                  </div>


                                                  <div class="form-group">
                                                      <div class="col-lg-offset-2 col-lg-10">
                                                          <button type="submit" class="btn btn-primary">Save</button>
                                                          <button type="button" class="btn btn-danger">Cancel</button>
                                                      </div>

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

@endsection

my profile blade

@extends('user.userlayout')
@section('content')

    <!-- Form validations -->             
              <div class="row">
                  <div class="col-lg-12">
                      <section class="panel">
                          <header class="panel-heading">
                              Profile
                          </header>
                      </section>
                  </div>
               </div>

               <table class="table table-striped table-advance table-hover">
                           <tbody>
                              <tr>
                                 <th><i class="icon_profile"></i> Full Name</th>
                                 <th><i class="icon_mail_alt"></i> Email</th>
                                 <th><i class="icon_cogs"></i> Action</th>

                              </tr>
                              <?php
                                  foreach ($users as $user){
                                    ?>
                                    <tr>
                                    <td><?php echo $user ->name ?></td>
                                    <td><?php echo $user ->email ?></td>
                                    </tr>
                                    <?php
                                  }
                                  ?>
                                  

                              </tbody>
                              </table>

               
@endsection

my profile controller

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\User;
use Auth;
use Illuminate\Database\Eloquent\ModelNotFoundException;
class ProfileController extends Controller
{

     public function __construct()
    {
        $this->middleware('auth');
    }

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

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

        $id = Auth::id();
        $users = user::where("id","=","$id")->get();
        return view('user.profile', ['users' => $users]);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $users= user::find($id);
        return view('user.edit', ['users' => $users]);
    }

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

        $users= user::findOrFail($id);
     dd($users);
        $users->name= request('name');
        $users->email= request('email');
        $users->save();

        return redirect('user.profile');
    }
    
    

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

    public function rules()
    {
        //
    }
}

hope u can assist me where did i go wrong pls
tykus
tykus
2 months ago (316,190 XP)

Are these typos, or do you prefer this URI pattern for some reason?

Route::get('/edit&{id}',[email protected]');
Route::post('/update{id}',[email protected]');
<form action="update&<?php echo $users ->id ?>">

The form action includes a & character, but it is not included in the route - so your wildcard {id} will include the & as well as the id. Change the route definition to match the form action:

Route::post('/update&{id}',[email protected]');
dzthe

@tykus i prefer this url, and i follow a tutorial. yeah i just change the route but i got this after clicking the save button User {#215 ▼ #table: "users" #primaryKey: "id" #casts: array:1 [▶] #fillable: array:6 [▶] #hidden: array:2 [▶] #connection: "mysql" #keyType: "int" +incrementing: true #with: [] #withCount: [] #perPage: 15 +exists: true +wasRecentlyCreated: false #attributes: array:10 [▶] #original: array:10 [▶] #dates: [] #dateFormat: null #appends: [] #events: [] #observables: [] #relations: [] #touches: [] +timestamps: true #visible: [] #guarded: array:1 [▶] #rememberTokenName: "remember_token" }

i seriously do not understand why this happen???

tykus
tykus
2 months ago (316,190 XP)

So you have gotten a user instance in this case! Good. This is progress; you can remove the dd($users) now, and everything should work.

The reason it didn't work before was because the wildcard matched &123 from the form action because it assumed the & was part of the wildcard. Whenever you tried to find a user with id &123, it failed, so you were then attempting to set properties on null rather than an object.

dzthe

@tykus sir thank you so much thank you for helping me and assisting till my problem solve. thank you so much sir, may god bless u

Sign In or create a forum account to participate in this discussion.