Trying to get property 'id' of non-object laravel
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
add a comment |
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
try to display and die your $id on the edit method. seems that the id is null.
– Dearwolves
Nov 21 '18 at 2:45
add a comment |
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
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
laravel
edited Nov 21 '18 at 7:58
Peter
8641213
8641213
asked Nov 21 '18 at 2:40
IKetut Widhi AdnyanaIKetut Widhi Adnyana
133
133
try to display and die your $id on the edit method. seems that the id is null.
– Dearwolves
Nov 21 '18 at 2:45
add a comment |
try to display and die your $id on the edit method. seems that the id is null.
– Dearwolves
Nov 21 '18 at 2:45
try to display and die your $id on the edit method. seems that the id is null.
– Dearwolves
Nov 21 '18 at 2:45
try to display and die your $id on the edit method. seems that the id is null.
– Dearwolves
Nov 21 '18 at 2:45
add a comment |
2 Answers
2
active
oldest
votes
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.
im already follow your answer.. but i still get error Property [id] does not exist on this collection instance.
– IKetut Widhi Adnyana
Nov 21 '18 at 6:17
@IKetutWidhiAdnyana Please post your full error here. To help you better
– Dearwolves
Nov 21 '18 at 7:10
@IKetutWidhiAdnyana are you sure you have and id field on your database?
– Dearwolves
Nov 21 '18 at 7:12
add a comment |
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');
}
thanks for advance.. i solved this error
– IKetut Widhi Adnyana
Nov 21 '18 at 9:03
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
im already follow your answer.. but i still get error Property [id] does not exist on this collection instance.
– IKetut Widhi Adnyana
Nov 21 '18 at 6:17
@IKetutWidhiAdnyana Please post your full error here. To help you better
– Dearwolves
Nov 21 '18 at 7:10
@IKetutWidhiAdnyana are you sure you have and id field on your database?
– Dearwolves
Nov 21 '18 at 7:12
add a comment |
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.
im already follow your answer.. but i still get error Property [id] does not exist on this collection instance.
– IKetut Widhi Adnyana
Nov 21 '18 at 6:17
@IKetutWidhiAdnyana Please post your full error here. To help you better
– Dearwolves
Nov 21 '18 at 7:10
@IKetutWidhiAdnyana are you sure you have and id field on your database?
– Dearwolves
Nov 21 '18 at 7:12
add a comment |
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.
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.
answered Nov 21 '18 at 2:48


DearwolvesDearwolves
315213
315213
im already follow your answer.. but i still get error Property [id] does not exist on this collection instance.
– IKetut Widhi Adnyana
Nov 21 '18 at 6:17
@IKetutWidhiAdnyana Please post your full error here. To help you better
– Dearwolves
Nov 21 '18 at 7:10
@IKetutWidhiAdnyana are you sure you have and id field on your database?
– Dearwolves
Nov 21 '18 at 7:12
add a comment |
im already follow your answer.. but i still get error Property [id] does not exist on this collection instance.
– IKetut Widhi Adnyana
Nov 21 '18 at 6:17
@IKetutWidhiAdnyana Please post your full error here. To help you better
– Dearwolves
Nov 21 '18 at 7:10
@IKetutWidhiAdnyana are you sure you have and id field on your database?
– Dearwolves
Nov 21 '18 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 '18 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 '18 at 6:17
@IKetutWidhiAdnyana Please post your full error here. To help you better
– Dearwolves
Nov 21 '18 at 7:10
@IKetutWidhiAdnyana Please post your full error here. To help you better
– Dearwolves
Nov 21 '18 at 7:10
@IKetutWidhiAdnyana are you sure you have and id field on your database?
– Dearwolves
Nov 21 '18 at 7:12
@IKetutWidhiAdnyana are you sure you have and id field on your database?
– Dearwolves
Nov 21 '18 at 7:12
add a comment |
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');
}
thanks for advance.. i solved this error
– IKetut Widhi Adnyana
Nov 21 '18 at 9:03
add a comment |
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');
}
thanks for advance.. i solved this error
– IKetut Widhi Adnyana
Nov 21 '18 at 9:03
add a comment |
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');
}
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');
}
edited Nov 21 '18 at 9:04
answered Nov 21 '18 at 6:43
IndianCodingIndianCoding
1,044119
1,044119
thanks for advance.. i solved this error
– IKetut Widhi Adnyana
Nov 21 '18 at 9:03
add a comment |
thanks for advance.. i solved this error
– IKetut Widhi Adnyana
Nov 21 '18 at 9:03
thanks for advance.. i solved this error
– IKetut Widhi Adnyana
Nov 21 '18 at 9:03
thanks for advance.. i solved this error
– IKetut Widhi Adnyana
Nov 21 '18 at 9:03
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
try to display and die your $id on the edit method. seems that the id is null.
– Dearwolves
Nov 21 '18 at 2:45