Home > Back-end >  Displaying data from different tables in Laravel in a single view
Displaying data from different tables in Laravel in a single view

Time:09-20

In the view im trying do display two rows from tables 'categories' and 'suppliers' categories->category_name and suppliers->company. The problem is that in each row od displayed data i should acces the edit, delete and update button which requests id of each purchase. For now everything is working fine but i can not display these two rows.
The function show_purchases() is responsible For that.

Controller

use Illuminate\Http\Request;
use App\Models\Suppliers;
use App\Models\Categories;
use App\Models\Purchases;

class Purchases_Controller extends Controller
{
    public function show_purchases()
    {
        return view('bookmarks.purchases.show_purchases',
            ['purchases' => Purchases::paginate(10)]);
    }

    public function make_purchase()
    {
        $categories = Categories::get();
        $suppliers = Suppliers::get();

        return view('bookmarks.purchases.make_purchase', 
             compact('categories', 'suppliers'));
    }

    public function store_purchase_data(Request $request)
    {
        $this->validate($request, [
            'purchased_product' => ['required', 'min:3', 'max:50'],
            'categories' => 'required',
            'purchase_price' => ['required', 'min:1'],
            'quantity' => ['required', 'min:1'],
            'expiration_date' => ['required'],
            'suppliers' => 'required',
            'total_purchase_price',
        ]);

        $total = $request->quantity * $request->purchase_price;

        Purchases::create([
            'purchased_product' => $request->purchased_product,
            'category_id' => $request->categories,
            'supplier_id' => $request->suppliers,
            'purchase_price' => $request->purchase_price,
            'quantity' => $request->quantity,
            'expiration_date' => $request->expiration_date,
            'total_purchase_price' => $total,
        ]);

        return redirect('/purchases/show_purchases')
            ->with('message', 'Purchase created successfully!');
    }

    // Show edit form
    public function edit_purchase(Purchases $purchases)
    {
        $categories = Categories::get();
        $suppliers = Suppliers::get();
        
        return view('bookmarks.purchases.edit_purchase', 
            compact('categories', 'suppliers'),
            ['purchases' => $purchases]);
    }

    public function update_purchase(Request $request, Purchases $purchases)
    {
        $this->validate($request, [
            'purchased_product' => ['required', 'min:3', 'max:50'],
            'categories' => 'required',
            'purchase_price' => ['required', 'min:1'],
            'quantity' => ['required', 'min:1'],
            'expiration_date' => ['required'],
            'suppliers' => 'required',
        ]);

        $purchases->update([
            'purchased_product' => $request->purchased_product,
            'category_id' => $request->categories,
            'supplier_id' => $request->suppliers,
            'purchase_price' => $request->purchase_price,
            'quantity' => $request->quantity,
            'expiration_date' => $request->expiration_date,
        ]);

        return redirect('/purchases/show_purchases')
            ->with('message', 'Purchase updated successfully!');
    }

    public function delete_purchase(Purchases $purchases)
    {
        $purchases->delete();

        return redirect('/purchases/show_purchases')
            ->with('message', 'Purchase deleted successfully');
    }

    public function auto_purchase()
    {
        return redirect('/suppliers/show_purchases')
            ->with('message', 'Product will be purchased again as scheduled');
    }
}

View/Blade

@extends('layout')
@section('content')

    <div >
        <div>
            <table >
                <thead>
                <tr>
                    <th scope="col">Medicine</th>
                    <th scope="col">Category</th>
                    <th scope="col">Supplier</th>
                    <th scope="col">Cost</th>
                    <th scope="col">Total Cost</th>
                    <th scope="col">Quantity</th>
                    <th scope="col">Expiration date</th>
                    <th scope="col">Edit</th>
                    <th scope="col">Delete</th>
                    <th scope="col">Auto-purchase</th>
                </tr>
                </thead>
                <tbody >
                @foreach($purchases as $purchase)
                    <tr>
                        <td>{{$purchase->purchased_product}}</td>
                        <td>{{$purchase->category_name}}</td>
                        <td>{{$purchase->company}}</td>
                        <td>{{$purchase->purchase_price}}</td>
                        <td>{{$purchase->total_purchase_price}}</td>
                        <td>{{$purchase->quantity}}</td>
                        <td>{{$purchase->expiration_date}}</td>
                        <td>
                            <a  href="/purchases/{{$purchase->id}}/edit">
                                <i ></i></a>

                        </td>
                        <td>
                            <form method="POST" action="/purchases/{{$purchase->id}}">

                                @csrf
                                @method('DELETE')
                                <button type="submit" ><i ></i></button>
                            </form>
                        </td>
                        <td>
                            <a  href="/purchases/{{$purchase->id}}/auto_purchase">
                                <i ></i></a>

                        </td>
                    </tr>
                @endforeach
                </tbody>
            </table>

            @if(count($purchases)==0)
                <p>no purchases found</p>
            @endif

            <a  href="/purchases/make_purchase" role="button">Add purchase</a>
        </div>
    </div>
