0

I'm quite new to laravel and I'm trying to understand the Eloquent Relations. I've already read some answers and the documentation but I haven't found a simple case similar to mine.

I have two model with one-to-many relation.

Document Model

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Document extends Model
{   
      public function dossiers()
      {
            return $this->belongsTo('App\Dossier');
      }
protected $table = 'documents'; 
protected $primaryKey = 'id_document';    
}

Dossier Model

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;

class Dossier extends Model
{

    public function documents()
    {
        return $this->hasMany('App\Document');
    }

    protected $table = 'dossiers'; 
    protected $primaryKey = 'id_dossier';    
}

So there is an attribute "protocol" inside both table. Inside Dossier is an unique attribute, inside Document can be repeated because more documents may be part of same dossier with that protocol.

Let's suppose I have to create a Document Model. But I need an attribute "color" that already exists in a Dossier. so:

  1. I have a form for the document. That can set all attribute except color (so it can create the protocol shared with Dossier)
  2. I submit the form then check if exists a Dossier with that protocol (that is not the primary_key)
  3. If i find a Dossier with that protocol, I took the Color of that dossier and I insert it in the Document Model.

    I'd like to know how I should implement the second step.

At the moment I've wrote in the DocumentController:

$document = Dossier::where('protocol', '=', $request->protocol)->first();

and then

$document -> color;

But I fell that's not the way. Thanks for any advice

2 Answers 2

2

I think you want to insert color in document from dossier table if protocol match. If i understand you correctly then you can write it like this.

$dossier = Dossier::where('protocol', '=', $request->protocol)->first(); 
$color = ($dossier) ? $dossier->color: 'defaultColor';

Now in your create document

$document -> create([
    'color' => $color,
    ... other form data here

]);

Note: it is not good practice to use primary key field as id_document or id_dossier. Better you change these to id, otherwise your current relationship will not work. Default, laravel relationship assume id as primary key. If you want to use different primary key name then you need to pass that key name in relationship as a second parameter.

$this->hasMany('App\Comment', 'foreign_key', 'local_key'); 

https://laravel.com/docs/5.6/eloquent-relationships#one-to-many

Sign up to request clarification or add additional context in comments.

Comments

0

There are manyway to write the relation code, one is as rkj said anad u did as well. The other one which i personally prefer is

$this->hasManny(Comment::class,'foreign_key', 'local_key'); //This is only if you are not following laravel standard  table syntex.

But if your local key is id and foreign key on another table is comment_id. Then you dont need to add foreign key and primary key. You can simply do like this

 $this->hasManny(Comment::class);

For making query, You can simply do this

$dossier = Dossier::where('protocol', $request->protocol)->first();  //This will give object

You do not need = , if its other then that u need to add that. This way its looks much cleaner right?

Then to display you can simply do this $dossier->color

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.