Laravel 5 Model Accessor Displayed as Undefined Variable - laravel-5

I have the following laravel model class and I have an accessor to get the location name as follows (not sure if its correct syntax)
class Job extends Model {
public function location()
{
return $this->belongsTo('App\Location');
}
public function getLocationNameAttribute()
{
return $this->location('name');
}
}
in my form I have the following:
{!! Form::text('location_name', $locationname !!}
But I get error Undefined variable: locationname when i try to view the page.
The location table has two columns(id, name) and I want to get the name of the location associated with the job. How do you do this. In the job table I only store the location_id. I can do it as follows isset($job->location_id) ? $job->location->name : '' but I'm trying to understand how I can achieve the same using an accessor. Can any one shed some light please.

Related

How to add my course Slug before lecture in url path?

i'm currently doing a project for my homework, which i didn't managed to see something like this to fix my problem.
I have 2 models with Course and CourseLecture name. and i'm using getPathAttribute in my class base on a tutorial in internet.
in my Course Model i'm using getPathAttribute like this :
public function getPathAttribute()
{
return "/clientside/instructor/courses/$this->slug";
}
and in my CourseLecture Model :
public function getPathAttribute()
{
return "/clientside/instructor/courses/{course}/lectures/$this->slug";
}
I need to put my course slug to this getPathAttribute like :
http://url.com/clientside/instructor/courses/php/lectures/one
also my CourseLecture is using course_id and i've got a relationship between them which, they're belongsTo' andHasMany'. so how can i add course slug base on this structor in this path?
Also for this homework, i'm using Vue.js/Laravel. and it's a spa. i've tagged vue.js bcs if there's any solution to fix this via router, I will be happy to use it.
You can dynamically grab course slug from the course() relationship defined on the CourseLecture model. See below:
In CourseLecture model:
protected $appends = ['path'];
// relationship
public function course()
{
return $this->belongsTo(Course::class);
}
public function getPathAttribute()
{
return "/clientside/instructor/courses/{$this->course->slug}/lectures/$this->slug";
}

Call to undefined relationship in laravel

I have two tables departments and course, and I have defined a relationship between them but I received this error message:
Call to undefined relationship in laravel
Can anyone tell me please where's the problem?
Model Code:
public function course()
{
return $this->hasMany('App\Course','departments_id','id');
}
Model Department:
public function department()
{
return $this->belongsTo('App\Departments','departments_id');
}
So problem is so clear. Either in your Course model you have to rename the function department to departments, or to make a new one with the correct name.
First of all little correction in schema $table->integer('department_id'); should be $table->unsignedInteger('department_id');
And the problem is - in your migration column name is department_id but in your relation you have written departments_id there is a extra S!
just correct the column name from model.
in my case, i used:
::with('boards')
i just changed it to :
::with(['boards'])
with brackets
in my department Model:
public function boards()
{
return $this->hasMany(Board::class);
}

Laravel Eloquent Relationship Undefinded Property

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";

Get model name dynamically in cakephp 2.x

Is there any function to get the all table or model names in cakePhp.
I want to update my table's field and for that I need to select all table dynamically so that in future when I'll add new table I do not have to make changes in function.
The new table automatically update the fields.
I don't quite understand the question - from what i gather you want to write abstract actions to account for multiple models? If this is the case you can add abstract actions into the AppController and use $this->modelClass. This will return the model name from were you are calling the abstract action from. For example if you calling the abstract action from 'UsersController' which by default uses the Model 'User' then the modelClass will return 'User'.
class AppController extends Controller {
public function abstractAdd() {
// Get the model in use
$this->{$this->modelClass}->create();
// Use the save method in that model
if ($this->{$this->modelClass}->save($data)) {
// do something
}
}
}
class UsersController extends AppController {
public function add() {
$this->abstractAdd();
}
}
Hope this helps

Correctly use Eloquent relationships

I am developing a Laravel 4 application with 4 database tables:
Therapist
TherapistType
Municipality
County
I'm using foreign keys to reference the data from other tables.
The relationships are the following:
A therapist can have one therapist type, municipality and county
A therapist type and municipality can belong to many therapists.
A municipality can have one county.
A county can belong to many municipalities and therapists.
I'm using Eloquent ORM.
This is the code I tried to use:
Therapist model:
public function therapistType() {
return $this->belongsTo('TherapistType');
}
public function municipality() {
return $this->hasOne('Municipality');
}
public function county() {
return $this->hasOne('County');
}
}
Municipality model:
public function county() {
return $this->hasOne('County');
}
In my controller I use the following code to fetch the therapists:
$therapists = Therapist::paginate(10);
return View::make('index', compact('therapists'));
And finally in my view this is how I'd like to get the corresponding therapist type for a therapist:
<span class="therapisttype">{{{ $therapist->therapistType }}}</span>
However I get no data.
What am I doing wrong?
$therapist->therapistType should be returning an object, but you are not echoing the property of said object. Let us just imagine that the therapistType table has a name property, then you should do
{{$therapist->therapistType->name}} if you want to echo that name.
I would begin by var_dumping the object, you can use $therapist->therapistType assuming that you have the relationships set up correctly, you should be able to see all it's properties.
Hope it helps.

Resources