I have a pivot model called UserTask in which I have an accessor function:
class UserTask extends Pivot implements HasMedia
{
use HasMediaTrait;
public function getCompletedAttribute()
{
return $this->getMedia()->isEmpty() && $this->completed;
}
public function task()
{
return $this->belongsTo(Task::class);
}
}
I specify the relation in my Task model like this:
class Task extends Model
{
public function users()
{
return $this->belongsToMany(User::class, 'user_task')->using('App\Models\UserTask')->withPivot('completed');
}
}
I get the following error:
"message": "Undefined property: App\Models\UserTask::$completed",
Anyone know why this is happening?
Try this accessor function:
public function getCompletedAttribute($value)
{
return $this->getMedia()->isEmpty() && $value;
}
Related
I want to seed my db that shows me this error:
Call to undefined method Illuminate\Database\Query\Builder::passengers()
this is databse seeder:
public function run()
{
// $this->call(UsersTableSeeder::class);
factory(App\Airport::class, 5)->create();
factory(App\Flight::class, 10)->create()->each(function ($flight) {
factory(App\Customer::class, 100)->make()->each(function ($customer) use ($flight) {
$flight->passengers()->save($customer);
});
});
}
Customer Model:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Customer extends Model
{
public function Flights()
{
return $this->belongsToMany('App\Customer');
}
}
Flight Model
class Flight extends Model
{
//
public function arrivalAirport(){
return $this->belongsto('App\Airport','arrivalAirport_id');
}
public function departureAirport(){
return $this->belongsto('App\Airport','departureAirport');
}
public function passenger(){
return $this->belongsto('App\Customer','flight_customer');
}
}
who know where this can come ?
You have used the singular in your model and trying to access the plural in the seeder.
class Flight extends Model
{
public function passengers()
{
return $this->belongsto('App\Customer', 'flight_customer');
}
}
I am struggling with the relation hasOne. here is what I have
User model:
class User extends Authenticatable
{
use Notifiable;
public function candidat()
{
return $this->hasOne('App\Model1');
}
public function element ()
{
return $this->hasOne('App\Model2');
}
}
Model1 class:
class Model1 extends Model
{
//
public function user()
{
return $this->belongsTo('App\User');
}
}
Model2 class:
class Model2 extends Model
{
//
public function user()
{
return $this->belongsTo('App\User');
}
}
Controller class:
public function savingcandidat(CandidatRequest $requete){
$candidat = $this->candidatSave($requete);
$user = User::query()->where('email',$requete->email)->get(); //there i get the user
//here is where i get the error : "Method Illuminate\Database\Eloquent\Collection::candidat does not exist."
$user->candidat()->save($candidat);
}
I get the error:
"Method Illuminate\Database\Eloquent\Collection::candidat does not
exist."
when I use tinker it works properly, thank you in advance for your help
$user = User::query()->where('email',$requete->email)->get();
This will return a collection result and it will not have that model/builder method. use first() instead of get if it's just one record
Like this
$user = User::query()->where('email',$requete->email)->first();
I've read many posts about this issue but none of them works for me. I have a 'ISA' relationship in my database. A person can be either a Patient or a Nurse:
class Person extends Model
{
protected $table = 'persons';
public function commentable()
{
return $this->morphTo();
}
}
class Patient extends Model
{
public function persons()
{
return $this->morphMany('App\Person', 'commentable');
}
}
class Nurse extends Model
{
public function persons()
{
return $this->morphMany('App\Person', 'commentable');
}
}
This is my tables and the data inside them:
And this is my Route:
Route::get('person', function () {
$person = Person::find(1)->commentable();
return json_decode(json_encode($person), true);
});
I get an empty array!
You have to access the relationship as a property:
$person = Person::find(1)->commentable;
So I have the following relational structure:
User - UserTask - Task
My pivot model (UserTask) needs to access a property from the Task model.
So I have an accessor function in my UserTask model in which I need to access the Task->document_upload_required property.
Any one know how I can access this?
Note:
I cannot set the accessor in the parent model because I need to use the Media functions set on the pivot model.
Here is how I've defined the relationships:
Task:
class Task extends Model
{
public function users()
{
return $this->belongsToMany(User::class, 'user_task')->using('App\Models\UserTask')->withPivot('completed');
}
}
UserTask:
class UserTask extends Pivot implements HasMedia
{
use HasMediaTrait;
public function getCompletedAttribute()
{
return "Need to access parent attribute here";
}
}
User:
class User extends Authenticatable
{
use HasApiTokens, Notifiable, Billable;
public function tasks()
{
return $this->belongsToMany(Task::class);
}
}
UPDATE:
Here is my pivot (UserTask) table that I am trying to fetch data from:
Here is the function in my pivot model:
public function isComplete() {
if($this->task->document_upload_required) {
return $this->getMedia()->isEmpty() && $this->completed;
} else {
return $this->completed;
}
}
public function getCompletedAttribute()
{
return $this->isComplete();
}
Here is how I make the call to join the models:
$sections = $this->model->with(['subsections.tasks.users' => function($q){
$q->where('users.id', '=', Auth::id());
}])->where('parent', NULL)->doesntHave('assessments')->sorted()->get();
You need to declare the relationship first and then access its property, like this:
public function task()
{
return $this->belongsTo(Task::class);
}
public function getCompletedAttribute()
{
return $this->task->completed;
}
EDIT:
$collection = $this->model->with(['subsections.tasks.users' => function($q){
$q->where('users.id', '=', Auth::id());
}])->where('parent', NULL)->doesntHave('assessments')->sorted()->get();
$collection->each(function($subsection) {
$subsection->tasks->each(function($task) {
$task->users->each(function($user) {
dump($user->pivot->completed);
});
});
});
dd();
How can I get all the submissions belongs to a particular user?
Trying this:
$user=User::find(1);
dd($user->submissions);
Throwing error:
Call to undefined method Illuminate\Database\Query\Builder::hasMany()
Will I have to loop through the models?
Here are the models:
class User extends Eloquent implements ConfideUserInterface, BillableInterface
{
public function categories()
{
return $this->hasMany('Category');
}
public function forms()
{
return $this->hasManyThrough('App\Models\Form', 'Category');
}
public function submissions()
{
return $this->hasMany('Category')->hasMany('Form')->hasMany('Submission');
}
}
class Category extends \Eloquent {
public function user()
{
return $this->belongsTo('User');
}
public function forms()
{
return $this->hasMany('App\Models\Form');
}
public function submissions()
{
return $this->hasManyThrough('Submission', 'App\Models\Form', 'id', 'form_id');
}
}
namespace App\Models;
class Form extends \Eloquent {
public function user()
{
return $this->category->user();
}
public function category()
{
return $this->belongsTo('Category');
}
public function submissions()
{
return $this->hasMany('Submission');
}
}
class Submission extends \Eloquent {
public function user()
{
return $this->form->category->user();
}
public function category()
{
return $this->form->category();
}
public function form()
{
return $this->belongsTo('App\Models\Form');
}
}
That doesn't really work that way with chaining relations...
What you can do, is this:
$submissions = Submission::whereHas('form.category.user', function($q){
$q->where('id', 1);
})->get();
Note that whereHas with nested relationships has only been added in the latest Laravel 4 release. Make sure to composer update.
If i'm not getting your question wrongly, you need to define relationship correctly.
According to laravel 4.2 you can use below kind of code to define relations:
class User extends \Eloquent implements ConfideUserInterface, BillableInterface{
//...
public function submissions(){
return $this->hasMany('Submission');
}
}
//...
class Submission extends \Eloquent {
public function user()
{
return $this->belongsTo('User');
}
//...
}
To further reading take a look at: http://laravel.com/docs/4.2/eloquent#relationships