How to create a new many to many record with attach - laravel-5

Okey, i seen some posts about this but i don't understand the concept of attach at all, i have three tables:
Llistes(Lists):
$table->increments('id');
$table->string('nom_llista');
$table->integer('user_id')->unsigned();
});
Cancons(Songs):
$table->increments('id');
$table->string('titol');
$table->integer('genere_id')->unsigned();
$table->integer('artista_id')->unsigned();
$table->integer('album_id')->unsigned();
Pivot table: llistes_cancons (lists_songs):
$table->increments('id');
$table->integer('id_canco')->unsigned();
$table->integer('id_llista')->unsigned();
$table->timestamps();
I have two other Classes that i think that are correct, but i''m not sure:
In Canco.php (Song.php):
public function llistescancons_llistes()
{
return $this->belongsToMany('App\Llista');
}
In Llista.php (List.php):
public function llistescancons_cancons()
{
return $this->belongsToMany('App\Canco');
}
So, the question is how can I implement in my controller a function that let me add new record to the pivot table (many to many) and if it's possible another funtion to show the records, i'm newbie in Laravel and it's a bit hard for me.

There's no need to implement methods to add/remove records from the pivot table. Eloquent has attach/detach methods that can do that for you, but first you need to give Eloquent the column names of the pivot table since you are not using Eloquent's column name convention.
In Canco.php (Song.php):
public function llistescancons_llistes()
{
return $this->belongsToMany('App\Llista','llistes_cancons','id_canco','id_llista');
}
In Llista.php (List.php):
public function llistescancons_cancons()
{
return $this->belongsToMany('App\Canco','llistes_cancons','id_llista','id_canco');
}
Then if you want to attach a song to list, you can easily use the song id to do that
$list = App\Llista::find(1);
$list->llistescancons_cancons()->attach($songId);
or the other way around
$song = App\Canco::find(1);
$song->llistescancons_llistes()->attach($listId);

Related

Can I create a double-morphed many-to-many relationship in Laravel?

I have two models: Product and Group, which I want to both give a many-to-many 'shown' and a 'recommended' relation with itself and the other (so both have has to be morphed in both ways).
So a 'shown' resource of either type has a set of 'recommended' resources of either type (the Groups are actually acting as a container for grouped Products).
The table should be pretty straightforward:
Schema::create('recommendables', function (Blueprint $table) {
$table->id();
$table->morphs('shown');
$table->morphs('recommendable');
$table->timestamps();
});
Then relations are defined in the trait 'RecommendableTrait' which is then given to both classes. As far as these relationships are concerned both classes are then handled pretty much the same later on (even to be merged into a single collection), so there is merit in trying to do it this way.
The problem is that the following relation definitions won't work, because it will look for group_id if I call the morphed relations on Group, similarly with `morphedByMany'.
trait RecommendableTrait {
// RECOMMENDED GROUPS
public function recommendedGroups()
{
return $this->morphToMany(Group::class, 'recommendable', 'recommendables')
->withTimestamps();
}
// RECOMMENDED PRODUCTS
public function recommendedProducts()
{
return $this->morphToMany(Product::class, 'recommendable', 'recommendables')
->withTimestamps();
}
// SHOWN GROUPS
public function shownGroups()
{
return $this->morphToMany(Group::class, 'shown', 'recommendables')
->withTimestamps();
}
// SHOWN PRODUCTS
public function shownProducts()
{
return $this->morphToMany(Product::class, 'shown', 'recommendables')
->withTimestamps();
}
}
Now I know, I'm looking to create a bit of a weird relation tangle here. But I'm hoping defining a double-morphed many-to-many is possible in Laravel?
If not, I guess I'll have to either split it up in two single-morphed many-to-Many relationship sets for both classes. Which would be annoying because of the code that goes on top of these relations would have to be needlessly duplicated. Or I'll have to forego relations and just DB query the results directly. Thus giving up some of Eloquence's convenience, but at least allowing me to keep the code confined to a single shared trait.

Laravel - is there a library that lets me do a hasManyThrough relationship where my models PK is either value

Is there a library where I am able to use a pivot table that points to the same model twice (i.e User) where I can have the PK as either column 2 or 3 for something like a friending or associating system?
Code Example
migration
Schema::create('friends', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('requestor_id');
$table->unsignedBigInteger('requested_id');
$table->boolean('accepted')->default(0);
});
model
public function friends(){
return $this->[LibraryMethod](Friend::class, 'requestor_id', 'requested_id')->where('accepted', 1);
}
So then I could then either look at 'requestor_id' or 'requested_id' for my PK and get the PK of an associated User.
Ideally this should also work with eloquent functions.
I recognise that friends isn't the best example as you could have a handshake-like system, but for simple associations (i.e, between products) something like this would be pretty useful.

Laravel multiple relation in a same table

