为查看、编辑和删除按钮创建关系,并在 laravel 应用程序中为相应的货件显示特定的 view/page

creating a relationship for view, edit and delete buttons and displaying a particlar view/page for the respective shipment in a laravel app

谁能帮我解决这个问题,我一直在尝试在我的网站页面上获取货件上的查看、编辑和删除按钮 table,以便在单击时显示相应货件按钮的视图.但是视图必须针对每个装运,其中 link 放置在 table 行中,即每组行按钮应显示 [=33] 上特定装运数据的视图=]行。

这是我的 blade 模板,用于显示所有装运 table:

<table id="shipment-list" class="table table-hover table-sm">
            <thead>
                <tr>
                    <th class="form-check">
                        <input class="form-check-input " id="wpcfe-select-all" type="checkbox">
                        <label class="form-check-label" for="materialChecked2"></label>
                    </th>
                    <th>Tracking Number</th>
                    <th class="no-space">Shipper Name</th>
                    <th class="no-space">Receiver Name  </th>
                    <th>Status</th>
                    <th>Shipment Type</th>                              
                    <th class="text-center">View</th>
                    <th class="text-center">Print</th>
                    <th class="text-center">Update</th>
                    <th class="text-center">Delete</th>
                </tr>
            </thead>
            <tbody>
                    @if(count($shipment) >0)
                    @foreach($shipment as $shipment)

                <tr id="shipment-2928" class="shipment-row pending">
                    <td class="form-check">
                        <input class="wpcfe-shipments form-check-input " type="checkbox" name="wpcfe-shipments[]" value="2928" data-number="MSD076941-CARGO">
                        <label class="form-check-label" for="materialChecked2"></label>
                    </td>
                    <td><a href="#" class="text-primary font-weight-bold" name="tracking_code">{{$shipment->tracking_code }}</a></td>
                    <td class="no-space">{{$shipment->sender_name }}</td>
                    <td class="no-space">{{$shipment->receiver_name }}</td>
                    <td class="shipment-status pending">{{$shipment->status }}</td>
                    <td class="shipment-type default">{{$shipment->shipment_type }}</td>                                    
                    <td class="text-center">
                        <a href="/single" title="View">
                            <i class="fa fa-list text-success"></i>
                        </a>
                            </td>
                    <td class="text-center print-shipment">
                <div class="dropdown">
                    <!--Trigger-->
                    <button class="btn btn-default btn-sm dropdown-toggle m-0 py-1 px-2 waves-effect waves-light" type="button" id="dropdownPrint" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true"><i class="fa fa-print"></i></button>
                    <!--Menu-->
                    <div class="dropdown-menu dropdown-primary" x-placement="bottom-start" style="position: absolute; will-change: transform; top: 0px; left: 0px; transform: translate3d(0px, 38px, 0px);" x-out-of-boundaries="">
                        <a class="dropdown-item print-invoice py-1" data-id="2928" data-type="invoice" href="#">Invoice</a>
                        <a class="dropdown-item print-label py-1" data-id="2928" data-type="label" href="#">Label</a>
                        <a class="dropdown-item print-waybill py-1" data-id="2928" data-type="waybill" href="#">Waybill</a>
                    </div>
                </div>
                </td>
                <td class="text-center wpcfe-action">                                   
                    <a href="#" title="Update">
                        <i class="fa fa-edit text-info"></i>
                    </a>                                    
                </td>
                <td class="text-center">
                    <a href="#" class="wpcfe-delete-shipment" data-id="2928" title="Delete"><i class="fa fa-trash text-danger"></i></a>
                </td>   
                                                    
            </tr>
            @endforeach

            @else
                <tr>
                    <td colspan="6">
                        <strong>No Users Found !!!</strong>
                    </td>
                </tr>
            @endif
        </tbody>
        </table>
    </div>

这里是家庭控制器,显示所有出货量(admin-index)page/view:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Shipment;
use DB;

class HomeController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Contracts\Support\Renderable
     */
    public function index()
    {
        $page_title = "Admin Dashboard";
        $shipment = Shipment::latest()->get();
        return view('home', compact('shipment','page_title'));
        
      
    }
}

这是我在装运控制器中尝试使用 table 上的按钮显示单个视图的方法。但我知道这离我应该做的还差得远:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Str;
use App\Shipment;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\MessageBag;




class ShipmentController extends Controller
{
   
