Trying to get property 'id' of non-object laravel












1














can someone to help me ? i have an error Trying to get property 'id' of non-object laravel while try to show my edit form



this is my controller



public function edit($id)
{
$produk = produk::where('id',$id)->first();
return view('produk.edit',compact('produk'));
}


public function update(Request $request, $id)
{
produk::where('id',$id)
->update([
'nama' => $request->nama,
'id_kategori' => $request->kategori,
'qty' => $request->qty,
'harga_beli' => $request->beli,
'harga_jual' => $request->jual,
]);
return redirect()->route('produk.index');
}


this is my model



<?php

namespace App;

use IlluminateDatabaseEloquentModel;

class produk extends Model
{
protected $guarded = ['id','created_at','updated_at'];

public function kategoris()
{
return $this->hasOne('Appkategori', 'id', 'id_kategori');
}
}


and this is my view



<select class="form-control" name="kategori">
<option value="Pilih Kategori"></option>
@foreach ($produk as $k)
<option value="{{ $k->id }}" @if($produk->id_kategori == $k->id) selected @endif>{{$k->nama}}</option>
@endforeach
</select>









share|improve this question
























  • try to display and die your $id on the edit method. seems that the id is null.
    – Dearwolves
    Nov 21 at 2:45


















1














can someone to help me ? i have an error Trying to get property 'id' of non-object laravel while try to show my edit form



this is my controller



public function edit($id)
{
$produk = produk::where('id',$id)->first();
return view('produk.edit',compact('produk'));
}


public function update(Request $request, $id)
{
produk::where('id',$id)
->update([
'nama' => $request->nama,
'id_kategori' => $request->kategori,
'qty' => $request->qty,
'harga_beli' => $request->beli,
'harga_jual' => $request->jual,
]);
return redirect()->route('produk.index');
}


this is my model



<?php

namespace App;

use IlluminateDatabaseEloquentModel;

class produk extends Model
{
protected $guarded = ['id','created_at','updated_at'];

public function kategoris()
{
return $this->hasOne('Appkategori', 'id', 'id_kategori');
}
}


and this is my view



<select class="form-control" name="kategori">
<option value="Pilih Kategori"></option>
@foreach ($produk as $k)
<option value="{{ $k->id }}" @if($produk->id_kategori == $k->id) selected @endif>{{$k->nama}}</option>
@endforeach
</select>









share|improve this question
























  • try to display and die your $id on the edit method. seems that the id is null.
    – Dearwolves
    Nov 21 at 2:45
















1












1








1







can someone to help me ? i have an error Trying to get property 'id' of non-object laravel while try to show my edit form



this is my controller



public function edit($id)
{
$produk = produk::where('id',$id)->first();
return view('produk.edit',compact('produk'));
}


public function update(Request $request, $id)
{
produk::where('id',$id)
->update([
'nama' => $request->nama,
'id_kategori' => $request->kategori,
'qty' => $request->qty,
'harga_beli' => $request->beli,
'harga_jual' => $request->jual,
]);
return redirect()->route('produk.index');
}


this is my model



<?php

namespace App;

use IlluminateDatabaseEloquentModel;

class produk extends Model
{
protected $guarded = ['id','created_at','updated_at'];

public function kategoris()
{
return $this->hasOne('Appkategori', 'id', 'id_kategori');
}
}


and this is my view



<select class="form-control" name="kategori">
<option value="Pilih Kategori"></option>
@foreach ($produk as $k)
<option value="{{ $k->id }}" @if($produk->id_kategori == $k->id) selected @endif>{{$k->nama}}</option>
@endforeach
</select>









share|improve this question















can someone to help me ? i have an error Trying to get property 'id' of non-object laravel while try to show my edit form



this is my controller



public function edit($id)
{
$produk = produk::where('id',$id)->first();
return view('produk.edit',compact('produk'));
}


public function update(Request $request, $id)
{
produk::where('id',$id)
->update([
'nama' => $request->nama,
'id_kategori' => $request->kategori,
'qty' => $request->qty,
'harga_beli' => $request->beli,
'harga_jual' => $request->jual,
]);
return redirect()->route('produk.index');
}


this is my model



<?php

namespace App;

use IlluminateDatabaseEloquentModel;

class produk extends Model
{
protected $guarded = ['id','created_at','updated_at'];

public function kategoris()
{
return $this->hasOne('Appkategori', 'id', 'id_kategori');
}
}


and this is my view



<select class="form-control" name="kategori">
<option value="Pilih Kategori"></option>
@foreach ($produk as $k)
<option value="{{ $k->id }}" @if($produk->id_kategori == $k->id) selected @endif>{{$k->nama}}</option>
@endforeach
</select>






laravel






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 at 7:58









Peter

8641213




8641213










asked Nov 21 at 2:40









IKetut Widhi Adnyana

133




133












  • try to display and die your $id on the edit method. seems that the id is null.
    – Dearwolves
    Nov 21 at 2:45




















  • try to display and die your $id on the edit method. seems that the id is null.
    – Dearwolves
    Nov 21 at 2:45


















