Laravel Eloquent Relationship Undefinded Property - laravel

I have a Model House and a Model energy_class.
A house has only one energy class. Energy Classes can be assigned to multiple houses. I therefore defined the relationships like this:
House.php
class House extends Model
{
public function energy_class()
{
return $this->hasOne('App\energy_class', 'id', 'energy_class');
}
}
energy_class.php
class energy_class extends Model
{
public function house()
{
return $this->belongsToMany('App\House');
}
}
When passing the House Data to a view like this:
$house = House::with('energy_class')->find($id);
return view('admin.houses.edit')->with('house', $house);
And referencing it on the view like this:
$house->energy_class()->name
I get this error: Undefined property: Illuminate\Database\Eloquent\Relations\HasOne::$name
When doing it like this: $house->energy_class->name I get trying to get property of non-object

Your first suggestion:
$house->energy_class()->name
Does not work, because at this point you only called the relationship, you didn't yet specify from it what you wanted, e.g.:
$house->energy_class()->first()
Your second try returns empty because $house->energy_class (which does the same as the above code), didn't return any results.
There can be various causes for this:
Your relationship is not properly signed up
Your database is not properly setup (have you set up a foreign key in your migration?)
The house actually does not have an energy class filled out
I suspect in your case it might be the second or the third. Could you paste me your migration script?
Edit: PS. if this error occurs because perhaps not each house actually has a an energy class, you could use the following code to check for this:
$house->energy_class ? $house->energy_class->name : "No energy class available";

Related

Laravel 5.5 retrieving null by nested relation

I have 3 databases:
Routes:
id
name
Rates:
Id
Route_id
Car_id
Cars:
id
name
My model for routes
public function rates()
{
return $this->hasMany('App\Rate', 'route_id');
}
My model for rates
public function car() {
return $this->belongsTo('App\Car','car_id');
}
Now I need to access the car relation, but when I do
return $this->route->with('from','to','rates.car')->paginate(74);
I get null for the car relation
{"id":1,"from_id":1,"to_id":2,"distance":400,"created_at":null,"updated_at":null,"from":{"id":1,"name":"\u0410\u043a\u043a\u043e","created_at":null,"updated_at":null,"lat":32.93310000000000314912540488876402378082275390625,"long":35.0827000000000026602720026858150959014892578125},"to":{"id":2,"name":"\u0410\u0440\u0430\u0434","created_at":null,"updated_at":null,"lat":31.261399999999998300381776061840355396270751953125,"long":35.21490000000000009094947017729282379150390625},"rates":[{"id":1,"route_id":1,"car_id":1,"rate":1123,"night_rate":1391,"car":null},{"id":5551,"route_id":1,"car_id":2,"rate":1123,"night_rate":1391,"car":null},{"id":11101,"route_id":1,"car_id":3,"rate":1123,"night_rate":1391,"car":null},{"id":16651,"route_id":1,"car_id":4,"rate":1123,"night_rate":1391,"car":null},{"id":22201,"route_id":1,"car_id":5,"rate":1123,"night_rate":1391,"car":null},{"id":27751,"route_id":1,"car_id":6,"rate":1123,"night_rate":1391,"car":null},{"id":33301,"route_id":1,"car_id":7,"rate":1123,"night_rate":1391,"car":null},{"id":38851,"route_id":1,"car_id":8,"rate":1123,"night_rate":1391,"car":null}]},
From my understanding you are trying to access a Car model through a Route model.
A couple of things I noticed that should help you find a solution.
First off I think the inverse relation you are supposed to use the belongToMany() function instead.
public function car() {
return $this->belongsToMany('App\Car','Rates'); // Perhaps call the table something like routes_cars to more clearly define it's a pivot table
}
Next I see you are trying to use model functions within the context of $this(). I assume you are doing this in your model? That logic should be in a controller, that might cause some undesired results but I'm not entirely sure. Also it looks like your parameters are incorrect when using with(). You use the function name that you defined in belongsToMany()
App/Route::with('car')->paginate(74);
With the correct relationships setup you rarely need to worry about the pivot table. If you are going to add extra information in the pivot table there are laravel functions to help you do that in the documentation.

Eloquent model method or attribute for calculation

I'm looking for a way to define a custom Eloquent method which only returns a calculation based on several columns in the according database table. I also tried setting it in the models attributes, but neither seem to work. Maybe I'm missing something, here's what I got so far:
<?php
use Illuminate\Database\Eloquent\Model;
class Team extends Model
{
public function difference() {
return $this->goals_f - $this->goals_a;
}
}
The error message I'm recieving is:
Relationship method must return an object of type Illuminate\Database\Eloquent\Relations\Relation
Your error makes it sound to me like you are trying to call your method like a property" $team->difference. You would instead need to call it like: $team->difference(). If you want to get the difference as if it was a property, then you would need to define an accessor:
public function getDifferenceAttribute(){
return $this->goals_f - $this->goals_a;
}
Then you could call it like $team->difference.
You could also define a protected $appends property to your model so that the difference property is subsequently visible in it's JSON representation:
protected $appends = ['difference'];
More information: https://laravel.com/docs/5.2/eloquent-mutators#accessors-and-mutators

