Class Comment not found Laravel 4 - php

Im trying to add a comment system to my laravel app.
But I can't seem to get it working.
I have two models
class Post extends \Eloquent {
protected $table = 'posts';
public function comments()
{
return $this->hasMany('Comment','postId');
}
}
and my Comment model
class Comment extends \Eloquent {
protected $table = 'comments';
public function post()
{
return $this->belongsTo('Post');
}
}
in my DashBoardController I'm trying to get the output from the models
use App\Models\Post;
use App\Models\Comment;
use Input, Redirect, Sentry, Str, View, Notification;
class DashboardController extends \BaseController {
public function index()
{
$post = Post::find(3)->comments()->comment;
print_r($post);die;
}
}
I think my database is properly linked, but now I'm getting the error
'Class Comment not found'.
Any advice on this one?

First try this: composer dump-auto (as commented by user1669496)
if this didn't helped then change your model...
Change this:
return $this->belongsTo('Post');
to smth like this:
$this->belongsTo('App\Models\Post');
Do the similar for Post model.
Just change App\Models\XXXX to your namespace where you have Post model saved.
I had similar problem and this helped me, hope it will help you.

Related

404 not found Laravel with route argument

I come to you today because I don't really understand where my problem is coming from. When I try to pass a dynamic argument in my url, I systematically fall on a 404 page. I looked in my db, there is the article, I looked in my model, it points well to this table of the db, I looked at my controller, I specify this article well... I do not understand too much.
My web.php:
Route::get('/article/{article}', 'ArticleController#index');
My controller:
class ArticleController extends Controller
{
public function index($article)
{
$scopedArticle = Article::where('id', $article)->firstOrFail();
return view('article', compact('scopedArticle'));
}
}
My model:
class Article extends Model
{
protected $table = 'articles';
protected $fillable = [
'titre', 'image', 'description',
];
}
My database:
So I don't really understand...
With route model binding there is no need to add $scopedArticle = Article::where('id', $article)->firstOrFail();
Example usage:
goto http://yourstie.com/article/1
class ArticleController extends Controller
{
public function index(Article $article)
{
return view('article', compact('article'));
}
}

Problem of Class not found on Laravel for a form

I tried to follow this tutorial to make a work on Laravel : https://vegibit.com/how-to-set-up-form-submission-in-laravel/ but I got a strange bug and I can't found a way to fix it, I wanted to make a form to insert something in my database and show it in another page (the showing is right, it's just the form page that get this problem) here is a screenshot :
Code :
namespace Doreas\Http\Controllers;
use Illuminate\Http\Request;
use App\Demand;
class DemandeController extends Controller
{
public function index()
{
$demande = Demand::all();
return view('demande.index', ['demande' => $demande]);
}
public function show($id)
{
$demande = Demande::find($id);
return view('demande.show', ['demande' => $demande]);
}
public function create()
{
return 'it works';
}
}
I don't understand where the problem of Demand come from
<?php
namespace App\Doreas;
use Illuminate\Database\Eloquent\Model;
class Demand extends Model
{
public function scopeTest($query)
{
return $query->where('title', '=', 'test');
}
}
Where I declare the class
The problem is that you have imported the class Demand using use App\Demand;
However its namespace is actually App\Doreas so you should do use App\Doreas\Demand;

Load extra table/data into user object