    /**
     * Create a new user instance after a valid registration.
     *
     * @param  Request  $request
     * @return \App\Shipment
     */
    protected function store(Request $request)
    {
        
        $shipment = new Shipment();

        $data  = $this->validate($request, [
            'sender_name' => 'required|string|max:255',
            'sender_email' => 'required|string|email|max:255',
            'receiver_email' => 'required|string|email|max:255',
            'sender_address' => 'required|string',
            'estimated_Date' => 'required||date|after:today',
            'shipment_type' => 'required|string|max:255',
            'content' => 'required|string|max:255', 
            'receiver_name' => 'required|string|max:255', 
            'receiver_address' => 'required|string|max:255', 
            'sender_country' => 'required|string|max:255', 
            'sender_telephone' => 'required|string|max:255',
            'comments' => 'required|string|max:255',
            'receiver_telephone' => 'required|string|max:255',
            'receiver_country' => 'required|string|max:255',
            'package_weight' => 'required|string|max:255',
            
        ]);
        

        $shipment->sender_name = $request->input('sender_name');
        $shipment->receiver_email= $request->input('receiver_email');
        $shipment->sender_email= $request->input('sender_email');
        $shipment->sender_address= $request->input('sender_address');
        $shipment->estimated_Date= $request->input('estimated_Date');
        $shipment->shipment_type= $request->input('shipment_type');
        $shipment->content= $request->input('content');
        $shipment->receiver_name= $request->input('receiver_name');
        $shipment->receiver_address= $request->input('receiver_address');
        $shipment->sender_country= $request->input('sender_country');
        $shipment->sender_telephone= $request->input('sender_telephone');
        $shipment->comments= $request->input('comments');
        $shipment->receiver_telephone= $request->input('receiver_telephone');
        $shipment->receiver_country= $request->input('receiver_country');
        $shipment->package_weight= $request->input('package_weight');
        $shipment->tracking_code = strtoupper(Str::random(20));
        
        $shipment->save();   

           
            return redirect('/index')->with('Success', 'Your Shipment has been created');
       
    }

    public function tracking(Request $request){

        $this->validate($request,
            [
                'tracking_code' => 'required|max:25',

            ]);

        $shipment = Shipment::where('tracking_code', $request->input('tracking_code'))->first();
        
        if ( $shipment == null)
        {
            
            return redirect('/shipment/track')->with('error', 'Incorect Tracking Number');
            
        }
        else{
            return view('shipment.single')->with('shipment', $shipment);
        }

    }



    public function single(request $request ){

        $page_title = "view shipment";
        $tracking_code = Shipment::get()->tracking_code;
        $shipment= Shipment::where('tracking_code', $tracking_code)->first();
        
        return view('shipment.single', compact('tracking_code','shipment','page_title'));
       
    }
    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $request
     * @return \Illuminate\Http\Response
     */
    public function edit(Request $request)
    {
        $shipment = Shipment::where($shipment->tracking_code );

        //check for Admin user
        if (Gate::allows('isAdmin')) {

            return view('shipment.edit')->with('shipment', $shipment);
            
        }else {
            return redirect('/home')->with('error', 'Unauthorized Page');
        }
       
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        
    }

这是我的路线:

Route::get('/single', 'ShipmentController@single')->name('single');

Route::get('/shipment/createnew', 'PagesController@createnew');

Route::Post('/shipment/storenew', 'ShipmentController@store')->name('storenew');

Route::get('/shipment/edit', 'ShipmentController@edit')->name('edit');



Route::Post('/tracking', 'ShipmentController@tracking')->name('tacking');

Auth::routes();

Route::get('/home', 'HomeController@index')->name('home');

如何创建一个关系,当单击相应的查看、编辑和删除按钮时,该关系可用于获取特定货件? 我的数据库 table 上的每批货物都有一个唯一的 ID 和 tracking_code 列。如何使用它们作为关系?

在您的 foreach loop 中,因为您有一列用于 deleteedit,因此您可以拥有类似的内容。我只为 edit

这样做

Blade 查看

<td class="text-center">
<a href="{{route('shipment.edit',$shipment->id)}}" class="wpcfe-edit-shipment" data-id="2928" title="Edit"><i class="fa fa-edit text-primary"></i></a>
</td>

web.php

Route::get('/shipment/edit/{shipment}', 'ShipmentController@edit')->name('shipment.edit');

控制器

public function edit(Shipment $shipment){
return view('shipmentview',compact('shipment'));
}

您可以按照以下相同的方式进行删除。

blade 查看

<td class="text-center">
    <a href="{{route('shipment.delete',$shipment->id)}}" class="wpcfe-edit-shipment" data-id="2928" title="Delete"><i class="fa fa-trash text-danger"></i></a>
    </td>

web.php

Route::get('/shipment/{shipment}/delete', 'ShipmentController@delete')->name('shipment.delete');

控制器

public function delete(Shipment $shipment){
$shipment->delete();
return back();
}