@endsection

Routes

Route::get('/purchases/show_purchases', [Purchases_Controller::class, 'show_purchases']);
Route::get('/purchases/make_purchase', [Purchases_Controller::class, 'make_purchase']);
Route::post('/purchases', [Purchases_Controller::class, 'store_purchase_data']);
Route::get('/purchases/{purchases}/edit', [Purchases_Controller::class, 'edit_purchase']);
Route::put('/purchases/{purchases}', [Purchases_Controller::class, 'update_purchase']);
Route::delete('/purchases/{purchases}', [Purchases_Controller::class, 'delete_purchase']);
Route::get('/purchases/auto_purchase', [Purchases_Controller::class, 'auto_purchase']);

Model

class Purchases extends Model
{
    use HasFactory;

    protected $fillable = [
        'purchased_product','category_id','supplier_id',
        'purchase_price','quantity','total_purchase_price' ,'expiration_date','image'
        
    ];

    protected $table = "purchases";

    public function suppliers(){
        return $this->belongsTo(Suppliers::class);
    }

    public function categories(){
        return $this->belongsTo(Categories::class);
    }
}

Looks like that for now

CodePudding user response:

If I understood your question correctly and what you want to do, then the correct thing would be for you to retrieve the information of categories and suppliers of purchases using the "dynamic property":

{{-- VIEW --}}
@extends('layout')
@section('content')

<div >

<div>

<table >
    <thead>
      <tr>
        {{-- <th scope="col">Name</th> --}}
        <th scope="col">Medicine</th>
        <th scope="col">Category</th>
        <th scope="col">Supplier</th>
        <th scope="col">Cost</th>
        <th scope="col">Total Cost</th>
        <th scope="col">Quantity</th>
        <th scope="col">Expiration date</th>
        <th scope="col">Edit</th>
        <th scope="col">Delete</th>
        <th scope="col">Auto-purchase</th>
      </tr>
    </thead>
    <tbody >
      

    @foreach($purchases as $purchase) 

      <tr>
        <td>{{$purchase->purchased_product}}</td>
        <td>{{$purchase->categories->category_name}}</td>
        <td>{{$purchase->suppliers->company}}</td>
        <td>{{$purchase->purchase_price}}</td>
        <td>{{$purchase->total_purchase_price}}</td>
        <td>{{$purchase->quantity}}</td>
        <td>{{$purchase->expiration_date}}</td>

        <td>
            <a  href="/purchases/{{$purchase->id}}/edit">
                <i ></i></a>
            
        </td>
        <td>
          <form method="POST" action="/purchases/{{$purchase->id}}">

            @csrf
            @method('DELETE')
            <button type="submit" ><i ></i></button>

        </form>
        </td>


        <td>
            <a  href="/purchases/{{$purchase->id}}/auto_purchase">
              <i ></i></a>
            
        </td>

      </tr>

    @endforeach

    </tbody>
  </table>

  @if(count($purchases)==0)
  <p>no purchases found</p>
  @endif

<a  href="/purchases/make_purchase" role="button">Add purchase</a>

</div>

</div>



@endsection

Check it out

CodePudding user response:

i used a query and it solved my problem

            ->join('suppliers', 'suppliers.id', '=', 'purchases.supplier_id')
            ->get(['suppliers.company', 'categories.category_name', 'purchases.purchase_price', 'purchases.quantity', 'purchases.expiration_date',
            'purchases.purchased_product', 'purchases.total_purchase_price', 'purchases.id']);

      <tr>
        <td>{{$row->purchased_product}}</td>
        <td>{{$row->category_name}}</td>
        <td>{{$row->company}}</td>
        <td>{{$row->purchase_price}}</td>
        <td>{{$row->total_purchase_price}}</td>
        <td>{{$row->quantity}}</td>
        <td>{{$row->expiration_date}}</td>

        <td>
            <a  href="/purchases/{{$row->id}}/edit">
                <i ></i></a>
            
        </td>
        <td>
          <form method="POST" action="/purchases/{{$row->id}}">

            @csrf
            @method('DELETE')
            <button type="submit" ><i ></i></button>

        </form>
        </td>
  • Related