sqlite 在单个视图中显示来自Laravel中不同表的数据

o7jaxewo  于 2022-11-15  发布在  SQLite
关注(0)|答案(2)|浏览(163)

在视图中,IM正在尝试显示来自表‘类别’和‘供应商’类别的两行->CATEGORY_NAME和供应商->COMPANY。问题是,在显示的每一行数据中,我应该访问编辑、删除和更新按钮,该按钮请求每次购买的ID。目前一切正常,但我不能显示这两行。
函数show_Purcheses()负责执行此操作。

控制器

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');
    }
}

查看/刀片

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

    <div class="container p-4">
        <div>
            <table class="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 class="table-group-divider">
                @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 class="nav-link" href="/purchases/{{$purchase->id}}/edit">
                                <i class="bi bi-pencil-square"></i></a>

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

                                @csrf
                                @method('DELETE')
                                <button type="submit" class="btn btn-danger"><i class="bi bi-trash3-fill"></i></button>
                            </form>
                        </td>
                        <td>
                            <a class="nav-link" href="/purchases/{{$purchase->id}}/auto_purchase">
                                <i class="bi bi-arrow-repeat"></i></a>

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

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

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

路由

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']);

机型

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

kadbb459

kadbb4591#

如果我正确理解了您的问题和您想要做的事情,那么正确的做法应该是使用“动态属性”检索采购的类别和供应商信息:

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

<div class="container p-4">

<div>

<table class="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 class="table-group-divider">
      

    @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 class="nav-link" href="/purchases/{{$purchase->id}}/edit">
                <i class="bi bi-pencil-square"></i></a>
            
        </td>
        <td>
          <form method="POST" action="/purchases/{{$purchase->id}}">

            @csrf
            @method('DELETE')
            <button type="submit" class="btn btn-danger"><i class="bi bi-trash3-fill"></i></button>

        </form>
        </td>

        <td>
            <a class="nav-link" href="/purchases/{{$purchase->id}}/auto_purchase">
              <i class="bi bi-arrow-repeat"></i></a>
            
        </td>

      </tr>

    @endforeach

    </tbody>
  </table>

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

<a class="btn btn-primary" href="/purchases/make_purchase" role="button">Add purchase</a>

</div>

</div>


@endsection

看看这个

mnemlml8

mnemlml82#

我使用了查询,它解决了我的问题

->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 class="nav-link" href="/purchases/{{$row->id}}/edit">
                <i class="bi bi-pencil-square"></i></a>
            
        </td>
        <td>
          <form method="POST" action="/purchases/{{$row->id}}">

            @csrf
            @method('DELETE')
            <button type="submit" class="btn btn-danger"><i class="bi bi-trash3-fill"></i></button>

        </form>
        </td>

相关问题