Nameless_0's avatar

Laravel 5.5 - Trying to display some data from another table with pagination

Hi everyone. I have this crud app but I want to display some data from my other table (inventory table). I am using pagination

Here is my Employee Controller:


<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Employee;
use App\Http\Resources\Employee as EmployeeResource;

class EmployeeController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
   
        $employees = Employee::orderBy('created_at', 'desc')->paginate(5);

        return EmployeeResource::collection($employees);
        
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $employee = $request->isMethod('put') ? Employee::findOrFail($request->employee_id) : new Employee;

        $employee->id = $request->input('employee_id');
        $employee->last_name = $request->input('last_name');
        $employee->first_name = $request->input('first_name');
        $employee->middle_name = $request->input('middle_name');
        $employee->email = $request->input('email');

        if($employee->save()) {
            return new EmployeeResource($employee);
        }

    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        // Get employee info
        $employee = Employee::findOrFail($id);
        
        //return the single employee as a resource
        return new EmployeeResource($employee);
    }


    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        // delete employee info
        $employee = Employee::findOrFail($id);

        if($employee->delete()) {
            return new EmployeeResource($employee);
        }
    }
}


0 likes
9 replies
staudenmeir's avatar

Does Employee have a relationship with Inventory?

1 like
Nameless_0's avatar

Yes. My othe table is actually named invsystems. And "employee_id" on my inventory table. Here is the employee model:

<?php
 
namespace App;

use Illuminate\Database\Eloquent\Model;

class Employee extends Model
{
    public function Invsystem()
    {
        return $this->hasMany(Invsystem::class);
    }
}

Nameless_0's avatar

Do I have to edit the inventory controller? Thanks again.

staudenmeir's avatar

I didn't realize you were talking about a different controller. Don't you want to return the $employees from EmployeeController::index() with their Invsystem data?

1 like
Nameless_0's avatar

I just edited the invsystem controller just in case it was needed. Though you're correct, I want to be able to display $employees with Invsystem data. Thanks again.

staudenmeir's avatar

You only have to replace the line in EmployeeController::index().

1 like
Nameless_0's avatar

I am trying to display the data in vue? Can you help me with my code? I already fixed both of the db's model. All that is left is to display it in the frontend. Really appreciate the help. Thanks.

invsystem model:

<?php

namespace App; 

use Illuminate\Database\Eloquent\Model; 

class Invsystem extends Model
{
    public function employee()
    {
        // return $this->hasMany(Employee::class);
        return $this->hasMany('App\Employee');
    }
}

employee model:

<?php

namespace App;
 
use Illuminate\Database\Eloquent\Model;

class Employee extends Model
{
    public function invsystem()
    {
        // return $this->belongsTo(Invsystem::class);
        return $this->belongsTo('App\Invsystem');
    }
}

I am trying to display the employee data along the invsystem data in this cards:

<div class="card card-body border-success text-green mb-2" v-for="invsystem in invsystems" v-bind:key="invsystem.id">
            <h5><b>stock name:</b> {{ invsystem.stock_name }}</h5>
            <h5><b>serial number:</b> {{ invsystem.serial }}</h5>
            <h5><b>model:</b> {{ invsystem.model }} </h5>
            <h5><b>product number: </b> {{ invsystem.product_no }}</h5>
            <p><b>remarks:</b> {{ invsystem.remarks }} </p>
            <!-- <p>{{ $employee->employee->email }} </p> -->
            <hr>
        <div>

Please or to participate in this conversation.