I'm trying to get a polymorphic relationship working on laravel but every test I make returns either null or ax exception on laravel's pretended SQL query...
FYI: My laravel version is 5.8 running on a MySQLserver 5.7 (WAMP package)
According to what I read on laravel's docs, I think I must use morphTo() in the polymorphic model's class and morphMany() on it's children tables, so I coded like this:
App\Models\Company
public function accounts(){
return $this->morphMany(Accountable::class, 'accountable');
}
App\Models\Account\Accountable
public function company()
{
return $this->belongsTo(Company::class);
}
public function accountable()
{
return $this->morphTo();
}
Migrations:
companies
Schema::create($this->tableName, function (Blueprint $table) {
$table->bigIncrements('id');
...
})
bank_accounts migration
Schema::create('bank_accounts', function (Blueprint $table) {
$table->bigIncrements('id');
...
});
company_has_accounts migration
Schema::create('company_has_accounts', function (Blueprint $table) {
$table->bigIncrements('id');
$table->morphs('accountable');
...
});
Errors:
The thing is: Whenever I try to debug BankAccount::find(1)->company I keep getting:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'company_has_accounts.company_type' in 'where clause' (SQL: select * from `company_has_accounts` where `company_has_accounts`.`c
ompany_id` = 1 and `company_has_accounts`.`company_id` is not null and `company_has_accounts`.`company_type` = Financeiro/Models/Account/BankAccount and `company_has_accounts`.`deleted_at` is null)
and when I try: Company::find(1)->accounts I keep getting an empty Collection.
My objectives are:
App\Models\Account\BankAccount
public function company()
To return a App\Models\Company Object.
App\Models\Account\Company
public function accounts()
To return a collection of mixed "App\Models\Account\BankAccount" and "App\Models\Account\CreditCard" Objects, all belonging to the current App\Models\Account\Company Model, as filtered by the "company_id" column on "company_has_accounts" table.
After some struggle I managed to get it working. But since I'm new to Laravel I don't know if it's the best approach...
App\Models\Account\Accountable
public function company(){
return $this->belongsTo(Company::class);
}
public function accountable(){
return $this->morphTo();
}
App\Models\Account\Company
public function accountables(){
return $this->hasMany(Accountable::class)->with('accountable');
}
public function accounts(){
$accountables = $this->accountables();
$accounts = [];
if ($accountables->count()) {
foreach ($accountables->get() as $accountable) {
$accounts[] = $accountable->accountable;
}
}
$accounts = collect($accounts);
return $accounts;
}
App\Models\Account\BankAccount
public function accountable(){
return $this->morphOne(Accountable::class, 'accountable');
}
public function company(){
$accountable = ($this->accountable())->with('company');
return $accountable->first()->company;
}
Related
I am using UUID's across my application and I have implemented the trait, as seen online, like so:
trait Uuid
{
protected static function boot(): void
{
parent::boot();
static::creating(function (Model $model) {
if (!$model->getKey()) {
$model->{$model->getKeyName()} = (string) Str::uuid();
}
});
}
public function getIncrementing(): bool
{
return false;
}
public function getKeyType(): string
{
return 'string';
}
}
Retrospectively, this works almost everywhere: I'm trying to create a pivot table on my products like so:
public function categories(): BelongsToMany
{
return $this->belongsToMany(
Category::class,
ProductCategory::class,
'product_id',
'category_id'
);
}
The migration looks as follows:
public function up(): void
{
Schema::create('product_categories', function (Blueprint $table) {
$table->uuid('id')->primary();
$table->foreignUuid('product_id')->index();
$table->foreignUuid('category_id')->index();
$table->timestamps();
});
}
However, whenever I do the following in my seeding:
Product::first()->categories()->sync(Categories::all()->pluck('id'));
I see this error:
PDOException::("SQLSTATE[HY000]: General error: 1364 Field 'id' doesn't have a default value")
Both Category and ProductCategory use the Uuidd trait and I cannot figure out how to make this work.
Any help appreciated.
As one of the possible solutions you could use your own model with your trait for the pivot table.
More:
https://laravel.com/docs/9.x/eloquent-relationships#defining-custom-intermediate-table-models.
I want to return the projects of the authenticated user, but am not getting any. I know the records exist in the database.
This is my model Project:
public function users(){
return $this->hasMany(User::class);
}
this is my model User:
public function projects(){
return $this->hasMany(Projet::class,'user_id');
}
and this is the controller function :
public function projetuser(){
$user = User::find(auth()->user()->id);
return $user->projects;
}
and this my user_projet migration:
public function up()
{
Schema::create('user_projet', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('projet_id');
$table->foreign('projet_id')->references('id')->on('projets')->onDelete('cascade');
$table->unsignedBigInteger('user_id');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->string('membre')->nullbale();
$table->timestamps();
});
}
You are defining a many-to-many relationship incorrectly. Use belongsToMany() instead of hasMany(). Because your pivot table name is not standard (it should be alphabetic order projet_user) you need to include it in the relationship definition as well.
<?php
use Illuminate\Database\Eloquent\Model;
class Projet extends Model
{
public function users()
{
return $this->belongsToMany(User::class, 'user_projet');
}
}
class User extends Model
{
public function projets(){
return $this->belongsToMany(Projet::class, 'user_projet');
}
}
Now in your controller you can do this:
public function projetuser(){
return auth()->user->projets;
}
Your question seems to vary between "projet" and "project." I assumed "projet" was the correct spelling, but try to keep this consistent.
Please note also the typo in your migration: nullbale.
I am making FollowController where I have two tables following_users table and following_user_item table. which is in hasMany relationship. When a authenticate current_user wants to follow an user, the ID of the user will stored in following_users table and its relational table stored the current_user_id and the following_user_id (which is the id of following_users table). here is the schema.
following_users_table:
public function up()
{
Schema::create('following_users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->bigInteger('user_id')->unsigned();
$table->foreign('user_id')
->references('id')
->on('users');
$table->timestamps();
});
}
following_user_item_table:
public function up()
{
Schema::create('following_user_items', function (Blueprint $table) {
$table->bigIncrements('id');
$table->bigInteger('following_users_id')->unsigned();
$table->foreign('following_users_id')
->references('id')
->on('following_users');
$table->bigInteger('user_id')->unsigned();
$table->timestamps();
});
}
I have done the FollowController but the problem is comming when try to check whether the user is already followed or not.
Follow Relationship in User model
public function followingUserList()
{
return $this->hasOne('App\FollowingUser');
}
/**
* Get the stories associated with the user through an intermediate table
*
*/
public function followingUsers()
{
return $this->hasManyThrough(
'App\FollowingUserItem',
'App\FollowingUser',
null,
'following_users_id'
);
}
FollowingUser Model Relationship with User and FollowingUserItem
public function user()
{
return $this->belongsTo('App\User');
}
public function users()
{
return $this->hasMany('App\FollowingUserItem','following_users_id');
}
Here is my FollowController:
class FollowController extends Controller
{
//
public function index($id)
{
$user = User::find($id);
$logged_userId = Auth::User();
if ($user->id == $logged_userId->id) {
return [
'status' => false,
'message' => 'You can not Follow yourself',
];
}
if ($user && $logged_userId) {
$checkUsers = FollowingUser::where('user_id', $user->id)->get()->users()->where('user_id', $logged_userId->id);
if ($checkUsers)
{
return 'already followed';
}
else
{
$user->followingUserList()->save(new FollowingUser())->users()->save(new FollowingUserItem(['user_id' => $logged_userId->id]));
return 'sucess';
}
}
}
}
I go the error
Method Illuminate\Database\Eloquent\Collection::users does not exist.
When you call get() Laravel returns a collection as it does not know how many rows there will be there. This is why you get collection does not have users set error. Since you filter on an id you know there is only gonna be one, therefor you can utilize the first() method.
So change the code to use first().
$checkUsers = FollowingUser::where('user_id', $user->id)->first()->users()->where('user_id', $logged_userId->id);
The following code in tinker returns a null value while it should return the project to which the first task is linked.
App\Task::first()->projects;
Already tried renaming the method names, column names in migrations, tried exiting tinker and logging back in
Project Migration
public function up()
{
Schema::create('projects', function (Blueprint $table) {
$table->bigIncrements('id');
$table->text('title');
$table->string('description');
$table->timestamps();
});
}
Task Migration
public function up()
{
Schema::create('tasks', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedInteger('project_id');
$table->string('description');
$table->boolean('completed')->default(false);
$table->timestamps();
});
}
Project.php
use App\Task;
class Project extends Model
{
protected $fillable = ['title','description'];
public function tasks(){
return $this->hasMany(Task::class);
}
}
Task.php
use App\Project;
class Task extends Model
{
protected $fillable = [
'completed'
];
public function projects(){
return $this->belongsTo(Project::class);
}
}
If anyone could just review this piece of code and let me know where I have made any conventional\idiotic mistakes (since Im new to route model binding) it would be of great help!
A task belongs to a project, so rename projects to project as it is singular. If you keep projects then provide the column name as second parameter:
public function projects(){
return $this->belongsTo(Project::class, 'project_id');
}
// I suggest this
public function project(){
return $this->belongsTo(Project::class);
}
Your column types are different, for the id of the project you use Big Integer and for the reference you use Integer, so this:
$table->unsignedInteger('project_id');
should be this:
$table->unsignedBigInteger('project_id');
// also good to make the relationship on the Database level:
$table->foreign('project_id')->references('id')->on('projects')->onDelete('cascade');
I am having issues calling a departure ICAO and arrival ICAO from my schedules table. Laravel keeps giving me errors that my relationships are screwed up. Here is the code.
Schema::create('airports', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('city');
$table->string('country');
$table->string('iata');
$table->string('icao');
$table->double('lat');
$table->double('lon');
$table->longText('data')->nullable(); //JSON Data for All gate information for the system.
$table->softDeletes();
});
Schema::create('schedule_templates', function (Blueprint $table) {
$table->increments('id');
$table->string('code');
$table->string('flightnum');
$table->integer('depicao')->unsigned();
$table->foreign('depicao')->references('id')->on('airports')->onDelete('cascade');
$table->integer('arricao')->unsigned();
$table->foreign('arricao')->references('id')->on('airports')->onDelete('cascade');
$table->string('aircraft')->nullable();
$table->boolean('seasonal');
$table->date('startdate');
$table->date('enddate');
$table->time('deptime');
$table->time('arrtime');
$table->integer('type');
$table->boolean('enabled');
$table->text('defaults');
$table->timestamps();
$table->softDeletes();
});
Here are the Models
class ScheduleTemplate extends Model
{
public $table = "schedule_templates";
public function depicao()
{
return $this->hasOne('App\Models\Airport', 'depicao');
}
public function arricao()
{
return $this->hasOne('App\Models\Airport', 'arricao');
}
}
class Airport extends Model
{
//
public $timestamps = false;
public function schedules()
{
return $this->belongsToMany('App\ScheduleTemplate');
}
}
When I attempt to query using the following code, I get the error
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'vaos_airports.depicao' in 'where clause' (SQL: select * from vaos_airports where vaos_airports.depicao in (1))
$schedules = ScheduleTemplate::with('depicao')->with('arricao')->get();
The end goal is to pull the results into a table. Here is that code if interested.
#foreach($schedules as $s)
<tr>
<td>{{$s->code}}</td>
<td>{{$s->flightnum}}</td>
<td>{{$s->depicao()->name}}</td>
<td>{{$s->arricao()->name}}</td>
<td>{{$s->aircraft}}</td>
<td>{{$s->seasonal}}</td>
<td>{{$s->type}}</td>
</tr>
#endforeach
EDIT:
I fixed the relationship problem Apparently I had them swapped. Here are the updated Model classes
class ScheduleTemplate extends Model
{
public $table = "schedule_templates";
public function depicao()
{
return $this->belongsTo('App\Models\Airport', 'depicao');
}
public function arricao()
{
return $this->belongsTo('App\Models\Airport', 'arricao');
}
}
class Airport extends Model
{
//
public $timestamps = false;
public function schedules()
{
return $this->hasMany('App\ScheduleTemplate');
}
}
The error now lies in the view file. I will either get a BelongsTo error:
Undefined property: Illuminate\Database\Eloquent\Relations\BelongsTo::$name
or this if I have arricao or depicao without "()"
Trying to get property of non-object
The point is, that the second argument of a relationship should be the foreign key, and the second the local key.
From the docs:
return $this->hasOne('App\Phone', 'foreign_key', 'local_key');
So in your case, try this:
public function depicao()
{
return $this->hasOne('App\Models\Airport', 'id', 'depicao');
}
public function arricao()
{
return $this->hasOne('App\Models\Airport', 'id', 'arricao');
}
Update
The errors are thrown because you have the same column name for this relationship. In my opinion, two solutions:
Try to get the first object out of the relationship, like this. But note here: eager loading will not work!
<td>{{$s->depicao()->first()->name}}</td>
<td>{{$s->arricao()->first()->name}}</td>
Rename your relationships or the columns, so they don't overlap current column names. This is by far the best option.
For example, you could change the columns to depeciao_id and arricao_id, this also indicates that the columns are referenced to another table with corresponding ID, which is more descriptive.