In my project, users can like & comment on feeds & forums. So, there is a contribution page where the user can see where he has provided his input (like or comment) sorted by created_at time.
There may be another feature in future like feed & forum where user can also provide like & comment.
In my contribution page, I want to list data like this -
You have commented on user_2's feed feed_title at created_at - comment
You have liked user_2's feed feed_title at created_at
You have commented on user_3's forum forum_title at created_at - comment
You have liked user_3's forum forum_title at created_at
But I am stuck in database design. So far I am trying this -
Schema::create('contributions', function (Blueprint $table) {
$table->uuid('id')->primary();
$table->uuid('user_id');
$table->uuid('contribution_id'); // id of my like/comment
$table->string('contribution_type'); // feed_like/feed_comment/forum_like/forum_comment
$table->uuid('target_id'); // id of feed/forum where I provided comment or like
$table->timestamps();
});
But it will cause a query loop when I retrieve the data. So, is there any better approach to what I am trying to get?
You are probably looking for Polymorphic Relationships.
That enables you to simplify the relationship by providing an ID of the related model and a naming of the related model instead.
A sample migration would look like this, using the morph method as inspiration (since you're using UUID's):
Schema::create('contributions', function (Blueprint $table) {
$table->uuid('id')->primary();
$table->uuid('user_id');
$table->uuid('contributable_id');
$table->string('contributable_type');
$table->timestamps();
});
This should enable you to do something like:
class Contribution extends Model {
public function contributable() {
return $this->morphTo();
}
}
class User extends Model
{
/**
* Get the user's contributions.
*/
public function contributions()
{
return $this->morphToMany(Contribution::class, 'contributable');
}
}
You should be able to retrieve the users contributions that way and defining the action based on the morphed instance type.

(livewire-datatables) Can't access to my two relationships to the same model

I have two foreign key related to the same table
When I try to add these two columns to datatable all data resolve from only the first one of them.
My code
cities
$table->id();
$table->string('name');
trips
$table->id();
$table->foreignId('from_ctiy_id')->references('id')->on('cities');
$table->foreignId('to_ctiy_id')->references('id')->on('cities');
in Trip model I make two relationships
public function from_ctiy()
{ return $this->belongsTo(City::class, 'from_ctiy_id'); }
public function to_ctiy()
{ return $this->belongsTo(City::class, 'to_ctiy_id'); }
It works fine I can access to these two columns normally
$trip->from_city->name;
$trip->to_city->name;
But when I create columns in livewire-datatable
Column::name('from_city.name')->lable('From'),
Column::name('to_city.name')->lable('To'),
I get two columns table the same data from only the first one of them
From
To
Fcity
Fcity
Fcity
Fcity
While should I get Fcity and Tcity
U have to build manually the join query like below.
public function builder()
{
return Trip::query()
->leftJoin('City as c1', 'from_ctiy_id', 'c1.id')
->leftJoin('City as c2', 'to_ctiy_id', 'c2.id');
}
public function columns()
{
return [
Column::name('c1.name')->label("From"),
Column::name('c2.name')->label("To")
];
}

Laravel 5 - defining relationships

everything was working fine with a single Model, but now I am implementing more, I have noticed an issue.
I have several document Models which represent a different type of document. For now, let's say I have DocumentA and DocumentB.
Each Document allows file uploads, so I have created a FileUpload Model. A Document can have many FileUploads.
So, seems pretty straight forward at this point. My FileUpload table has a documentId field, which is a reference to the id field of the Document that is using it.
In DocumentA, I have something like so
public function uploadFile()
{
return $this->hasMany('App\UploadFile', 'documentId');
}
So DocumentA can have many UploadFiles, linked by the documentId.
DocumentB has the same function within its Model.
My problem lies with the UploadFiles model. Firstly, this model now has two belongTo events e.g.
public function documentA()
{
return $this->belongsTo('App\DocumentA', 'documentId');
}
public function documentB()
{
return $this->belongsTo('App\DocumentB', 'documentId');
}
This could be the problem, not sure if I can have multiple belongs to? My immediate problem however is to do with the migration of the doc_file table. At the moment I have this
Schema::table('doc_file', function (Blueprint $table) {
$table->integer('documentId')->unsigned()->default(0);
$table->foreign('documentId')->references('id')->on('document_a')->onDelete('cascade');
});
Schema::table('doc_file', function (Blueprint $table) {
$table->integer('documentId')->unsigned()->default(0);
$table->foreign('documentId')->references('id')->on('document_b')->onDelete('cascade');
});
So I am trying to provide foreign keys to my documents. When I try to migrate, it tells me that
Column already exists: 1060 Duplicate column name documentId
Am I handling my relationships correctly? Essentially, I have many documents, and each document can have many files.
Any assistance with my database relationships appreciated.
Many thanks
Looking at your problem at first glance, it seems that there is a little confusion for you regarding the concept model.
The Model Concept
A model is in fact a conceptualization of a real-world object as it is used to represent a real-world entity.
In other words, it represents a whole class of objects with similar properties. For instance a Car model would represent all cars, whether they are of type Lamborghini or Mercedez. The fact is that they all come under the Car classification.
Same concept goes in Eloquent, and with your use case; therefore a Document model is sufficient to represent both of your documents (DocumentA and DocumentB).
Eloquent Relationships
To refine what you've achieved so far, your models' relationships can be refactored as such:
Document Model
public function fileUploads(){
return $this->hasMany('App\FileUpload');
}
FileUpload Model
public function document(){
return $this->belongsTo('App\Document');
}
Based on the relationship "EACH document has MANY file uploads", and the inverse "EACH file upload BELONGS to exactly one document", as you can see, there is only one belongsTo() method in the FileUpload model to define the latter part of the relationship.
Similarly, the schema for the tables defining the above relationship are as follows:
// Schema for Document table
Schema::table('document', function (Blueprint $table) {
$table->increment('id');
$table->string('name', 100);
});
// Schema for FileUpload table
Schema::table('doc_file', function (Blueprint $table) { // file_uploads would have been a more friendly name in my opinion
$table->integer('documentId')->unsigned()->default(0); // note that `documentId` is interpreted as `documentid` in MySQL
$table->foreign('documentId')->references('id')->on('document')->onDelete('cascade');
});

Resources