try to display and die your $id on the edit method. seems that the id is null.
– Dearwolves
Nov 21 at 2:45






try to display and die your $id on the edit method. seems that the id is null.
– Dearwolves
Nov 21 at 2:45














2 Answers
2






active

oldest

votes


















2














Its because of this



$produk = produk::where('id',$id)->first();


this returns an object not an array of object. thats why your getting an error on your view. Instead use:



$produk = produk::where('id',$id)->get();


to return an array of object.






share|improve this answer





















  • im already follow your answer.. but i still get error Property [id] does not exist on this collection instance.
    – IKetut Widhi Adnyana
    Nov 21 at 6:17












  • @IKetutWidhiAdnyana Please post your full error here. To help you better
    – Dearwolves
    Nov 21 at 7:10










  • @IKetutWidhiAdnyana are you sure you have and id field on your database?
    – Dearwolves
    Nov 21 at 7:12



















0














You are trying to foreach trough product properties, but looks like you need to foreach trough collection of categories.



Add categories to view in controller:



public function edit($id)
{
$produk = produk::find($id);
$kategoris = kategori::all();
return view('produk.edit',compact('produk', 'kategoris'));
}


Iterate trough $kategoris (not $produk) in View:



<select class="form-control" name="id_kategori">
<option value="Pilih Kategori"></option>
@foreach ($kategoris as $kategori)
<option value="{{ $kategori->id }}" @if($produk->id_kategori == $kategori->id) selected @endif>{{$kategori->nama}}</option>
@endforeach
</select>


Also, if foreign key is id_kategori, it is better to use name=id_kategori istead of name=kategori



You don't need relation here, because you compare categories ids with id_kategori attribute. But you should replace hasOne to belongsTo in this case.



public function kategoris()
{
return $this->belongsTo('Appkategori', 'id_kategori');
}





share|improve this answer























  • thanks for advance.. i solved this error
    – IKetut Widhi Adnyana
    Nov 21 at 9:03











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53404572%2ftrying-to-get-property-id-of-non-object-laravel%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














Its because of this



$produk = produk::where('id',$id)->first();


this returns an object not an array of object. thats why your getting an error on your view. Instead use:



$produk = produk::where('id',$id)->get();


to return an array of object.






share|improve this answer





















  • im already follow your answer.. but i still get error Property [id] does not exist on this collection instance.
    – IKetut Widhi Adnyana
    Nov 21 at 6:17












  • @IKetutWidhiAdnyana Please post your full error here. To help you better
    – Dearwolves
    Nov 21 at 7:10










  • @IKetutWidhiAdnyana are you sure you have and id field on your database?
    – Dearwolves
    Nov 21 at 7:12
















2














Its because of this



$produk = produk::where('id',$id)->first();


this returns an object not an array of object. thats why your getting an error on your view. Instead use:



$produk = produk::where('id',$id)->get();


to return an array of object.






share|improve this answer





















  • im already follow your answer.. but i still get error Property [id] does not exist on this collection instance.
    – IKetut Widhi Adnyana
    Nov 21 at 6:17












  • @IKetutWidhiAdnyana Please post your full error here. To help you better
    – Dearwolves
    Nov 21 at 7:10










  • @IKetutWidhiAdnyana are you sure you have and id field on your database?
    – Dearwolves
    Nov 21 at 7:12














2












2








2






Its because of this



$produk = produk::where('id',$id)->first();


this returns an object not an array of object. thats why your getting an error on your view. Instead use:



$produk = produk::where('id',$id)->get();


to return an array of object.






share|improve this answer












Its because of this



$produk = produk::where('id',$id)->first();


this returns an object not an array of object. thats why your getting an error on your view. Instead use:



$produk = produk::where('id',$id)->get();


to return an array of object.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 21 at 2:48









Dearwolves

199113




199113












  • im already follow your answer.. but i still get error Property [id] does not exist on this collection instance.
    – IKetut Widhi Adnyana
    Nov 21 at 6:17












  • @IKetutWidhiAdnyana Please post your full error here. To help you better
    – Dearwolves
    Nov 21 at 7:10










  • @IKetutWidhiAdnyana are you sure you have and id field on your database?
    – Dearwolves
    Nov 21 at 7:12


















  • im already follow your answer.. but i still get error Property [id] does not exist on this collection instance.
    – IKetut Widhi Adnyana
    Nov 21 at 6:17












  • @IKetutWidhiAdnyana Please post your full error here. To help you better
    – Dearwolves
    Nov 21 at 7:10










  • @IKetutWidhiAdnyana are you sure you have and id field on your database?
    – Dearwolves
    Nov 21 at 7:12
















im already follow your answer.. but i still get error Property [id] does not exist on this collection instance.
– IKetut Widhi Adnyana
Nov 21 at 6:17






im already follow your answer.. but i still get error Property [id] does not exist on this collection instance.
– IKetut Widhi Adnyana
Nov 21 at 6:17














@IKetutWidhiAdnyana Please post your full error here. To help you better
– Dearwolves
Nov 21 at 7:10