Laravel — How to get string value from object via model method?

I have model House with relation hasMany HousePhotos.
I try get link to main photo from table house_photos.
class House extends Model
{
public function photos(){
return $this->hasMany('app\HousePhoto');
}
public function get_main_photo(){
return $this->photos()->where('main', true);
}
}
Controller:
$house=House::find(1);
In View i use
{{$house->main_photo()->link}}
and got error.
When i use
{{$house->main_photo()}}
i got object. How to get string value of link to photo?
First of all you need to understand the difference between the Builder object and the Collection object.
As it is now, function get_main_photo returns a Builder object (actually a Relation object, which contains a Builder).
On these objects you can call function get() to finish the query and get the results. In this case you will get a Collection object, which you will be able to iterate.
Alternatively, in this case you seem to only have one 'main photo' per house, so instead you can call first():
public function get_main_photo(){
return $this->photos()->where('main', true)->first();
}
This will return the single associated model, on which you will be able to access ->link (if the model was found).
Remember at any point while debugging you can call the convenient dd function (which dumps the object passed as parameter and terminates the applicacion) to see what type of object you are dealing with, and all its attributes.

Ordering table data

I already read this piece from the laravel 5.1 documentation:
$users = DB::table('users')
->orderBy('name', 'desc')
->get();
I have no ideia where to write that.
And this is what I tried to write inside my Model:
class Professor extends Model
{
$professor = DB:table('professor')->orderBy('name','asc')->get();
}
Also tried:
class Professor extends Model
{
Professor::orderBy('name')->get();
//$professor = Professor::orderBy('name')->get();
}
Nothing works e.e
All of them give me erros like:
syntax error, unexpected '$professor' (T_VARIABLE), expecting function (T_FUNCTION)
The piece of code your trying to write should not be placed inside a Model. It should be in a controller or a repository if your using a Repository Pattern.
Assuming you got the following in your code.
A table called professors. A model Professor . A Controller called ProfessorsController.
And a route file with the following code get('professors','ProfessorsController#index');
Then on the controller you should have the following code.
class ProfessorsController extends Controller {
public function index()
{
$professors = \DB:table('professors')->orderBy('name','asc')->get();
return view('proffesors')->with('proffessors',$professors);
}
}
This will return an order list of professors to the view. That is if you are using a view to represent the data.
It does not need to be in model. Most of time something like that goes in controller.
In model you need to define relations or functions that would be used application wide on a object.
If you want to do something similar in model you won't use DB::table you need something like:
class Professor extends \Eloquent
{
public function professorsByName(){
$professionCollection = Professor::all()->orderBy('name','asc')->get();
return $professionCollection;
}
}
Please take it as example it's not something that should go in model at least this simple not.
Mental Note
Never use DB::table reason your observer if any won't execute.
I think you all are missing the primary issue with
class Professor extends Model
{
$professor = DB:table('professor')->orderBy('name','asc')->get();
}
This is not how PHP classes work.
OP, you need to brush up on the concepts of OOP.
What you need is something like this:
your controller:
public function myRoutedMethod()
{
$professors = Professor::getModel()->orderBy('name','asc')->get();
foreach($professors as $professor)
{
var_dump($professor->toArray();
}
}
Or probably even better, create a repository to interface with your model and just call $repository->professors()->toArray();
Google search: Laravel Repository

Associating a child model in a polymorphic relationship causes an infinite loop?

I have a base Message class for an inbox using a polymorphic relationship to attach custom message types which all implement the same interface and behave differently in views based on their type. Display of that all works swimmingly, but I hit a snag when I tried to actually add these with code.
This is the Message class:
<?php
class Message extends Eloquent {
public function author() {
$this->belongsTo("User", "author_id");
}
public function recipient() {
$this->belongsTo("User", "recipient_id");
}
public function message() {
$this->morphTo();
}
}
The model that I attach to message() implements MessageInterface, So I thought I'd be able to make a quick helper to attach this model's relationship via Message::send():
public static function send(MessageInterface $message, User $to, User $from) {
if (! $message->exists)
$message->save();
$parent = new static;
$parent->author()->associate($from);
$parent->recipient()->associate($to);
$parent->message()->associate($message); // line that errors
return $parent->save();
}
But this ends up throwing what looks to be infinite recursion at me:
FatalErrorException: Maximum function nesting level of '100' reached, aborting!
This is the studly function, and from some searching it seems to happen when two models reference each other.
The Schema for the messages table is:
$table->increments("id");
$table->integer("message_id")->unsigned();
$table->string("message_type");
$table->integer("recipient_id")->unsigned();
$table->integer("author_id")->unsigned();
$table->timestamps();
Am I just doing something really wrong, here? I've looked through the morphTo method call in the source and tried seeing if reflection is the problem here (grabbing the function name and snake casing it), but I can't seem to find what is happening. The associate method call is just setting attributes and getting the class name for message_type, then returning a relationship.
There's no useful information in the error; it's a Symfony\Component\Debug\Exception\FatalErrorException with no context.
I'm running Laravel 4.1

Resources