Laravel relationship on a column that is an array - laravel

I am rather stuck (or not stuck, as I could go and solve this simply with foreach) with a problem I am facing.
I have database where I have a model that should have a relation to another one. No problem here. However, that relation is defined in a column that is an array. I sadly cant change that. Is there any way to still get the corresponding relation of hasMany etc?
Or is my only choice to get all entries of my model and then go over foreach?
Thanks in advance.

Related

What does a colon followed by two column names after a relationship do when eager loading?

When looking for a fix for an issue I had dealing with Datatables, I found this interesting syntax but I cannot find anything on the Laravel documentation to help me understand what's going on but I'd like to learn its useability.
This is the code snippet:
return $model->select('sessions.*')
->with('employee:id,name');
It’s mentioned in the docs: https://laravel.com/docs/8.x/eloquent-relationships#eager-loading-specific-columns
You may not always need every column from the relationships you are retrieving. For this reason, Eloquent allows you to specify which columns of the relationship you would like to retrieve:
$books = Book::with('author:id,name')->get();

One for all or individual Pivot for every many to many in Laravel?

i dont know that i'm doing right or wrong..
i want to make a blog that have multiple categories and tags with many to many relation.
should i make a pivot table for each one like:
"category_post"
$table->integer('post_id')->unsigned()->index();
$table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade');
$table->integer('category_id')->unsigned()->index();
$table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
and another one for Tags table...
or can i make one table for both with an extra column that can filter throw that?
"pivot_post"
$table->integer('post_id')->unsigned()->index();
$table->integer('pivot_id')->unsigned()->index();
$table->string('type');
but.. if i can make one for all, i dont know hot make db and relation??
and which one is better for performance..?
1 big relation tableor multiple relation table?
tnx.
I think you're looking for a polymorphic relationship if you wanna accomplish this with a single table.
https://laravel.com/docs/5.6/eloquent-relationships#polymorphic-relations

How to check if one record is attached to another?

I have defined a many-to-many relationship between Student and Seminar models. How can I find out if one particular student is attending one particular seminar? Non-working example of what I want to achieve is illustrated below:
$seminar->students()->contains($student->id);
The following error is shown when using the above code snippet:
BadMethodCallException with message 'Call to undefined method Illuminate\Database\Query\Builder::contain()'
Try instead:
$seminar->students->contains($student->id);
Presuming $student contains an instance of the Student model you can simplify it further and just use:
$seminar->students->contains($student);
When you add parentheses you're using the Laravel query builder, and you never completed the query. To do it in the manner you had originally you would need:
$seminar->students()->get()->contains($student->id);
This method could be useful if you wanted to add constraints when fetching your students.
But generally you can omit the parentheses and a Collection will be returned, allowing you to use methods like contains.
This has the additional benefit of not re-querying the database once the relationship is loaded, so will generally be a far more efficient means of fetching relationships.
If you haven't already loaded the students and want a database efficient method of checking you could instead use:
$seminar->students()->where('students.id', $student->id)->exists();
In your case the exception is you are calling 'contains()' function (which is for Laravel Collection) on 'Query Builder'. It should be
$seminar->students->get()->contains($student->id);
but this is inefficient since this will retrieve all the students of the seminar.
so instead,
$seminar->students()->wherePivot('student_id', $student->id)->exists();
this method will check in the intermediate table of many to many relationship for particular seminar-student pair, and will return whether exists or not.
The accepted answer is wrong. $seminar->students()->exists($student->id) does not check if the relationship exists. It only checks if the student exists. The student could belong to any seminar and it would still return true.
The correct way to check if a relationship exists without fetching records from the database would be:
$seminar->students()->whereId($student->id)->exists()
Check using query
$seminar->students()->whereKey($student->getKey())->exists();
Check after query (angry), can have memory problems if you have multiple attachments
$seminar->students->contains($student->getKey());
// or
$seminar->students()->get()->contains($student->getKey());

Right way of getting specific columns in Eloquent model

In our app few of our Eloquent models need only a specific columns always. What is the right way to have my method? As of now there is a new method written which does,
DB::table('MyTable')->selectRaw('required_column_list')
I feel it is odd since the table name is hard-coded. What is the right way to do?
If you don't want to hard-code table name, so you have to hard code ModelName?
Use model name with getTable function
DB::table(YourModel::create()->getTable());

Laravel / Eloquent - Many to many pivot model with morphToMany relation

I have a manyToMany relationship set up and have modelled the pivot table also. Also, in the pivot model I have a morphToMany relationship set up.
Here is a diagrammatic representation of the setup.
The trouble I am having is that I can't pull in the joins attribute on the pivot model.
I have this code in my Dimension model;
return $this->belongsToMany('Datasource', 'dimension_datasource')->withPivot('joins');
But I get this error: Unknown column dimension_datasource.joins
I have tried setting up an accessor on the pivot model but it appears that it is being ignored as I get the same error.
Thanks,
Geoff
This is working now. I'm distressed to admit I am not 100% sure why, but I'm fairly certain it was to do with the character encoding of backslashes in the fully qualified class names in the joinable_type column of the joinables table. So, if you are having the same issue, try looking there (there are indications elsewhere on the internet that Laravel should be escaping those backslashes and also vague suggestion of server settings being relevant).
Sorry I can't be more specific!!

Resources