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');
}
}
Related
I've following relationship:
class Project extends Model
{
public function invitors()
{
return $this->hasMany(Invitation::class)
}
}
// User model
class User extends Model
{
public function invitations()
{
return $this->morphMany('App\Invitation', 'invitee');
}
}
// Business Model
class Business extends Model
{
public function invitations()
{
return $this->morphMany('App\Invitation', 'invitee');
}
}
// Invitation model
class Invitation extends Model
{
public function invitee()
{
return $this->morphTo();
}
public function project()
{
return $this->belongsTo(Project::class);
}
}
What I want is to get project invitors (users, and businesses). When I use from $project->invitors->invitee its not working.
In your example $project->invitors is a collection of Invitation instances, and it hasn't property invitee. You can use helper pluck to loop $project->invitors and get their invitees:
$invitees = $project->invitors->pluck('invitee');
As a response to the first comment:
to get specific attributes of fetched linked models ("invitee") you can specify accessors:
class Business extends Model
{
...
public function getInviteeNameAttribute() {
return $this->name;
}
}
class User extends Model
{
...
public function getInviteeNameAttribute() {
return $this->first_name . ' ' . $this->last_name;
}
}
...then get that attribute from invitee:
$invitees[0]->invitee_name
See https://laravel.com/docs/7.x/eloquent-mutators#defining-an-accessor
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 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;
}
The error i am getting is "Call to undefined function App\belongsToMany".
This is one of the two models that is used for the relationship:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Review extends Model
{
protected $table = "reviews";
protected $fillable = [ 'user_id','email','review'];
public function user()
{
return $this->belongsTo('App\User');
}
public function votes()
{
return $this->belongsToMany('App\User')->withPivot('vote')->withTimestamps();
}
public function categories()
{
return $this-belongsToMany('Category','category_review','review_id','category_id')->withTimestamps();
}
public function tags()
{
return $this->belongsToMany('App\Tag')->withTimestamps();
}
}
My other model :
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
public function reviews()
{
return $this->belongsToMany('App\Review','category_review','category_id','review_id')->withTimestamps();
}
public function children()
{
return $this->hasMany('App\Category','parent_id');
}
public function parent()
{
return $this->belongsTo('App\Category','parent_id');
}
}
The problem is, i can run the App\Category::find(1)->reviews; but, i can't run App\Review::find(1)->categories; It says "Call to undefined function App\BelongsToMany"
You have two errors in your categories() method.
public function categories()
{
return $this-belongsToMany('Category','category_review','review_id','category_id')->withTimestamps();
}
The first error is the arrow. You put $this-belongsToMany but it should be $this->belongsToMany.
The second error is the namespace. It should be App\Category.
All together, the method should be:
public function categories()
{
return $this->belongsToMany('App\Category','category_review','review_id','category_id')->withTimestamps();
}
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