Autour de Laravel

Création projet laravel

Avatar de samsam
samsam

Bonjour a tous j'ai un petit projet laravel contient (projets-automates...) j'ai crée plusieurs projets et plusieurs automates et je dois afficher dans chaque projet une liste de ses automates, comment faire ca voila le code du model et controller de projet et automate pour model du projet

?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Project extends Model
{
    use HasFactory;
    protected $fillable = [
        'title',
    ];

    public function automates()
    {
        return $this->hasMany(Automate::class);
    }
    public function proprietes()
    {
        return $this->hasMany(Propriete::class);
    }
}

et son controller

<?php

namespace App\Http\Controllers;

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

class ProjectController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $project = project::latest()->paginate(10); //nombre de project a afficher
        return view('project.index', compact('project'));
    }

    public function create()
    {
        return view('project.create');
    }

    public function store(Request $request)

    {
        $request->validate([
            'title'=>'required',
            ]);

            $project = project::create($request->all());

      return redirect()->route('projects.index')
      ->with('Success', 'project added successfully');

    }

    public function show(project $project)
    {
        return view('project.show', compact('project'));

    }

    public function edit(project $project)
    {
         return view('project.edit', compact('project'));
            }


    public function update(Request $request, project $project)
    {
        $request->validate([
            'title'=>'required',
            ]);

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

      return redirect()->route('projects.index')
      ->with('Success', 'project updated successfully');
    }


    public function destroy(project $project)
    {
       $project->delete();
       return redirect()->route('projects.index')
      ->with('Success', 'project deleted successfully');
    }
}

et pour les automates

<?php

namespace App\Models;

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

class Automate extends Model
{
    use HasFactory;
    protected $fillable = [
        'name',
        'project_id',
        'is_root',
    ];
    public function states()
    {
        return $this->hasMany(State::class);
    }
    public function transitions()
    {
        return $this->hasMany(Transition::class);
    }
}

et son controller

<?php

namespace App\Http\Controllers;

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

class AutomateController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $automate = Automate::all(); // afficher toutes les automates
        return view('automate.index', compact('automate'));
    }


    public function create()
    {
        return view('automate.create');
    }
    public function store(Request $request)
    {
        $request->validate([
        'name'=>'required',
        'project_id'=>'required',
        'is_root'=>'required',
            ]);

            $automate = Automate::create($request->all());

      return redirect()->route('automates.index')
      ->with('success', 'automate added successfully');

    }

    public function show(Automate $automate)
    {
        return view('automate.show', compact('automate'));
    }
    public function edit(Automate $automate)
    {
        return view('automate.edit', compact('automate'));

    }


    public function update(Request $request, Automate $automate)
    {
        $request->validate([
        'name'=>'required',
        'project_id'=>'required',
        'is_root'=>'required',
            ]);

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

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

    public function destroy(Automate $automate)
    {
        $automate->delete();
        return redirect()->route('automates.index')
       ->with('success', 'automate deleted successfully');
    }
}

Posté il y a 4 mois

Vous ne pouvez pas répondre à ce sujet.