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.
Related
I keep getting an error:
Call to undefined method App\Facebook::fbLogin()** when
**handleProviderCallback()
Controller
namespace App\Http\Controllers;
use App\Facebook;
class FacebookController extends Controller
{
public function handleProviderCallback()
{
$user = Socialite::driver('facebook')->user();
$route = Facebook::fbLogin($user);
return redirect()->route($route);
}
}
Model
namespace App;
class Facebook extends Model
{
public static function fbLogin($user){
.......
}
}
Already spent hours looking for a solution. Please help.
Delete the model and recreate it again. maybe some kind of indexing issue. other than this run following command in the composer.
composer dump-autoload
I'm working with too many mysql large views. I don't want to use Eloquent Model for the views.
I created "ViewBalance extends Illuminate\Support\Facades\DB". Everything worked as I wanted.
But i need to set init() method for company scope.
How can I use the global scope without init() method?
ViewModel
<?php
namespace App\Models\Views;
use App\Facades\CoreService;
use Illuminate\Support\Facades\DB;
class ViewBalance extends DB
{
const COMPANY_COLUMN = 'company_id';
const TABLE = 'view_balances';
public static function init()
{
return parent::table(self::COMPANY_COLUMN)
->where(self::COMPANY_COLUMN, CoreService::companyId());
}
}
In Controller
<?php
$data = ViewBalance::init()->get(); // Worked!
I have answered my own question. Because, I don't want to edit my question for more complicate. I want to talk about a solution to this problem.
I added $table_view variable and getView() method in Laravel model. If you want, you can create trait for clean codes.
It can be accessed easily views. Also it is part of the main model.
For example;
Laravel Basic Account Model
class Account extends Model {
protected $table = 'accounts';
protected $table_view = 'view_accounts';
public function getView()
{
return \DB::table($this->table_view)->where('global_scope', 1);
}
}
Laravel Account Controller
class AccountController extends Controller {
public function index()
{
$items = (new Account)->getView()->paginate(20);
}
}
public function scopeActive($query)
{
return $query->where('active', true);
}
or
public function scopeInactive($query)
{
return $query->where('active', false);
}
I have the following controllers:
class ProjectsController extends \BaseController {
public function tasks()
{
return $this->hasMany('Task');
}
}
class TasksController extends \BaseController {
public function project()
{
return $this->belongsTo('Project');
}
}
If I call $project->tasks->count() in my view, I get an error Call to a member function count() on a non-object
I'm following this tutorial on creating a todo list, and everything went well up until this point. I really hope someone can assist.
And then I ask myself.... why would you put that function in the Controller instead of the Model?
So, I moved the functions from the controllers to the models and all is well.
Sometimes it helps to switch one's brain on :) lol
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.
So, I'm getting a fatal error because the method is undefined when the controller calls the method. Though this is not true as the method is inside the classes model.
StudentsController.php
<?php
class StudentsController extends AppController{
public function index(){
$students = $this->Student->find('all');
$this->set('students', $students);
}
public function add(){
if($this->request->is('post')){
$this->formatData($this->request->data);
}
}
}
?>
And then my model:
Student.php (Model)
<?php
class Student extends AppModel{
var $studentData;
public function formatData($studentData){
if(is_null($studentData)){
return false;
}else{
echo $studentData;
}
}
}
?>
You're not invoking the method on the model, but on the controller where there is no such method available, hence the error.
While the controller may automatically load the model, it doesn't expose its API, it just makes an instance of the model available via magic property accessors.
So instead of
$this->formatData($this->request->data);
you have to invoke the method on the model like this:
$this->Student->formatData($this->request->data);
See also
http://book.cakephp.org/2.0/en/controllers.html#Controller::$uses
http://book.cakephp.org/2.0/en/controllers.html#Controller::loadModel
http://book.cakephp.org/2.0/en/models.html#understanding-models