J'ai créé un site de crud de base en utilisant Laravel 8.

L'application créée est capable de répertorier les données d'une base de données MySQL locale et de créer un nouvel objet, mais il ne peut pas afficher les détails, modifier et supprimer un objet. L'erreur indiquée sur la page d'édition est la suivante:

Paramètre requis manquant pour [Itinéraire: Tâches.Update] [URI: Tâches / {Tâche}] [Paramètre manquant: tâche]. (Vue: /USERSERS/ADV/DOCUMENT/WORKSPACE/UNLOCKER/HOBBS-ADMIN/RESOURCES/VIEWS/TASKS/EDIT.BLADE.PHP)

J'utilise var_dump($hobbsTask) dans edit.blade.php et trouvé l'objet HobbsTask est vide. Comment venir?

D'autre part, le show.blade.php ne montre aucune donnée du tout. Semble c'est le même problème que la page d'édition. Qu'est-ce que j'ai raté?


Les codes sont les suivants:

app / http / contrôleurs / hobbstakcontroller

<?php

namespace App\Http\Controllers;

use App\Models\HobbsTask;
use Illuminate\Http\Request;

class HobbsTaskController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $tasks = HobbsTask::latest()->paginate(5);
        return view('tasks.index', compact('tasks'))
            ->with('i', (request()->input('page', 1) - 1) * 5);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('tasks.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $request->validate([
            'session_name' => 'required',
            'pid' => 'required',
            'file_path' => 'required',
            'file_type' => 'required',
            'file_size' => 'required'
        ]);

        HobbsTask::create($request->all());

        return redirect()->route('tasks.index')
            ->with('success', 'Task created successfully.');
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\HobbsTask  $hobbsTask
     * @return \Illuminate\Http\Response
     */
    public function show(HobbsTask $hobbsTask)
    {
        return view('tasks.show', compact('hobbsTask'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\HobbsTask  $hobbsTask
     * @return \Illuminate\Http\Response
     */
    public function edit(HobbsTask $hobbsTask)
    {
        return view('tasks.edit', compact('hobbsTask'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\HobbsTask  $hobbsTask
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, HobbsTask $hobbsTask)
    {
        $request->validate([
            'session_name' => 'required'
        ]);

        $hobbsTask->update($request->all());

        return redirect()->route('tasks.index')
            ->with('success', 'Task updated successfully');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\HobbsTask  $hobbsTask
     * @return \Illuminate\Http\Response
     */
    public function destroy(HobbsTask $hobbsTask)
    {
        $hobbsTask->delete();
        return redirect()->route('tasks.index')
            ->with('success', 'Task deleted successfully');
    }
}

app / modèles / hobbstask.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class HobbsTask extends Model
{
    use HasFactory;

    protected $table = 'unlocker_tickets';
    public $timestamps = true;

    protected $fillable = [
        'pid',
        'session_name',
        'file_type',
        'file_path',
        'file_size'
    ];
}

ressources / vues / tâches / index.blade.php

@extends('layouts.app')

@section('content')
<div class="row">
    <div class="col-lg-12 margin-tb">
        <div class="pull-left">
            <h2>Laravel 8 CRUD Example </h2>
        </div>
        <div class="pull-right">
            <a class="btn btn-success" href="{{ route('tasks.create') }}" title="Create a task"> <i class="fas fa-plus-circle"></i>
                </a>
        </div>
    </div>
</div>

@if ($message = Session::get('success'))
    <div class="alert alert-success">
        <p>{{ $message }}</p>
    </div>
@endif

<table class="table table-bordered table-responsive-lg">
    <tr>
        <th>ID</th>
        <th>Session Name</th>
        <th>PID</th>
        <th>File Path</th>
        <th>File Type</th>
        <th>File Size</th>
        <th>Date Created</th>
        <th>Actions</th>
    </tr>
    @foreach ($tasks as $task)
        <tr>
            <td>{{ $task->id }}</td>
            <td>{{ $task->session_name }}</td>
            <td>{{ $task->pid }}</td>
            <td>{{ $task->file_path }}</td>
            <td>{{ $task->file_type }}</td>
            <td>{{ $task->file_size }}</td>
            <td>{{ date_format($task->created_at, 'jS M Y') }}</td>
            <td>
                <form action="{{ route('tasks.destroy', $task->id) }}" method="POST">
                    <a href="{{ route('tasks.show', $task->id) }}" title="show">
                        <i class="fas fa-eye text-success fa-lg"></i>
                    </a>
                    <a href="{{ route('tasks.edit', $task->id) }}">
                        <i class="fas fa-edit fa-lg"></i>
                    </a>

                    @csrf
                    @method('DELETE')

                    <button type="submit" title="delete" style="border: none; background-color:transparent;">
                        <i class="fas fa-trash fa-lg text-danger"></i>
                    </button>
                </form>
            </td>
        </tr>
    @endforeach
</table>

{!! $tasks->links() !!}

@endsection

ressources / vues / tâches / edit.blade.php

@extends('layouts.app')

@section('content')
    <div class="row">
        <div class="col-lg-12 margin-tb">
            <div class="pull-left">
                <h2>Edit Task</h2>
            </div>
            <div class="pull-right">
                <a class="btn btn-primary" href="{{ route('tasks.index') }}" title="Go back"> <i class="fas fa-backward "></i> </a>
            </div>
        </div>
    </div>

    @if ($errors->any())
        <div class="alert alert-danger">
            <strong>Error!</strong>
            <ul>
                @foreach ($errors->all() as $error)
                    <li>{{ $error }}</li>
                @endforeach
            </ul>
        </div>
    @endif

    <form action="{{ route('tasks.update', $hobbsTask->id) }}" method="POST">
        @csrf
        @method('PUT')

        <div class="row">
            <div class="col-xs-12 col-sm-12 col-md-12">
                <div class="form-group">
                    <strong>Session Name:</strong>
                    <input type="text" name="session_name" class="form-control" value="{{ $hobbsTask->session_name }}">
                </div>
            </div>
            <div class="col-xs-12 col-sm-12 col-md-12">
                <div class="form-group">
                    <strong>PID:</strong>
                    <input type="number" name="pid" value="{{ $hobbsTask->pid }}" />
                </div>
            </div>
            <div class="col-xs-12 col-sm-12 col-md-12">
                <div class="form-group">
                    <strong>File Path:</strong>
                    <input type="text" name="file_path" class="form-control" value="{{ $hobbsTask->file_path }}">
                </div>
            </div>
            <div class="col-xs-12 col-sm-12 col-md-12">
                <div class="form-group">
                    <strong>File Type:</strong>
                    <input type="text" name="file_type" class="form-control" value="{{ $hobbsTask->file_type }}">
                </div>
            </div>
            <div class="col-xs-12 col-sm-12 col-md-12">
                <div class="form-group">
                    <strong>File Size:</strong>
                    <input type="number" name="file_size" value="{{ $hobbsTask->file_size }}" />
                </div>
            </div>
            <div class="col-xs-12 col-sm-12 col-md-12 text-center">
                <button type="submit" class="btn btn-primary">Submit</button>
            </div>
        </div>

    </form>
@endsection

ressources / vues / tâches / show.blade.php

@extends('layouts.app')

@section('content')
    <div class="row">
        <div class="col-lg-12 margin-tb">
            <div class="pull-left">
                <h2>{{ $hobbsTask->session_name }}</h2>
            </div>
            <div class="pull-right">
                <a class="btn btn-primary" href="{{ route('tasks.index') }}" title="Go back"> <i class="fas fa-backward "></i> </a>
            </div>
        </div>
    </div>

    <div class="row">
        <div class="col-xs-12 col-sm-12 col-md-12">
            <div class="form-group">
                <strong>Session Name:</strong>
                {{ $hobbsTask->session_name }}
            </div>
        </div>
        <div class="col-xs-12 col-sm-12 col-md-12">
            <div class="form-group">
                <strong>PID:</strong>
                {{ $hobbsTask->pid }}
            </div>
        </div>
        <div class="col-xs-12 col-sm-12 col-md-12">
            <div class="form-group">
                <strong>File Path:</strong>
                {{ $hobbsTask->file_path }}
            </div>
        </div>
        <div class="col-xs-12 col-sm-12 col-md-12">
            <div class="form-group">
                <strong>File Type:</strong>
                {{ $hobbsTask->file_type }}
            </div>
        </div>
        <div class="col-xs-12 col-sm-12 col-md-12">
            <div class="form-group">
                <strong>File Size:</strong>
                {{ $hobbsTask->file_size }}
            </div>
        </div>
    </div>
@endsection

routes / web.php

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\HobbsTaskController;

Route::get('/', function () {
    return view('welcome');
});

Route::resource('tasks', HobbsTaskController::class);

Informations de routage:

% php artisan route:list


+--------+-----------+-------------------+---------------+--------------------------------------------------+------------+
| Domain | Method    | URI               | Name          | Action                                           | Middleware |
+--------+-----------+-------------------+---------------+--------------------------------------------------+------------+
|        | GET|HEAD  | /                 |               | Closure                                          | web        |
|        | GET|HEAD  | api/user          |               | Closure                                          | api        |
|        |           |                   |               |                                                  | auth:api   |
|        | GET|HEAD  | tasks             | tasks.index   | App\Http\Controllers\HobbsTaskController@index   | web        |
|        | POST      | tasks             | tasks.store   | App\Http\Controllers\HobbsTaskController@store   | web        |
|        | GET|HEAD  | tasks/create      | tasks.create  | App\Http\Controllers\HobbsTaskController@create  | web        |
|        | GET|HEAD  | tasks/{task}      | tasks.show    | App\Http\Controllers\HobbsTaskController@show    | web        |
|        | PUT|PATCH | tasks/{task}      | tasks.update  | App\Http\Controllers\HobbsTaskController@update  | web        |
|        | DELETE    | tasks/{task}      | tasks.destroy | App\Http\Controllers\HobbsTaskController@destroy | web        |
|        | GET|HEAD  | tasks/{task}/edit | tasks.edit    | App\Http\Controllers\HobbsTaskController@edit    | web        |
+--------+-----------+-------------------+---------------+--------------------------------------------------+------------+
1
Raptor 24 mars 2021 à 09:53

1 réponse

Meilleure réponse

Parce que votre nom de paramètre de route est une tâche.

Vous devez remplacer $ HOBBSTASK à $ tâche dans Modifier et afficher la fonction.

Vous pouvez le faire comme ça

public function show(HobbsTask $task)
 {
        $hobbsTask = $task;
        return view('tasks.show',compact('hobbsTask'));
 }
public function edit(HobbsTask $task)
{
      $hobbsTask = $task;
      return view('tasks.edit', compact('hobbsTask'));
}

1
Yudiz Solutions 24 mars 2021 à 14:04