@IKetutWidhiAdnyana Please post your full error here. To help you better
– Dearwolves
Nov 21 at 7:10












@IKetutWidhiAdnyana are you sure you have and id field on your database?
– Dearwolves
Nov 21 at 7:12




@IKetutWidhiAdnyana are you sure you have and id field on your database?
– Dearwolves
Nov 21 at 7:12













0














You are trying to foreach trough product properties, but looks like you need to foreach trough collection of categories.



Add categories to view in controller:



public function edit($id)
{
$produk = produk::find($id);
$kategoris = kategori::all();
return view('produk.edit',compact('produk', 'kategoris'));
}


Iterate trough $kategoris (not $produk) in View:



<select class="form-control" name="id_kategori">
<option value="Pilih Kategori"></option>
@foreach ($kategoris as $kategori)
<option value="{{ $kategori->id }}" @if($produk->id_kategori == $kategori->id) selected @endif>{{$kategori->nama}}</option>
@endforeach
</select>


Also, if foreign key is id_kategori, it is better to use name=id_kategori istead of name=kategori



You don't need relation here, because you compare categories ids with id_kategori attribute. But you should replace hasOne to belongsTo in this case.



public function kategoris()
{
return $this->belongsTo('Appkategori', 'id_kategori');
}





share|improve this answer























  • thanks for advance.. i solved this error
    – IKetut Widhi Adnyana
    Nov 21 at 9:03
















0














You are trying to foreach trough product properties, but looks like you need to foreach trough collection of categories.



Add categories to view in controller:



public function edit($id)
{
$produk = produk::find($id);
$kategoris = kategori::all();
return view('produk.edit',compact('produk', 'kategoris'));
}


Iterate trough $kategoris (not $produk) in View:



<select class="form-control" name="id_kategori">
<option value="Pilih Kategori"></option>
@foreach ($kategoris as $kategori)
<option value="{{ $kategori->id }}" @if($produk->id_kategori == $kategori->id) selected @endif>{{$kategori->nama}}</option>
@endforeach
</select>


Also, if foreign key is id_kategori, it is better to use name=id_kategori istead of name=kategori



You don't need relation here, because you compare categories ids with id_kategori attribute. But you should replace hasOne to belongsTo in this case.



public function kategoris()
{
return $this->belongsTo('Appkategori', 'id_kategori');
}





share|improve this answer























  • thanks for advance.. i solved this error
    – IKetut Widhi Adnyana
    Nov 21 at 9:03














0












0








0






You are trying to foreach trough product properties, but looks like you need to foreach trough collection of categories.



Add categories to view in controller:



public function edit($id)
{
$produk = produk::find($id);
$kategoris = kategori::all();
return view('produk.edit',compact('produk', 'kategoris'));
}


Iterate trough $kategoris (not $produk) in View:



<select class="form-control" name="id_kategori">
<option value="Pilih Kategori"></option>
@foreach ($kategoris as $kategori)
<option value="{{ $kategori->id }}" @if($produk->id_kategori == $kategori->id) selected @endif>{{$kategori->nama}}</option>
@endforeach
</select>


Also, if foreign key is id_kategori, it is better to use name=id_kategori istead of name=kategori



You don't need relation here, because you compare categories ids with id_kategori attribute. But you should replace hasOne to belongsTo in this case.



public function kategoris()
{
return $this->belongsTo('Appkategori', 'id_kategori');
}





share|improve this answer














You are trying to foreach trough product properties, but looks like you need to foreach trough collection of categories.



Add categories to view in controller:



public function edit($id)
{
$produk = produk::find($id);
$kategoris = kategori::all();
return view('produk.edit',compact('produk', 'kategoris'));
}


Iterate trough $kategoris (not $produk) in View:



<select class="form-control" name="id_kategori">
<option value="Pilih Kategori"></option>
@foreach ($kategoris as $kategori)
<option value="{{ $kategori->id }}" @if($produk->id_kategori == $kategori->id) selected @endif>{{$kategori->nama}}</option>
@endforeach
</select>


Also, if foreign key is id_kategori, it is better to use name=id_kategori istead of name=kategori



You don't need relation here, because you compare categories ids with id_kategori attribute. But you should replace hasOne to belongsTo in this case.



public function kategoris()
{
return $this->belongsTo('Appkategori', 'id_kategori');
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 21 at 9:04

























answered Nov 21 at 6:43









IndianCoding

84519




84519












  • thanks for advance.. i solved this error
    – IKetut Widhi Adnyana
    Nov 21 at 9:03


















  • thanks for advance.. i solved this error
    – IKetut Widhi Adnyana
    Nov 21 at 9:03
















thanks for advance.. i solved this error
– IKetut Widhi Adnyana
Nov 21 at 9:03




thanks for advance.. i solved this error
– IKetut Widhi Adnyana
Nov 21 at 9:03


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53404572%2ftrying-to-get-property-id-of-non-object-laravel%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Tonle Sap (See)

I get strange results when I access the Sqlitedatabase with Unity C# via XAMPP

Guatemaltekische Davis-Cup-Mannschaft