Laravel/Inertia:我无法将两个模型之间的关系联系起来

hvvq6cgz  于 2023-05-08  发布在  其他
关注(0)|答案(1)|浏览(165)

我目前正在使用Laravel、Inertiajs和Reactjs创建一个项目,我需要一些帮助来通过foreing键将包模型链接到状态
如果你能识别出问题,这就是代码:

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('states', function (Blueprint $table) {
            $table->id();
            $table->timestamps();
            $table->string('name')->nullable();
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('states');
    }
};

包迁移:

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('packages', function (Blueprint $table) {
            $table->id();
            $table->timestamps();
            $table->string('article');
            $table->foreignId('state_id');
            $table->double('prix',8,2)->nullable();
            $table->string('phone')->nullable();
            $table->text('adresse')->nullable();
            $table->string('client')->nullable();
            $table->string('ville')->nullable();
            $table->text('remarque')->nullable();


        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('packages');
    }
};

型号:

<?php

namespace App\Models;

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

class State extends Model
{
    use HasFactory;
    public function Package()
    {
        return $this->hasMany(Package::class);
    }
}
<?php

namespace App\Models;

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

class Package extends Model
{
    use HasFactory;
    public function State()
    {
        return $this->belongsTo(State::class);
    }
}

软件包控制器:

<?php

namespace App\Http\Controllers;

use App\Models\Package;
use App\Models\State;
use Illuminate\Http\Request;
use Inertia\Inertia;
use Symfony\Contracts\Service\Attribute\Required;

class PackageController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        return Inertia::render('Colis/Colis',[
            'colis' => Package::all()
        ]);
  
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
    
        return Inertia::render('Colis/Colis.create');

    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request )
    {
        $request->validate([
            'article' =>' Required|min:4'
        ]);
        $colis = new Package();
        $colis->article = $request->article;
        $colis->client = $request->client;
        $colis->prix = $request->prix;
        $colis->remarque = $request->remarque;
        $colis->phone = $request->phone;
        $colis->adresse = $request->adresse;
        $colis->ville = $request->ville;
        $colis->state_id= 2;


        $colis->save();
    }

    /**
     * Display the specified resource.
     */
    public function show(Package $package)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(Package $package)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, Package $package)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(Package $package)
    {
        //
    }
}

{props.colis.map((item,key)=> { return

<td><p href="#" className="text-body"> {item.article} </p></td>
                                <td><span className="badge badge-soft-warning mb-0">{console.log(item)}</span></td>
    
            
                            </tr>
                              })}

当我控制台日志colis objcet时,这是结果:{“id”:1,“created_at”:“2023-05-01T22:36:28.000000Z”,“updated_at”:“2023-05-01T22:36:28.000000Z”,“article”:“TODLICH COULEUR“,“state_id”:2、“prix”:200、“phone”:“062360984”,“地址”:“nN 362 CASA”,“客户端”:“ZACK”,“ville”:null,“remarque”:“ok”}
我想显示软件包的状态,但我只得到state_id

fjnneemd

fjnneemd1#

如果你需要使用with()方法快速加载状态关系,请尝试以下操作:

public function index()
 {
    return Inertia::render('Colis/Colis',[
       'colis' => Package::with('State')->get()
    ]);
  
 }

相关问题