Laravel table pivot

anthonyr_25 Messages postés 172 Statut Membre -  
anthonyr_25 Messages postés 172 Statut Membre -
Bonjour,

(laravel, jetstream)

J'utilise une table pivot qui s'appelle category_inflow pour lier ma table inflows avec ma table categories en n:n et maintenant j'aimerais afficher les valeurs de inflows avec leurs categories correspondante mais je n'y arrive pas :

Voici ce que j'ai essayer :

(dans mon InflowController)
use App\Models\Category;
use App\Models\Inflow;
use App\Models\User;

public function index()
    {
        $inflows = Inflow::with('user')->get();
        $categories = Category::where('id', 'user_id')->get();
        return view('stage.inflow', compact('inflows', 'categories'));
    }


(dans mon inflow.blade.php)
@foreach ($inflows as $inflow)
            <tr class="bg-white border-b dark:bg-gray-800 dark:border-gray-700">
                <th scope="row" class="px-6 py-4 font-medium text-gray-900 dark:text-white whitespace-nowrap">
                    {{ $inflow->user->name }}
                </th>
                <td class="px-6 py-4">
                  {{ Str::limit($inflow->name, 25) }}
                </td>
                <td class="px-6 py-4">
                  {{ $inflow->created_at }}
                </td>
                <td class="px-6 py-4">
                  {{ $inflow->value }}
                </td>
                <td class="px-6 py-4">
                  {{ $categories->name }}
                </td>
                <td class="px-6 py-4 text-right">
                    <a href="#" class="font-medium text-blue-600 dark:text-blue-500 hover:underline">Edit</a>
                </td>
            </tr>
          @endforeach


Pour les données de mes tables users et inflows, je les récupère, mais pour ma categories cela ne marche pas, dois-je faire un model et un controller pour ma table pivot ? Sinon, comment puis-je récupérer les valeurs de inflows ainsi que leurs categories associé ?

Configuration: Configuration: Windows / Chrome 102.0.0.0

1 réponse

  1. anthonyr_25 Messages postés 172 Statut Membre 6
     
    j'ai modifier un peu ma function dans mon controller :
    public function index()
        {
            $inflows = Inflow::with('category', 'user')->get();
            $categories = Category::get();
            return view('stage.inflow', compact('inflows', 'categories'));
        }
    


    et j'ai modifier mon blade :
    @foreach ($inflows as $inflow)
                <tr class="bg-white border-b dark:bg-gray-800 dark:border-gray-700">
                    <th scope="row" class="px-6 py-4 font-medium text-gray-900 dark:text-white whitespace-nowrap">
                        {{ $inflow->user->name }}
                    </th>
                    <td class="px-6 py-4">
                      {{ Str::limit($inflow->name, 25) }}
                    </td>
                    <td class="px-6 py-4">
                      {{ $inflow->created_at }}
                    </td>
                    <td class="px-6 py-4">
                      {{ $inflow->value }}
                    </td>
                    <td class="px-6 py-4">
                      {{ $inflow->category->name }}
                    </td>
                    <td class="px-6 py-4 text-right">
                        <a href="#" class="font-medium text-blue-600 dark:text-blue-500 hover:underline">Edit</a>
                    </td>
                </tr>
              @endforeach
    


    Il me sort l'erreur suivante :
    Attempt to read property "name" on null
    1
    1. anthonyr_25 Messages postés 172 Statut Membre 6
       
      Si quelqu'un as une idée... peux être que ma question est mal formulée ?
      1