Undefinded Index in Mongodb Array When Calling From Laravel

343 views Asked by At

I have Laravel set up with Moloquent in order to use a seperate Mongodb I have set up. I'm using the database Articles, which has a collection "articles", where each article is an array with info like title, url, image, etc. Some things I can pull just fine, like title and source, but if I try to call others (like url or content) i get and "Undefined index: url" error from Laravel.

use App\Articles;
$articles = App\Articles:all();

@foreach ($articles as $article)
<li>{{ $article['title'] }} - {{ $article['source'] }} </li> //this will work great
<li>{{ $article['title'] }} - {{ $article['url'] }} </li> //this will throw an error
@endforeach

Here's my Articles.php model:

<?php
namespace App;   
use Illuminate\Database\Eloquent\Model;
class Articles extends Model
{protected $table = 'articles';}

Also, if I just show $article (after $article = App\Articles:all();) I get all the information. Any ideas would be helpful.

Edit: here's the mongoose schema as well:

const mongoose = require('mongoose');
let articleSchema = new mongoose.Schema({
    title: String,
    article: String,
    image: String,
    source: String,
    url: String,
    categories: String,
    dateCrawled: Date,
    dateWritten: String
});
let Article = mongoose.model('Article', articleSchema);
module.exports = Article;
1

There are 1 answers

0
Alteredorange On BEST ANSWER

I ended up just switching to Postgres. Mongo was causing too many issues, and I was able to port everything over in just a couple hours.

If you're just getting started with Laravel I highly recommend using either MYSQL or Postgres since they work great right out of the box, and you shouldn't have any major issues (the above was only one MongoDB related issue, I've had quite a few more...)