Can't get Laravel relationship to work - laravel

I dont know what I'm doing wrong today, I can't get a 1-N relationship to work in Laravel 4. I'm trying to describe soccer teams that have many players.
I have a "players.team_id" field. I double-checked the database, the datas are OK.
First model :
class Team extends Eloquent {
public function players() {
return $this->hasMany('Player');
}
}
Second model :
class Player extends Eloquent {
function team() {
return $this->belongsTo('Team');
}
}
In my controllers, I can use $player->team but $team->players always retunrs null.
If I try to var_dump $team->players() (I should see the relationship description), it returns an error :
Call to undefined method Illuminate\Database\Query\Builder::players()
This is what my controller looks like :
class HomeController extends BaseController {
public function showTeam($slug) {
$team = Team::where('slug','=',$slug)->first();
// this works fine
$player = Player::find(1);
var_dump($player->team);
// this works, too.
$players = Player::where('team_id','=',516)->get();
var_dump($players);
$team = Team::where('slug','=',$slug)->first();
var_dump($team->players); // returns null
var_dump($team->players()); // throws an error
return View::make('team')
->with('team', $team);
}
}
Any leads ? Thank you very much !

I found the bug thanks to Quasdunk comment.
composer dump-auto
told me that I had twice the "Team" class.
Warning: Ambiguous class resolution
I copy/pasted a model file and forgot to change the class name in it. The problem was solved after I renamed this class.

Related

Returning other entry in the url includes the first entry as well

public function show(Criminal $criminal){
$profile = Criminal::with(['profile','crimes'])->findOrFail($criminal);
dd($profile);
}
I have this method and it should return like this when I type localhost:8000/criminal/1
But when I say like criminal/3
it also returns the criminal/1 json output like this :
the first entry looks like this :
try this
public function show(Criminal $criminal, $id){
$profile = Criminal::with(['profile','crimes'])->findOrFail($id);
dd($profile);
}
There is no need to query the database again, the model passed to your function is already an eloquent model.
public function show(Criminal $criminal) {
dd($criminal);
}
If you really want to lazy load the relations, this can be done as follows:
public function show(Criminal $criminal) {
$criminal->load('profile', 'crimes')
dd($criminal);
}
This should not be necessary however as Laravel loads relations when needed.

OctoberCMS Related Model Dynamic Default Value

I have a backend controller implements Backend\Behaviors\RelationController, I just want to set default values for the related model depending on the parent model values.
I have tried the following: model.beforeCreate, formExtendFields but no luck.
Thank you all.
After whole day of searching, I found the solution, it is documented no where on OctoberCMS website, I inspected the source file Backend\Behaviors\RelationController, after that I came with the below solution.
You should implement relationExtendViewWidget on your controller, then you can access the model from: $widget->model, something like below:
class Members extends Controller
{
public $implement = [
'Backend\Behaviors\RelationController',
];
public function relationExtendViewWidget($widget, $field)
{
$member = Member::findOrFail($this->params[0]);
$widget->model->course_id = $member->course_id;
$widget->model->member_id = $member->id;
}
public function relationExtendManageWidget($widget, $field)
{
$member = Member::findOrFail($this->params[0]);
$widget->model->course_id = $member->course_id;
$widget->model->member_id = $member->id;
}
}
I believe this should be documented somewhere on OctoberCMS documentation

Laravel policies - passing the class as a variable to $user->can() method doesn't work

I have a route with dynamic model recognition. In other words, I take the desired model as an argument and use it in the controller. I have complex authorization in my app and I need to pass the model class name as a variable to the $user->can() method for using policies, but for some reason it doesn't work. Here's my code:
Policy:
public function view($user, Model $model) {
return $user->model_id == $model_id;
}
public function create($user) {
return $user->isAdmin();
}
Controller:
public function createModel($model) {
$model_class = $model . '::class';
if (Auth::user()->can('create', $model_class)) {
return $model_class::create();
}
return 'invalid_permissions';
}
If I hardcode the model class name it works. For example, if my model is 'Car' and in the controller I put:
if (Auth::user()->can('create', Car::class)) {
Anybody got any ideas why this is so and how to fix it? I hope that it's possible because I would have to change my whole concept if it isn't.
*Note: this is example code, not my actuall classes

Laravel Relation - Different Results with Static Vs Non-Static

I've run in to a problem with Laravel which I cannot for the life of me work out.
I have the following models which are essentially:
class Team extends Model
{
public function notices()
{
return $this->belongsToMany('App\Notice', 'notice_recipients');
}
}
class Notice extends Model
{
public function teams()
{
return $this->belongsToMany('App\Team', 'notice_recipients');
}
}
I have run into the problem where on my Team model:
dump($this->notices);
returns different results to:
dump(Team::find($this->id)->notices);
To investigate further I have the following code:
dump($this->id); // = 20
dump($this->notices()->toSql());
dump($this->notices()->getBindings());
dump($this->notices);
dump(Team::find($this->id)->notices()->toSql());
dump(Team::find($this->id)->notices()->getBindings());
dump(Team::find($this->id)->notices);
Lines 2-3 output exactly the same as far as I can tell as lines 5-6 so I should be gettings the same results, however lines 4 and 7 output different results! (Line 4 seems to output only a selection of the results outputted by line 7).
The issue seems to happen only on certain Team models and it seems completely random as to whether the issue occurs or not on any particular model.
Any ideas as to what might be going on or how I might further debug this issue?
Many thanks
notice_recipients table if has the following fields
id
team_id
notice_id
try this maybe this will work
public function notices()
{
return $this->belongsToMany('App\Notice', 'notice_recipients', 'team_id', notice_id);
}
public function teams()
{
return $this->belongsToMany('App\Team', 'notice_recipients', 'notice_id', 'team_id');
}

how to debug data query in laravel

This is a beginner question, but I searched for an hour and couldn't find an answer.
I am trying to write a simple data query which I included in my HomeController
<?php
class HomeController extends BaseController {
public function showWelcome()
{
return View::make('hello');
}
}
$programs=DB::table('node')->where('type', 'Programs')->get();
$programs is undefined so I am guessing that my query didn't work but I have no idea how to debug it. I tried installing firebug and phpbug and chromphp tools but they don't seem to show anything. My apache log doesn't show anything either. Am I missing something? How do I debug this?
You can't use an expression outside of a method when using a class, instead you need to put it inside a method like:
class HomeController extends BaseController {
public function getPrograms()
{
$programs = DB::table('node')->where('type', 'Programs')->get();
// pass the $programs to the programs view for showing it
return View::make('programs')->with('programs', $programs);
}
}
So, for example, if you have a route like this:
Route::get('/programs', 'HomeController#getPrograms');
Then you may use an URL like: example.com/programs to invoke the getPrograms method in the class HomeController.
Probably this answer doesn't help much but I think you should learn the basics (PHP Manual) first so read books and articles online and check the Laravel website to read the documentation.
You can pass the result of that query to the view like so:
class HomeController extends BaseController {
public function showWelcome()
{
$programs = DB::table('node')->where('type', 'Programs')->get();
return View::make('hello', array('programs' => $programs));
}
}
And in your view you will have access to the $programs variable.
I don't know If i'm missing the point, but can't you use the "dd($programs)" to check what is or not is inside the variable?

Resources