Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
942 views
in Technique[技术] by (71.8m points)

php - Laravel - Get data between tables using foreign key in Eloquent

I want to get a fields from a controller with the data of another class using foreign key with Eloquent. I'm not sure if it's possible or not.

The scenario

Model Job

class Job extends Model
{
    use HasFactory;

      /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'id',
        'job_name',
        'id_customer',
    ];

    // relación 1:n de Client amb Feines
    public function customer()
    {
        return $this->belongsTo(Customer::class, 'id_customer');

    }
    //Relacion 1:N de Jobs i Taskjob 
    public function TaskJobs()
    {
        return $this->hasMany(TaskJob::class);
    }  
}

Model customer

class Customer extends Model
{
    use HasFactory;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'id',
        'customer_name',
    ];


    //Relacion 1:N de Clients i Feines 
    public function jobs()
    {
        return $this->hasMany(Job::class);
    }  
}

Model TaskJob

class TaskJob extends Model
{
    use HasFactory;
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'id',
        'id_job',
        'id_concept',
        'id_user',
        'start_task',
        'finish_task',
        'quantity',
        'timetask',

    ];
    //Relacion 1:N de Jobs i Taskjob 
    public function jobs()
    {
        return $this->belongsTo(Job::class, 'id_job');
    } 
    //Relacion 1:N de Concept i Taskjob 
    public function concepts()
    {
        return $this->belongsTo(Concept::class, 'id_concept');
    }
    //Relacion 1:N de Users i Taskjob 
    public function users()
    {
        return $this->belongsTo(User::class, 'id_user');
    }  
}

The foreign key allow the access of data between class. For example I can get an object of TaskJob and I can get the customer_name or another data from another class.

From TaskJobController:

public function index()
{
    $usuario = auth()->user();
    $taskJobs= TaskJob::where('id_user', $usuario->id)->orderBy('start_task', 'DESC')->Paginate(10);
    return view('taskjobs.index')
        ->with('taskjobs', $taskJobs)
        ->with('usuario', $usuario);
}

And from view I can show the data as:

 @foreach($taskjobs as $taskJob)
    <tr>
        <td>{{ $taskJob->jobs->customer->customer_name}}</td>
        <td>{{ $taskJob->jobs->job_name }}</td>
        <td>{{ $taskJob->concepts->concept_name }}</td>
    </tr>
 @endforeach

This works...

But now I want to send a json to Vue component. I'm thinking in modify the controller to get the info and send to json:

public function getCurrentTasks()
{
    $data = TaskJob::select('id','id_job','id_concept','id_user')->orderBy('id')->get();
    return response()->json($data);
}

This works but I need to get data from 'TaskJob/id_job' to 'Job/job_name' I don't know how to it.

Maybe:

$data = TaskJob::select('id','id_job'->'Jobs::job_name','id_job'->'Jobs::id_customer'->'Customer::customer_name','id_user')->orderBy('id')->get();

It's wrong...

Please Could you help to get the data from another table to foreign key in eloquent?

Thanks;

question from:https://stackoverflow.com/questions/66064203/laravel-get-data-between-tables-using-foreign-key-in-eloquent

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

use this

I am not sure but try this

public function index()
{
    $usuario = auth()->user();
    $taskJobs= TaskJob::(['rel1','rel2'])->where('id_user', $usuario->id)->orderBy('start_task', 'DESC')->Paginate(10);
    return view('taskjobs.index')->with(['taskjobs', 'usuario');
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...