Laravel关系数据库语言crud

euoag5mw  于 2022-12-14  发布在  其他
关注(0)|答案(1)|浏览(124)

我有一个3表,正如我下面提到的,我想做的是,选择与选择选项(多语言)来自后。和选定语言的id 可用示例1〈arabic or 1,2 arabic,英语我如何记录它们?以及我必须在我的模型之间写入什么关系才能正确渲染。

public function up()
    {
        Schema::create('languages', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('slug');
            $table->timestamps();
        });
    }
Schema::create('galleries', function (Blueprint $table) {
            $table->id();
            $table->string('title');
            $table->string('image');
            $table->timestamps();
        });
Schema::create('gallery_language', function (Blueprint $table) {
            $table->foreignIdFor(Gallery::class)->onDelete('cascade');
            $table->foreignIdFor(Language::class)->onDelete('cascade');
            $table->timestamps();
        });
Gallery Model
public function languages()
    {
        return $this->belongsToMany(Language::class);
    }
GalleryController
/**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $post = new Gallery();
        $post->title = $request->title;
        $post->image = 'hello.js';
        $post->save();
    }

    /**
     * Display the specified resource.
     *
     * @param \App\Models\Gallery $gallery
     * @return \Illuminate\Http\Response
     */
    public function show(Gallery $gallery)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param \App\Models\Gallery $gallery
     * @return \Illuminate\Http\Response
     */
    public function edit(Gallery $gallery)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @param \App\Models\Gallery $gallery
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Gallery $gallery)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param \App\Models\Gallery $gallery
     * @return \Illuminate\Http\Response
     */
    public function destroy(Gallery $gallery)
    {
        //
    }

来自邮政的数据

"title" => "CamScanner12-05-202210"
 "image" => 'image'
 "language" => "1,2"
3pmvbmvn

3pmvbmvn1#

流程我可以告诉你,你可以写你的代码在控制器上的存储或创建功能。确保关系表是正确的。然后在字段语言可以接收一个或多个语言的数组。

相关问题