In Laravel 5.6 I'm trying to load data into the user object, so I can view user credentials/settings etc.
Whats annoying is I had it working, but for some reason now It seems to have stopped, and I'm not sure what I've changed to break it.
Anyway I want to load two tables, access and settings. Both of them have user_id field in there with the corresponding user_id in.
In my User.php class I have two functions:
public function access() {
return $this->hasMany(Access::class);
}
public function settings() {
return $this->hasOne(Settings::class);
}
I am not Use-ing them at the top of the class (i.e. use \App\Access) if that makes any difference.
And then the Access class looks like:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Access extends Model
{
protected $table = "access";
}
And the Settings class is very much the same:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Settings extends Model
{
protected $table = "settings";
}
However whenever I try and access Auth::user()->settings or Auth::user()->access I get undefined index: error. It's frustrating because like I said I had it working the other day and I'm not sure what's changed.
Few things you could try here. First, Lazy Eager Load the relationships by loadMissing:
// settings
Auth::user()->loadMissing('settings');
// access
Auth::user()->loadMissing('access');
To load a relationship only when it has not already been loaded, use the loadMissing method
Second, you can use with when querying for a user, although it's not as relevant with using the auth facade:
User::with(['settings', 'access'])->where('atribute', $value)->get();
Last, if you always want the settings and access relationships to always be returned with each user model, set the with attribute on the user model:
public class User {
protected $with = ['settings', 'access'];
...
}
I usually define the inverse relationships on models as well, so Access and Settings would have a BelongsTo relationship defined:
class Access extends Model
{
protected $table = "access";
public function user() {
return $this->belongsTo(User::class);
}
}
class Settings extends Model
{
protected $table = "settings";
public function user() {
return $this->belongsTo(User::class);
}
}

Laravel 5.5 MassAssignmentException

I'm following the Laravel From Scratch tutorial series, I'm currently at the part that you are creating a comment system for your articles system. But I'm having a problem, I don't really know what the error is saying at this point.
The error:
Illuminate\Database\Eloquent\MassAssignmentException
body
The comment model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Comment extends Model
{
public function post()
{
return $this->belongsTo(Post::class);
}
}
The post model:
<?php
namespace App;
class Post extends Model
{
public function comments()
{
return $this->hasMany(Comment::class);
}
public function addComment($body)
{
$this->comments()->create(compact('body'));
}
}
The route I made:
Route::post('/posts/{post}/comments', 'CommentsController#store');
The comments controller:
<?php
namespace App\Http\Controllers;
use App\Post;
class CommentsController extends Controller
{
public function store(Post $post)
{
$post->addComment(request('body'));
return back();
}
}
Thanks in advance!
Explanation of this error
This is a security feature of Laravel. It is designed to protect you against form manipulation when using mass assignments.
For example on a sign-up form: When you have an is_admin column in your database, a user simply could manipulate your form to set is_admin to true on your server, and therefore in your database. This security feature prevents that by using a whitelist to define safe fields.
How to fix that
You need to set a $fillable property on your model. It's value must be an array containing all fields that are safe to mass assignable (like username, email address, ...).
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Comment extends Model
{
# This property!
protected $fillable = ['body'];
// ...
}
See "Mass assignment" in the docs:
https://laravel.com/docs/5.5/eloquent#mass-assignment
Mass assignment is when you send an array to the model creation, basically setting a bunch of fields on the model in a single go, rather than one by one, something like what you did here:
public function addComment($body)
{
$this->comments()->create(compact('body'));
}
You need to add the field you are populating to the fillable array in Comments.php model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Comment extends Model
{
protected $fillable = ['body'];
public function post()
{
return $this->belongsTo(Post::class);
}
}
As the documentation states:
You may also use the create method to save a new model in a single
line. The inserted model instance will be returned to you from the
method. However, before doing so, you will need to specify either a
fillable or guarded attribute on the model, as all Eloquent models
protect against mass-assignment by default.
Hope this helps you.

Call to undefined method Illuminate\Database\Query\Builder::groups() eager loading

I am creating a website using laravel. I have a small issue with eager loading. I have already made several websites with laravel, but still I can't find what is wrong here.
This is my config model:
<?php
class Config extends \Eloquent {
protected $table = "configs";
public function groups() {
return $this->hasMany('ConfigOptionGroup', 'config_id');
}
}
And this is my testcontroller class:
<?php
namespace WebsiteController;
class DemoController extends \BaseController {
public function getTest() {
$c = \Config::where('id', 1)->with(['groups' => function($q){
$q->whereNull('config_option_group_id');
}])->first();
return $c;
}
}
Whenever I surf to the url that calls the getTest method I get an error saying Call to undefined method Illuminate\Database\Query\Builder::groups(). However, the function groups() exists in the Config model.
When I remove the with() function from the query, it works just fine. But I can never load the groups via the relation.
Is there anyone who can help me with this problem?
Update: I have removed the Config facade by commenting out the line in /app/config/app.php.

Categories