skip to Main Content

I am trying to read data from a local collections from my mongodb database. Name of database Intuit name of collection post.
Code inside PostController:

<?php
use IlluminateHttpRequest;
use AppModelsPost;
class PostController extends Controller{

public function home() {
    $posts = Post::with(['user'])->get();

    return view('home', ['posts' => $posts->toArray()]);
}}

Code inside Post:

class Post extends Model{
use HasFactory;

protected $fillable = [
'STATUS',

'DDC_CODE',

'TRADE_NAME',

'SCIENTIFIC_CODE',

'SCIENTIFIC_NAME',

'INGREDIENT_STRENGTH',

'DOSAGE_FORM_PACKAGE',

'ROUTE_OF_ADMIN',
'PACKAGE_PRICE',

'GRANULAR_UNIT',

'MANUFACTURER',

'REGISTERED_OWNER',

'UPDATED_DATE',

'SOURCE'
];}

All of the above are the titles of the columns in the table.
Next home.blade.php:

<!DOCTPE html>
<html>
<head>
<title>View Records</title>
</head>
<body>
<table border = "1">
<tr>
<td>Id</td>
<td>STATUS</td>
<td>DDC_CODE</td>
<td>TRADE_NAME</td>
<td>SCIENTIFIC_CODE</td>
<td>SCIENTIFIC_NAME</td>
<td>SCIENTIFIC_CODE</td>
<td>INGREDIENT_STRENGTH</td>
<td>DOSAGE_FORM_PACKAGE</td>
<td>ROUTE_OF_ADMIN</td>
<td>PACKAGE_PRICE</td>
<td>GRANULAR_UNIT</td>
<td>MANUFACTURER</td>
<td>UPDATED_DATE</td>
<td>SOURCE</td>

</tr>
@foreach ($posts as $post)
<tr>
<td>{{ $post->_id }}</td>
<td>{{ $post->STATUS }}</td>
<td>{{ $post->DDC_CODE }}</td>
<td>{{ $post->TRADE_NAME }}</td>
<td>{{ $post->SCIENTIFIC_CODE }}</td>
<td>{{ $post->SCIENTIFIC_NAME }}</td>
<td>{{ $post->INGREDIENT_STRENGTH }}</td>
<td>{{ $post->DOSAGE_FORM_PACKAGE }}</td>
<td>{{ $post->ROUTE_OF_ADMIN }}</td>
<td>{{ $post->PACKAGE_PRICE }}</td>
<td>{{ $post->GRANULAR_UNIT }}</td>
<td>{{ $post->MANUFACTURER }}</td>
<td>{{ $post->REGISTERED_OWNER }}</td>
<td>{{ $post->UPDATED_DATE }}</td>
<td>{{ $post->SOURCE }}</td>
</tr>
@endforeach
</table>
</body>
</html>

The route handler is as follows

Route::get('view','AppHttpControllersPostController@home');

all of this seems to be fine but when I load up the application I see nothing but the column titles not sure what I am missing, I believe it is not working because the query in PostController is incorrect and need to change it as it is taken from another projec. Any and all help and suggestions are welcome.

3

Answers


  1. You are sending data as array, But you are trying to get them as object in your blade view file.

    Instead of getting data like this:

    $post->STATUS
    

    Try using like this:

    $post['STATUS']
    
    Login or Signup to reply.
  2. Don’t use ->toArray()

    Pass data as below & variables will get displayed

    return view('home', ['posts' => $posts)]);

    Login or Signup to reply.
  3. Instead of

    Post::with(['user'])->get();
    

    use

    Post::with(['user'])->all();
    

    subject to user relationship is properly defined in your Post model

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search