Display data from 2 table - Property [name] does not exist on this collection instance











up vote
0
down vote

favorite












I try display data from table Course and Test, the relation is one-to-many.



course name   | test name  
___________________________
course name1 | test name 1
course name2 | test name 2


etc.



public $table = 'Course';

public function test()
{
return $this->hasMany('AppTest');
}


public $table = 'Test';

protected $fillable = ['name',];

public function course()
{
return $this->belongsTo('AppCourse');
}


TestController.php



public function courses() {

$courses = Course::with(['test'])->get();,
return view('admin.test.list_course', [
'courses' => $courses,

]);
}


list_course.blade.php



 @foreach($courses as $course)

<tr>
<td>
{{ $course->name }}
</td>

<td>
{{ $course->test->name }}
</td>

</tr>
@endforeach


And I get error




Property [name] does not exist on this collection instance




Please, do you anybody know where is error? Thank you!!










share|improve this question
























  • And which line in your code causes this error
    – RiggsFolly
    Nov 8 at 12:13










  • in blade {{ $course->test->name }}
    – Sonya
    Nov 8 at 12:16










  • Have you tried $course->test['name'] or $course->{'test name'}?
    – Francesco Mineo
    Nov 8 at 12:17












  • yes, i got error syntax error, unexpected '[', expecting identifier (T_STRING) or variable (T_VARIABLE) or '{' or '$'
    – Sonya
    Nov 8 at 12:19










  • {'test name'} display empty rows
    – Sonya
    Nov 8 at 12:20















up vote
0
down vote

favorite












I try display data from table Course and Test, the relation is one-to-many.



course name   | test name  
___________________________
course name1 | test name 1
course name2 | test name 2


etc.



public $table = 'Course';

public function test()
{
return $this->hasMany('AppTest');
}


public $table = 'Test';

protected $fillable = ['name',];

public function course()
{
return $this->belongsTo('AppCourse');
}


TestController.php



public function courses() {

$courses = Course::with(['test'])->get();,
return view('admin.test.list_course', [
'courses' => $courses,

]);
}


list_course.blade.php



 @foreach($courses as $course)

<tr>
<td>
{{ $course->name }}
</td>

<td>
{{ $course->test->name }}
</td>

</tr>
@endforeach


And I get error




Property [name] does not exist on this collection instance




Please, do you anybody know where is error? Thank you!!










share|improve this question
























  • And which line in your code causes this error
    – RiggsFolly
    Nov 8 at 12:13










  • in blade {{ $course->test->name }}
    – Sonya
    Nov 8 at 12:16










  • Have you tried $course->test['name'] or $course->{'test name'}?
    – Francesco Mineo
    Nov 8 at 12:17












  • yes, i got error syntax error, unexpected '[', expecting identifier (T_STRING) or variable (T_VARIABLE) or '{' or '$'
    – Sonya
    Nov 8 at 12:19










  • {'test name'} display empty rows
    – Sonya
    Nov 8 at 12:20













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I try display data from table Course and Test, the relation is one-to-many.



course name   | test name  
___________________________
course name1 | test name 1
course name2 | test name 2


etc.



public $table = 'Course';

public function test()
{
return $this->hasMany('AppTest');
}


public $table = 'Test';

protected $fillable = ['name',];

public function course()
{
return $this->belongsTo('AppCourse');
}


TestController.php



public function courses() {

$courses = Course::with(['test'])->get();,
return view('admin.test.list_course', [
'courses' => $courses,

]);
}


list_course.blade.php



 @foreach($courses as $course)

<tr>
<td>
{{ $course->name }}
</td>

<td>
{{ $course->test->name }}
</td>

</tr>
@endforeach


And I get error




Property [name] does not exist on this collection instance




Please, do you anybody know where is error? Thank you!!










share|improve this question















I try display data from table Course and Test, the relation is one-to-many.



course name   | test name  
___________________________
course name1 | test name 1
course name2 | test name 2


etc.



public $table = 'Course';

public function test()
{
return $this->hasMany('AppTest');
}


public $table = 'Test';

protected $fillable = ['name',];

public function course()
{
return $this->belongsTo('AppCourse');
}


TestController.php



public function courses() {

$courses = Course::with(['test'])->get();,
return view('admin.test.list_course', [
'courses' => $courses,

]);
}


list_course.blade.php



 @foreach($courses as $course)

<tr>
<td>
{{ $course->name }}
</td>

