Laravel table pivot

Fermé
anthonyr_25 Messages postés 165 Date d'inscription mercredi 5 janvier 2022 Statut Membre Dernière intervention 6 juillet 2022 - Modifié le 1 juil. 2022 à 11:44
anthonyr_25 Messages postés 165 Date d'inscription mercredi 5 janvier 2022 Statut Membre Dernière intervention 6 juillet 2022 - 6 juil. 2022 à 13:59
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
A voir également:

1 réponse

anthonyr_25 Messages postés 165 Date d'inscription mercredi 5 janvier 2022 Statut Membre Dernière intervention 6 juillet 2022 6
1 juil. 2022 à 16:22
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
anthonyr_25 Messages postés 165 Date d'inscription mercredi 5 janvier 2022 Statut Membre Dernière intervention 6 juillet 2022 6
6 juil. 2022 à 13:59
Si quelqu'un as une idée... peux être que ma question est mal formulée ?
1