Trying to get property 'catid' of non-object using laravel

trying to get property of non-object laravel
trying to get property of non-object codeigniter
trying to get property of non-object in php error
how to solve trying to get property of non-object in php
trying to get property of non-object yii2
trying to get property 'image' of non-object
php notice: trying to get property 'id' of non-object
trying to get property 'role' of non object

This is my code

public function edit($id) {
    $categories = Category::all();
    $category = Category::find($id);
    return view('categories.edit', compact('categories', 'category'));

}

Following is my edit form

{!! Form::open(['url' => ['categories', $category->catid], 'method' => 'put']) !!}
    <div class="form-group padding-t-20 {{ $errors->has('name') ? 'has-error' : '' }}">
        {{ Form::label('name', 'Name') }}
        {{ Form::text('name', $category->name, ['class' => 'form-control', 'placeholder' => 'Category']) }}
        <span class="text-danger">{{ $errors->has('name') ? $errors->first('name') : '' }}</span>
    </div>
    <div class="text-center padding-t-20">
            {{ Form::submit('Update', ['class' => 'btn btn-info btn-fill btn-wd']) }}
    </div>
{!! Form::close() !!}

It works fine but when I use DB facade like below

 public function edit($id) {
        $categories = DB::select('select * from tbl_menu_category');
        $category = DB::select('select * from tbl_menu_category where catid = ?', [$id]);
        return view('categories.edit', compact('categories', 'category'));
}

It gives me error Trying to get property 'catid' of non-object

When you use DB::select() to query database it will return the results as array even if you are trying to get only record from database.

So the solutions is:

public function edit($id) {
  $categories = DB::select('select * from tbl_menu_category');
  $category = DB::select('select * from tbl_menu_category where catid = ?', [$id])[0]; <--- grab the first index of the array returned.
  return view('categories.edit', compact('categories', 'category'));
}

Hope it will help :)

Trying to get property 'user' of non-object, I'm trying to get the properties of a user by the ID. I'm getting the ID through a relationship with my other model. When I use that, this is what get. Public Function GetProperty (name As String) As PropertyInfo. The string containing the name of the public property to get. An object representing the public property with the specified name, if found; otherwise, null. Get Property (String) AmbiguousMatchException. More than one property is found with the specified name. ArgumentNullException.

execute the query

public function edit($id) {
        $categories = DB::table('tbl_menu_category')->get();  <--here
        $category = DB::table('tbl_menu_category')->where('catid', $id)->first(); <-- here
        return view('categories.edit', compact('categories', 'category'));
}

What does "Trying to get property of non object in.." mean , The root problem here is assumptions. For example lets say we have an array here: $test = [ 'foo' => 'bar' ];. If we run echo $test['foo']; we should  Then you try to use the array as an object. Next, the returned query result is an array as well which you try to use as a object.Truth be told I find the getDfnsvTrgt function confusing. I think

$category = DB::table('tbl_menu_category')->where('catid',$id)->first(); try this one...

if does this help you please rate me bro...

Fix Error Trying to get property of non object in Laravel, Notice: Trying to get property of non-object in …/public_html/wp-content/plugins/​wordpress-seo/frontend/schema/class-schema-utils.php on line 26. Through  Notice: trying to get property 'num_rows' of non-object in C:\xampp\htdocs\cecri karikudi\reportdatewise.php on line 83 Converting JSON non-object to XML in C# Call to a member function save() on a non-object using ajax in laravel 5

if you don't know type of incoming data, optional() method is the best for these stuation..

optional($category)->catid

Also don't forget to fetch result when you done with the query.. You can use first() method for single result and get() for multiple results

public function edit($id) {
        $categories = DB::select('select * from tbl_menu_category')->get();
        $category = DB::select('select * from tbl_menu_category where catid = ?', [$id])->first();
        return view('categories.edit', compact('categories', 'category'));
}

Also I have to say that, Eloquant is a advanced orm, so you don't have to write sql directly. You can use QueryBuilder methods, which make your queries more readable..

public function edit($id) {
        $categories = DB::table('tbl_menu_category')->get();
        $category = DB::table('tbl_menu_category')->where(catid,$id)->first();
        return view('categories.edit', compact('categories', 'category'));
}

Notice: Trying to get property of non-object in class-schema-utils.php , `Notice: Trying to get property of non-object in /home/public_html/rarathemes.​com/wp-content/plugins/wordfence/lib/wordfenceClass.php on line 2429. Trying to get property of non-object Posted 4 years ago by thinkjay. Hi what am I missing here, this was working a few days ago and not sure what I did so that it's

Trying to get property of non-object, to access /vi/user/1806/buyerfeedback. I have the marketplace feedback module installed. I am running php 7.3.5 Trying to get property 'uid'  (1/1) ErrorExceptionTrying to get property 'headers' of non-object in VerifyCsrfToken.php line 158 at HandleExceptions->handleError(8, 'Trying to get property 'headers' of non-object',

Trying to get property 'uid' of non-object in UcAddressesPermissions , Notice: Trying to get property 'ID' of non-object in /home/thehes/public_html/wp-​includes/class-wp-query.php on line 3998. Notice: Trying to get property  Forum Laravel Trying to get property 'id' of non-object - how to fix. Reply Follow All Threads Popular This Week Popular All Time Solved Unsolved No Replies Yet

<br /> <b>Notice</b>: Trying to get property 'ID' of non-object in <b , I can see all the auto suggestions so this seems like it should work. However, I get "error": "Trying to get property 'handle' of non-object". I see a  Trying to get property of non-object × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié. × Attention, ce sujet est très ancien.

Comments
  • As error message said check this part catid = ?', [$id]
  • $category = DB::table('tbl_menu_category')->where('catid',$id)->first(); try this one...
  • Yes worked @DejavuGuy
  • if did this help you I left answer in the bottom please mark if it is not difficult for you bro please ...
  • its giving error "Call to a member function get() on array"
  • using optional removes the error but does not get the data
  • @Peace I didin't see your query at the first and I update my answer then. You need the fetch your result when you done with the query, as above, you can use first() or get() methods