<td>
{{ $course->test->name }}
</td>

</tr>
@endforeach


And I get error




Property [name] does not exist on this collection instance




Please, do you anybody know where is error? Thank you!!







php mysql laravel eloquent laravel-blade






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 8 at 12:11









RiggsFolly

68.6k1764109




68.6k1764109










asked Nov 8 at 12:05









Sonya

74




74












  • And which line in your code causes this error
    – RiggsFolly
    Nov 8 at 12:13










  • in blade {{ $course->test->name }}
    – Sonya
    Nov 8 at 12:16










  • Have you tried $course->test['name'] or $course->{'test name'}?
    – Francesco Mineo
    Nov 8 at 12:17












  • yes, i got error syntax error, unexpected '[', expecting identifier (T_STRING) or variable (T_VARIABLE) or '{' or '$'
    – Sonya
    Nov 8 at 12:19










  • {'test name'} display empty rows
    – Sonya
    Nov 8 at 12:20


















  • And which line in your code causes this error
    – RiggsFolly
    Nov 8 at 12:13










  • in blade {{ $course->test->name }}
    – Sonya
    Nov 8 at 12:16










  • Have you tried $course->test['name'] or $course->{'test name'}?
    – Francesco Mineo
    Nov 8 at 12:17












  • yes, i got error syntax error, unexpected '[', expecting identifier (T_STRING) or variable (T_VARIABLE) or '{' or '$'
    – Sonya
    Nov 8 at 12:19










  • {'test name'} display empty rows
    – Sonya
    Nov 8 at 12:20
















And which line in your code causes this error
– RiggsFolly
Nov 8 at 12:13




And which line in your code causes this error
– RiggsFolly
Nov 8 at 12:13












in blade {{ $course->test->name }}
– Sonya
Nov 8 at 12:16




in blade {{ $course->test->name }}
– Sonya
Nov 8 at 12:16












Have you tried $course->test['name'] or $course->{'test name'}?
– Francesco Mineo
Nov 8 at 12:17






Have you tried $course->test['name'] or $course->{'test name'}?
– Francesco Mineo
Nov 8 at 12:17














yes, i got error syntax error, unexpected '[', expecting identifier (T_STRING) or variable (T_VARIABLE) or '{' or '$'
– Sonya
Nov 8 at 12:19




yes, i got error syntax error, unexpected '[', expecting identifier (T_STRING) or variable (T_VARIABLE) or '{' or '$'
– Sonya
Nov 8 at 12:19












{'test name'} display empty rows
– Sonya
Nov 8 at 12:20




{'test name'} display empty rows
– Sonya
Nov 8 at 12:20












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










Your test relation on the Course Model is a one-to-many relation, meaning that there can be more than one test for every course, because of this the relation will return a collection of all tests belonging to the course.
To display it in the View you could loop through all tests:



@foreach($courses as $course)
<tr>
<td>
{{ $course->name }}
</td>
<td>
@foreach($course->test as $test)
{{ $test->name }} <br>
@endforeach
</td>
</tr>
@endforeach





share|improve this answer























  • wohooo... thank you! work perfectly
    – Sonya
    Nov 8 at 12:31










  • Can I ask you on more thing please.. I try display question count. course name | test name | question count . I have relation between Test and Question 1:N . how can I do this, pls? In my Testcontroller, i have $courses = Course::with(['test'])->get(); $questions = $courses->question;
    – Sonya
    Nov 8 at 13:36










  • Could you accept this answer and make a completely new question, so you can explain better what the new problem is.
    – Remul
    Nov 8 at 13:45













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',
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%2f53207405%2fdisplay-data-from-2-table-property-name-does-not-exist-on-this-collection-in%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








up vote
1
down vote



accepted










Your test relation on the Course Model is a one-to-many relation, meaning that there can be more than one test for every course, because of this the relation will return a collection of all tests belonging to the course.
To display it in the View you could loop through all tests:



@foreach($courses as $course)
<tr>
<td>
{{ $course->name }}
</td>
<td>
@foreach($course->test as $test)
{{ $test->name }} <br>
@endforeach
</td>
</tr>
@endforeach





