简体   繁体   English

LARAVEL:显示属于特定 user_id 的列表

[英]LARAVEL: Display lists that belongs to a specific user_id

I'm creating a clone for a website where parents can create a list of things they need for their newborn baby so other people can buy it for them as a gift.我正在为一个网站创建一个克隆,在该网站上,父母可以为他们的新生婴儿创建一个他们需要的东西的列表,这样其他人就可以为他们购买它作为礼物。

At this moment I've managed to insert data into my table and to link that row of data to the user id (so user who is logged in and completed the form).此刻,我已经设法将数据插入到我的表中,并将该行数据链接到用户 ID(因此登录并完成表单的用户)。 I've managed to show all the lists from all the user id's but when I go to the dashboard of the authenticated user, I only want to show the lists who is linked to his user_id.我已经设法显示所有用户 id 的所有列表,但是当我转到经过身份验证的用户的仪表板时,我只想显示链接到他的 user_id 的列表。 I can't get it working but I'm sure I have to use hasMany() and belongsTo().我无法让它工作,但我确定我必须使用 hasMany() 和 belongsTo()。 This is my code:这是我的代码:

My migration:我的迁移:

    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('email')->unique;
            $table->binary('password');
            $table->enum('role', ['user','admin'])->default('user');
            $table->timestamp('created_at');
            $table->timestamp('updated_at');
        });

        Schema::create('lists', function (Blueprint $table)
        {
            $table->increments('id');
            $table->foreignId('user_id')->nullable()->constrained()->onDelete('cascade');
            $table->string('baby');
            $table->string('vader');
            $table->string('moeder');
            $table->integer('telefoonnummer');
            $table->string('email');
            $table->string('adres');
            $table->integer('huisnummer');
            $table->string('toevoeging')->nullable();
            $table->string('stad');
            $table->integer('postcode');
            $table->string('land');
        });
    }

My User model:我的用户模型:

class User extends Authenticatable
{
    use HasApiTokens, HasFactory, Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'name',
        'email',
        'password',
    ];

    /**
     * The attributes that should be hidden for serialization.
     *
     * @var array<int, string>
     */
    protected $hidden = [
        'password',
        'remember_token',
    ];

    /**
     * The attributes that should be cast.
     *
     * @var array<string, string>
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

    public function birthLists()
    {
        return $this->hasMany(Birthlist::class, 'user_id');
    }
}

My Birthlist model:我的出生名单模型:

class Birthlist extends Model
{
    use HasFactory;

    protected  $table = 'lists';

    protected $primaryKey = 'id';

    protected $fillable = 
    [
        'user_id',
        'baby',
        'vader',
        'moeder',
        'telefoonnummer',
        'email',
        'adres',
        'huisnummer',
        'toevoeging',
        'stad',
        'postcode',
        'land'
    ];

    public $timestamps = false;

    public function user()
    {
        return $this->belongsTo(User::class, 'user_id');
    }
}

My controller我的控制器

<?php 

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Providers\RouteServiceProvider;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use App\Models\User;
use App\Models\Birthlist;



class DashController extends Controller 
{

    public function dashboard($id) 
    {
        $userId = Auth::id();
        $lists = Birthlist::where('user_id')->first();

        return view('dashboard', [
            'lists' => $lists,

        ]);
    }

}

My view我的观点

<body class="bg-red-100 w-screen h-screen pb">
        <main class="">
            <div class="text-center p-8 bg-green-100">
                <p class="">welkom</p>
                <h2 class="text-3xl font-bold">{{ Auth::user()->name }}</h2>
            </div>
            <section class="bg-red-100">
                <span class="p-4"><p class="text-center text-xl font-semibold">Mijn lijsten</p></span>
                    @foreach ($lists->birthLists as $list)
                        <div class="bg-red-200 p-8 bg-gradient-to-b from-green-300 to-fuchsia-400 drop-shadow-xl text-white md:w-5/12 xl:w-3/12">
                            <div class="text-3xl font-bold">
                                {{ $list->baby }}
                            </div>
                            <div class="flex flex-row justify-between">
                                {{ $list->vader }} & {{ $list->moeder }}
                            </div>
                        </div>
                    @endforeach
            </section>
        </main>
    @include('partials.footer')
</body>

In User model :在用户模型中:

public function birthLists()
{
   return $this->hasMany(Birthlist::class);
}

and in view :并认为:


<body class="bg-red-100 w-screen h-screen pb">
        <main class="">
            <div class="text-center p-8 bg-green-100">
                <p class="">welkom</p>
                <h2 class="text-3xl font-bold">{{ Auth::user()->name }}</h2>
            </div>
            <section class="bg-red-100">
                <span class="p-4"><p class="text-center text-xl font-semibold">Mijn lijsten</p></span>
                    @foreach (auth()->user()->birthLists as $list)
                        <div class="bg-red-200 p-8 bg-gradient-to-b from-green-300 to-fuchsia-400 drop-shadow-xl text-white md:w-5/12 xl:w-3/12">
                            <div class="text-3xl font-bold">
                                {{ $list->baby }}
                            </div>
                            <div class="flex flex-row justify-between">
                                {{ $list->vader }} & {{ $list->moeder }}
                            </div>
                        </div>
                    @endforeach
            </section>
        </main>
    @include('partials.footer')
</body>

and don't need to get data from controller because you can get birthLists in blade file.并且不需要从控制器获取数据,因为您可以在刀片文件中获取出生列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM