Laravel 5.7 Eloquent Relation for listing
i am trying to get dataset with join, and i wanna do it using eloquent and automatically without no manual query. I did the relations too but when i try to list the data joined, somehow it does it otherway around.
Laravel version 5.7
MySQL database
UPDATED, added the database schema.
my_tab3s table:
mytab1 table:
Error:
"SQLSTATE[42S22]: Column not found: 1054 Unknown column
'myTab1.my_tab3_id' in 'where clause' (SQL: select * frommyTab1
wheremyTab1
.my_tab3_id
in (1, 2))"
myTab3.php
<?php
namespace App;
use IlluminateDatabaseEloquentModel;
class myTab3 extends Model
{
//
protected $table = 'my_tab3s';
public $timestamps = true;
protected $fillable = [
'coolField',
'muhCurrentDate',
'rast',
'myTab1_id'
];
public function myTab1(){
return $this->hasOne('AppmyTab1');
}
}
myTab1.php
<?php
namespace App;
use IlluminateDatabaseEloquentModel;
class myTab1 extends Model
{
//
public function scopeBigger($query){
return $query->where('id','>','1');
}
public function scopeHasLetterZ($query){
return $query->where('someField','like','%z%');
}
public function gimmeAll(){
return myTab1::All();
}
protected $table = 'myTab1';
public $timestamps = true;
protected $fillable = [
'someField'
];
public function myTab3(){
return $this->belongsTo('AppmyTab3');
}
}
myTab3Controller.php INDEX method
public function index()
{
/*$dataSet = myTab3::All();*/
//$dataSet->myTab1()->get();
$dataSet = myTab3::with('myTab1')->get();
return view('myTab3.index',compact('dataSet'));
}
view part
@foreach($dataSet as $data)
<tr>
<td>{{$data->id}}</td>
<td>{{$data->coolField}}</td>
<td>{{$data->muhCurrentDate}}</td>
<td>{{$data->created_at}}</td>
<td>{{$data->updated_at}}</td>
<td>{{$data->rast}}</td>
<td>{{$data->myTab1->someField}}</td>
</tr>
@endforeach
php mysql laravel eloquent eloquent--relationship
add a comment |
i am trying to get dataset with join, and i wanna do it using eloquent and automatically without no manual query. I did the relations too but when i try to list the data joined, somehow it does it otherway around.
Laravel version 5.7
MySQL database
UPDATED, added the database schema.
my_tab3s table:
mytab1 table:
Error:
"SQLSTATE[42S22]: Column not found: 1054 Unknown column
'myTab1.my_tab3_id' in 'where clause' (SQL: select * frommyTab1
wheremyTab1
.my_tab3_id
in (1, 2))"
myTab3.php
<?php
namespace App;
use IlluminateDatabaseEloquentModel;
class myTab3 extends Model
{
//
protected $table = 'my_tab3s';
public $timestamps = true;
protected $fillable = [
'coolField',
'muhCurrentDate',
'rast',
'myTab1_id'
];
public function myTab1(){
return $this->hasOne('AppmyTab1');
}
}
myTab1.php
<?php
namespace App;
use IlluminateDatabaseEloquentModel;
class myTab1 extends Model
{
//
public function scopeBigger($query){
return $query->where('id','>','1');
}
public function scopeHasLetterZ($query){
return $query->where('someField','like','%z%');
}
public function gimmeAll(){
return myTab1::All();
}
protected $table = 'myTab1';
public $timestamps = true;
protected $fillable = [
'someField'
];
public function myTab3(){
return $this->belongsTo('AppmyTab3');
}
}
myTab3Controller.php INDEX method
public function index()
{
/*$dataSet = myTab3::All();*/
//$dataSet->myTab1()->get();
$dataSet = myTab3::with('myTab1')->get();
return view('myTab3.index',compact('dataSet'));
}
view part
@foreach($dataSet as $data)
<tr>
<td>{{$data->id}}</td>
<td>{{$data->coolField}}</td>
<td>{{$data->muhCurrentDate}}</td>
<td>{{$data->created_at}}</td>
<td>{{$data->updated_at}}</td>
<td>{{$data->rast}}</td>
<td>{{$data->myTab1->someField}}</td>
</tr>
@endforeach
php mysql laravel eloquent eloquent--relationship
what are the columns onmyTab1
?
– Bagus Tesa
Jan 3 at 1:01
table columns has been added
– Skywarth
Jan 3 at 1:12
add a comment |
i am trying to get dataset with join, and i wanna do it using eloquent and automatically without no manual query. I did the relations too but when i try to list the data joined, somehow it does it otherway around.
Laravel version 5.7
MySQL database
UPDATED, added the database schema.
my_tab3s table:
mytab1 table:
Error:
"SQLSTATE[42S22]: Column not found: 1054 Unknown column
'myTab1.my_tab3_id' in 'where clause' (SQL: select * frommyTab1
wheremyTab1
.my_tab3_id
in (1, 2))"
myTab3.php
<?php
namespace App;
use IlluminateDatabaseEloquentModel;
class myTab3 extends Model
{
//
protected $table = 'my_tab3s';
public $timestamps = true;
protected $fillable = [
'coolField',
'muhCurrentDate',
'rast',
'myTab1_id'
];
public function myTab1(){
return $this->hasOne('AppmyTab1');
}
}
myTab1.php
<?php
namespace App;
use IlluminateDatabaseEloquentModel;
class myTab1 extends Model
{
//
public function scopeBigger($query){
return $query->where('id','>','1');
}
public function scopeHasLetterZ($query){
return $query->where('someField','like','%z%');
}
public function gimmeAll(){
return myTab1::All();
}
protected $table = 'myTab1';
public $timestamps = true;
protected $fillable = [
'someField'
];
public function myTab3(){
return $this->belongsTo('AppmyTab3');
}
}
myTab3Controller.php INDEX method
public function index()
{
/*$dataSet = myTab3::All();*/
//$dataSet->myTab1()->get();
$dataSet = myTab3::with('myTab1')->get();
return view('myTab3.index',compact('dataSet'));
}
view part
@foreach($dataSet as $data)
<tr>
<td>{{$data->id}}</td>
<td>{{$data->coolField}}</td>
<td>{{$data->muhCurrentDate}}</td>
<td>{{$data->created_at}}</td>
<td>{{$data->updated_at}}</td>
<td>{{$data->rast}}</td>
<td>{{$data->myTab1->someField}}</td>
</tr>
@endforeach
php mysql laravel eloquent eloquent--relationship
i am trying to get dataset with join, and i wanna do it using eloquent and automatically without no manual query. I did the relations too but when i try to list the data joined, somehow it does it otherway around.
Laravel version 5.7
MySQL database
UPDATED, added the database schema.
my_tab3s table:
mytab1 table:
Error:
"SQLSTATE[42S22]: Column not found: 1054 Unknown column
'myTab1.my_tab3_id' in 'where clause' (SQL: select * frommyTab1
wheremyTab1
.my_tab3_id
in (1, 2))"
myTab3.php
<?php
namespace App;
use IlluminateDatabaseEloquentModel;
class myTab3 extends Model
{
//
protected $table = 'my_tab3s';
public $timestamps = true;
protected $fillable = [
'coolField',
'muhCurrentDate',
'rast',
'myTab1_id'
];
public function myTab1(){
return $this->hasOne('AppmyTab1');
}
}
myTab1.php
<?php
namespace App;
use IlluminateDatabaseEloquentModel;
class myTab1 extends Model
{
//
public function scopeBigger($query){
return $query->where('id','>','1');
}
public function scopeHasLetterZ($query){
return $query->where('someField','like','%z%');
}
public function gimmeAll(){
return myTab1::All();
}
protected $table = 'myTab1';
public $timestamps = true;
protected $fillable = [
'someField'
];
public function myTab3(){
return $this->belongsTo('AppmyTab3');
}
}
myTab3Controller.php INDEX method
public function index()
{
/*$dataSet = myTab3::All();*/
//$dataSet->myTab1()->get();
$dataSet = myTab3::with('myTab1')->get();
return view('myTab3.index',compact('dataSet'));
}
view part
@foreach($dataSet as $data)
<tr>
<td>{{$data->id}}</td>
<td>{{$data->coolField}}</td>
<td>{{$data->muhCurrentDate}}</td>
<td>{{$data->created_at}}</td>
<td>{{$data->updated_at}}</td>
<td>{{$data->rast}}</td>
<td>{{$data->myTab1->someField}}</td>
</tr>
@endforeach
php mysql laravel eloquent eloquent--relationship
php mysql laravel eloquent eloquent--relationship
edited Jan 3 at 8:41
Bagus Tesa
54411126
54411126
asked Jan 3 at 0:50
SkywarthSkywarth
417
417
what are the columns onmyTab1
?
– Bagus Tesa
Jan 3 at 1:01
table columns has been added
– Skywarth
Jan 3 at 1:12
add a comment |
what are the columns onmyTab1
?
– Bagus Tesa
Jan 3 at 1:01
table columns has been added
– Skywarth
Jan 3 at 1:12
what are the columns on
myTab1
?– Bagus Tesa
Jan 3 at 1:01
what are the columns on
myTab1
?– Bagus Tesa
Jan 3 at 1:01
table columns has been added
– Skywarth
Jan 3 at 1:12
table columns has been added
– Skywarth
Jan 3 at 1:12
add a comment |
1 Answer
1
active
oldest
votes
It's hard to say without the database but i think you reverse your relationships, it shoud be a belongsTo
for myTab3
and hasOne
for myTab1
EDIT:
try with :
public function myTab1(){
return $this->belongsTo('AppmyTab1', 'myTab1_id');
}
Almost worked, but now i got "Trying to get property 'someField' of non-object" error. Is it because i use {{$data->myTab1->someField}} ?
– Skywarth
Jan 3 at 1:17
@Skywarth, try todd($data)
and inspect whether themyTab3
is defined for all of the objects, otherwise you need to checkmyTab3
field for null before{{
printing}}
them.
– Bagus Tesa
Jan 3 at 1:21
@Louis R And with that, it returns me a result set like this: {"id":2,"coolField":"rtryrtyrtyty","muhCurrentDate":"2019-01-03 00:12:19","created_at":"2019-01-03 00:12:19","updated_at":"2019-01-03 00:12:19","rast":3,"myTab1_id":3,"my_tab1":null}] which indicate that relation is not done by looking at the nulls.
– Skywarth
Jan 3 at 1:21
@Louis R, yes seems like to do the magic, one of the row from the returning row is like that: {"id":2,"coolField":"rtryrtyrtyty","muhCurrentDate":"2019-01-03 00:12:19","created_at":"2019-01-03 00:12:19","updated_at":"2019-01-03 00:12:19","rast":3,"myTab1_id":3,"my_tab1":{"id":3,"someField":"zuhahahaha","updated_at":"2019-01-02 04:00:15","created_at":"2019-01-02 04:00:15"}}] But the problem is, how can i get that value for listing(in the foreach) ?
– Skywarth
Jan 3 at 1:38
Ok, figured out how to list. Thanks for the solution.
– Skywarth
Jan 3 at 2:10
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%2f54015042%2flaravel-5-7-eloquent-relation-for-listing%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
It's hard to say without the database but i think you reverse your relationships, it shoud be a belongsTo
for myTab3
and hasOne
for myTab1
EDIT:
try with :
public function myTab1(){
return $this->belongsTo('AppmyTab1', 'myTab1_id');
}
Almost worked, but now i got "Trying to get property 'someField' of non-object" error. Is it because i use {{$data->myTab1->someField}} ?
– Skywarth
Jan 3 at 1:17
@Skywarth, try todd($data)
and inspect whether themyTab3
is defined for all of the objects, otherwise you need to checkmyTab3
field for null before{{
printing}}
them.
– Bagus Tesa
Jan 3 at 1:21
@Louis R And with that, it returns me a result set like this: {"id":2,"coolField":"rtryrtyrtyty","muhCurrentDate":"2019-01-03 00:12:19","created_at":"2019-01-03 00:12:19","updated_at":"2019-01-03 00:12:19","rast":3,"myTab1_id":3,"my_tab1":null}] which indicate that relation is not done by looking at the nulls.
– Skywarth
Jan 3 at 1:21
@Louis R, yes seems like to do the magic, one of the row from the returning row is like that: {"id":2,"coolField":"rtryrtyrtyty","muhCurrentDate":"2019-01-03 00:12:19","created_at":"2019-01-03 00:12:19","updated_at":"2019-01-03 00:12:19","rast":3,"myTab1_id":3,"my_tab1":{"id":3,"someField":"zuhahahaha","updated_at":"2019-01-02 04:00:15","created_at":"2019-01-02 04:00:15"}}] But the problem is, how can i get that value for listing(in the foreach) ?
– Skywarth
Jan 3 at 1:38
Ok, figured out how to list. Thanks for the solution.
– Skywarth
Jan 3 at 2:10
add a comment |
It's hard to say without the database but i think you reverse your relationships, it shoud be a belongsTo
for myTab3
and hasOne
for myTab1
EDIT:
try with :
public function myTab1(){
return $this->belongsTo('AppmyTab1', 'myTab1_id');
}
Almost worked, but now i got "Trying to get property 'someField' of non-object" error. Is it because i use {{$data->myTab1->someField}} ?
– Skywarth
Jan 3 at 1:17
@Skywarth, try todd($data)
and inspect whether themyTab3
is defined for all of the objects, otherwise you need to checkmyTab3
field for null before{{
printing}}
them.
– Bagus Tesa
Jan 3 at 1:21
@Louis R And with that, it returns me a result set like this: {"id":2,"coolField":"rtryrtyrtyty","muhCurrentDate":"2019-01-03 00:12:19","created_at":"2019-01-03 00:12:19","updated_at":"2019-01-03 00:12:19","rast":3,"myTab1_id":3,"my_tab1":null}] which indicate that relation is not done by looking at the nulls.
– Skywarth
Jan 3 at 1:21
@Louis R, yes seems like to do the magic, one of the row from the returning row is like that: {"id":2,"coolField":"rtryrtyrtyty","muhCurrentDate":"2019-01-03 00:12:19","created_at":"2019-01-03 00:12:19","updated_at":"2019-01-03 00:12:19","rast":3,"myTab1_id":3,"my_tab1":{"id":3,"someField":"zuhahahaha","updated_at":"2019-01-02 04:00:15","created_at":"2019-01-02 04:00:15"}}] But the problem is, how can i get that value for listing(in the foreach) ?
– Skywarth
Jan 3 at 1:38
Ok, figured out how to list. Thanks for the solution.
– Skywarth
Jan 3 at 2:10
add a comment |
It's hard to say without the database but i think you reverse your relationships, it shoud be a belongsTo
for myTab3
and hasOne
for myTab1
EDIT:
try with :
public function myTab1(){
return $this->belongsTo('AppmyTab1', 'myTab1_id');
}
It's hard to say without the database but i think you reverse your relationships, it shoud be a belongsTo
for myTab3
and hasOne
for myTab1
EDIT:
try with :
public function myTab1(){
return $this->belongsTo('AppmyTab1', 'myTab1_id');
}
edited Jan 3 at 1:33
answered Jan 3 at 1:02
Louis RLouis R
554213
554213
Almost worked, but now i got "Trying to get property 'someField' of non-object" error. Is it because i use {{$data->myTab1->someField}} ?
– Skywarth
Jan 3 at 1:17
@Skywarth, try todd($data)
and inspect whether themyTab3
is defined for all of the objects, otherwise you need to checkmyTab3
field for null before{{
printing}}
them.
– Bagus Tesa
Jan 3 at 1:21
@Louis R And with that, it returns me a result set like this: {"id":2,"coolField":"rtryrtyrtyty","muhCurrentDate":"2019-01-03 00:12:19","created_at":"2019-01-03 00:12:19","updated_at":"2019-01-03 00:12:19","rast":3,"myTab1_id":3,"my_tab1":null}] which indicate that relation is not done by looking at the nulls.
– Skywarth
Jan 3 at 1:21
@Louis R, yes seems like to do the magic, one of the row from the returning row is like that: {"id":2,"coolField":"rtryrtyrtyty","muhCurrentDate":"2019-01-03 00:12:19","created_at":"2019-01-03 00:12:19","updated_at":"2019-01-03 00:12:19","rast":3,"myTab1_id":3,"my_tab1":{"id":3,"someField":"zuhahahaha","updated_at":"2019-01-02 04:00:15","created_at":"2019-01-02 04:00:15"}}] But the problem is, how can i get that value for listing(in the foreach) ?
– Skywarth
Jan 3 at 1:38
Ok, figured out how to list. Thanks for the solution.
– Skywarth
Jan 3 at 2:10
add a comment |
Almost worked, but now i got "Trying to get property 'someField' of non-object" error. Is it because i use {{$data->myTab1->someField}} ?
– Skywarth
Jan 3 at 1:17
@Skywarth, try todd($data)
and inspect whether themyTab3
is defined for all of the objects, otherwise you need to checkmyTab3
field for null before{{
printing}}
them.
– Bagus Tesa
Jan 3 at 1:21
@Louis R And with that, it returns me a result set like this: {"id":2,"coolField":"rtryrtyrtyty","muhCurrentDate":"2019-01-03 00:12:19","created_at":"2019-01-03 00:12:19","updated_at":"2019-01-03 00:12:19","rast":3,"myTab1_id":3,"my_tab1":null}] which indicate that relation is not done by looking at the nulls.
– Skywarth
Jan 3 at 1:21
@Louis R, yes seems like to do the magic, one of the row from the returning row is like that: {"id":2,"coolField":"rtryrtyrtyty","muhCurrentDate":"2019-01-03 00:12:19","created_at":"2019-01-03 00:12:19","updated_at":"2019-01-03 00:12:19","rast":3,"myTab1_id":3,"my_tab1":{"id":3,"someField":"zuhahahaha","updated_at":"2019-01-02 04:00:15","created_at":"2019-01-02 04:00:15"}}] But the problem is, how can i get that value for listing(in the foreach) ?
– Skywarth
Jan 3 at 1:38
Ok, figured out how to list. Thanks for the solution.
– Skywarth
Jan 3 at 2:10
Almost worked, but now i got "Trying to get property 'someField' of non-object" error. Is it because i use {{$data->myTab1->someField}} ?
– Skywarth
Jan 3 at 1:17
Almost worked, but now i got "Trying to get property 'someField' of non-object" error. Is it because i use {{$data->myTab1->someField}} ?
– Skywarth
Jan 3 at 1:17
@Skywarth, try to
dd($data)
and inspect whether the myTab3
is defined for all of the objects, otherwise you need to check myTab3
field for null before {{
printing}}
them.– Bagus Tesa
Jan 3 at 1:21
@Skywarth, try to
dd($data)
and inspect whether the myTab3
is defined for all of the objects, otherwise you need to check myTab3
field for null before {{
printing}}
them.– Bagus Tesa
Jan 3 at 1:21
@Louis R And with that, it returns me a result set like this: {"id":2,"coolField":"rtryrtyrtyty","muhCurrentDate":"2019-01-03 00:12:19","created_at":"2019-01-03 00:12:19","updated_at":"2019-01-03 00:12:19","rast":3,"myTab1_id":3,"my_tab1":null}] which indicate that relation is not done by looking at the nulls.
– Skywarth
Jan 3 at 1:21
@Louis R And with that, it returns me a result set like this: {"id":2,"coolField":"rtryrtyrtyty","muhCurrentDate":"2019-01-03 00:12:19","created_at":"2019-01-03 00:12:19","updated_at":"2019-01-03 00:12:19","rast":3,"myTab1_id":3,"my_tab1":null}] which indicate that relation is not done by looking at the nulls.
– Skywarth
Jan 3 at 1:21
@Louis R, yes seems like to do the magic, one of the row from the returning row is like that: {"id":2,"coolField":"rtryrtyrtyty","muhCurrentDate":"2019-01-03 00:12:19","created_at":"2019-01-03 00:12:19","updated_at":"2019-01-03 00:12:19","rast":3,"myTab1_id":3,"my_tab1":{"id":3,"someField":"zuhahahaha","updated_at":"2019-01-02 04:00:15","created_at":"2019-01-02 04:00:15"}}] But the problem is, how can i get that value for listing(in the foreach) ?
– Skywarth
Jan 3 at 1:38
@Louis R, yes seems like to do the magic, one of the row from the returning row is like that: {"id":2,"coolField":"rtryrtyrtyty","muhCurrentDate":"2019-01-03 00:12:19","created_at":"2019-01-03 00:12:19","updated_at":"2019-01-03 00:12:19","rast":3,"myTab1_id":3,"my_tab1":{"id":3,"someField":"zuhahahaha","updated_at":"2019-01-02 04:00:15","created_at":"2019-01-02 04:00:15"}}] But the problem is, how can i get that value for listing(in the foreach) ?
– Skywarth
Jan 3 at 1:38
Ok, figured out how to list. Thanks for the solution.
– Skywarth
Jan 3 at 2:10
Ok, figured out how to list. Thanks for the solution.
– Skywarth
Jan 3 at 2:10
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%2f54015042%2flaravel-5-7-eloquent-relation-for-listing%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
what are the columns on
myTab1
?– Bagus Tesa
Jan 3 at 1:01
table columns has been added
– Skywarth
Jan 3 at 1:12