share|improve this answer























  • wohooo... thank you! work perfectly
    – Sonya
    Nov 8 at 12:31










  • Can I ask you on more thing please.. I try display question count. course name | test name | question count . I have relation between Test and Question 1:N . how can I do this, pls? In my Testcontroller, i have $courses = Course::with(['test'])->get(); $questions = $courses->question;
    – Sonya
    Nov 8 at 13:36










  • Could you accept this answer and make a completely new question, so you can explain better what the new problem is.
    – Remul
    Nov 8 at 13:45

















up vote
1
down vote



accepted










Your test relation on the Course Model is a one-to-many relation, meaning that there can be more than one test for every course, because of this the relation will return a collection of all tests belonging to the course.
To display it in the View you could loop through all tests:



@foreach($courses as $course)
<tr>
<td>
{{ $course->name }}
</td>
<td>
@foreach($course->test as $test)
{{ $test->name }} <br>
@endforeach
</td>
</tr>
@endforeach





share|improve this answer























  • wohooo... thank you! work perfectly
    – Sonya
    Nov 8 at 12:31










  • Can I ask you on more thing please.. I try display question count. course name | test name | question count . I have relation between Test and Question 1:N . how can I do this, pls? In my Testcontroller, i have $courses = Course::with(['test'])->get(); $questions = $courses->question;
    – Sonya
    Nov 8 at 13:36










  • Could you accept this answer and make a completely new question, so you can explain better what the new problem is.
    – Remul
    Nov 8 at 13:45















up vote
1
down vote



accepted







up vote
1
down vote



accepted






Your test relation on the Course Model is a one-to-many relation, meaning that there can be more than one test for every course, because of this the relation will return a collection of all tests belonging to the course.
To display it in the View you could loop through all tests:



@foreach($courses as $course)
<tr>
<td>
{{ $course->name }}
</td>
<td>
@foreach($course->test as $test)
{{ $test->name }} <br>
@endforeach
</td>
</tr>
@endforeach





share|improve this answer














Your test relation on the Course Model is a one-to-many relation, meaning that there can be more than one test for every course, because of this the relation will return a collection of all tests belonging to the course.
To display it in the View you could loop through all tests:



@foreach($courses as $course)
<tr>
<td>
{{ $course->name }}
</td>
<td>
@foreach($course->test as $test)
{{ $test->name }} <br>
@endforeach
</td>
</tr>
@endforeach






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 8 at 12:36

























answered Nov 8 at 12:29









Remul

68212




68212












  • wohooo... thank you! work perfectly
    – Sonya
    Nov 8 at 12:31










  • Can I ask you on more thing please.. I try display question count. course name | test name | question count . I have relation between Test and Question 1:N . how can I do this, pls? In my Testcontroller, i have $courses = Course::with(['test'])->get(); $questions = $courses->question;
    – Sonya
    Nov 8 at 13:36










  • Could you accept this answer and make a completely new question, so you can explain better what the new problem is.
    – Remul
    Nov 8 at 13:45




















  • wohooo... thank you! work perfectly
    – Sonya
    Nov 8 at 12:31










  • Can I ask you on more thing please.. I try display question count. course name | test name | question count . I have relation between Test and Question 1:N . how can I do this, pls? In my Testcontroller, i have $courses = Course::with(['test'])->get(); $questions = $courses->question;
    – Sonya
    Nov 8 at 13:36










  • Could you accept this answer and make a completely new question, so you can explain better what the new problem is.
    – Remul
    Nov 8 at 13:45


















wohooo... thank you! work perfectly
– Sonya
Nov 8 at 12:31




wohooo... thank you! work perfectly
– Sonya
Nov 8 at 12:31












Can I ask you on more thing please.. I try display question count. course name | test name | question count . I have relation between Test and Question 1:N . how can I do this, pls? In my Testcontroller, i have $courses = Course::with(['test'])->get(); $questions = $courses->question;
– Sonya
Nov 8 at 13:36




Can I ask you on more thing please.. I try display question count. course name | test name | question count . I have relation between Test and Question 1:N . how can I do this, pls? In my Testcontroller, i have $courses = Course::with(['test'])->get(); $questions = $courses->question;
– Sonya
Nov 8 at 13:36












Could you accept this answer and make a completely new question, so you can explain better what the new problem is.
– Remul
Nov 8 at 13:45






Could you accept this answer and make a completely new question, so you can explain better what the new problem is.
– Remul
Nov 8 at 13:45




















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53207405%2fdisplay-data-from-2-table-property-name-does-not-exist-on-this-collection-in%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

Guess what letter conforming each word

Port of Spain

Run scheduled task as local user group (not BUILTIN)