Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

meglo's avatar
Level 1

i have case about views and relation eloquent please..

hello excuse me, sorry to disturb the time sir/master/instructor, im newbe and still learning now about laravel.

i have case like this i want doing CRUD my "anggota_hadiah.php" table but before CRUD i want which can display the contents of the member's name in option tag in my create.blade.php. i want doing CRUD on the model "anggota_hadiah.php"

i have 3 tables in my Database. first is "anggota" this table collect name's member (nama). Image

and the second is "anggota_hadiah" this table collect anggota_id and hadiah_id Image

and the third is "hadiah" this table collect hadiah (in english its mean name's prize) Image

this is my views file "create.blade.php"

<form action="">
                <label> name </label>
                <select name="j_kel" type="text" class="form-control">
                <option selected> choose name mamber here</option>
                    @foreach($create as $a)
                        <option>{{ $a->anggota_id }}</option>
                    @endforeach
            </form>

from the code above. i want in the code @foreach($create as $a)<option>{{ $a->anggota_id }}</option>@endforeach can show the nama(name's mamber) from the table anggota. my controller for this function is like this

public function create()
    {
        $create_form = Anggota_hadiah::get();
        return view('create', ['create' => $create_form]);
    }

so if i doing click in view option to choose name's member its only show the name from anggota table and the name in option is not repeated like this anggota_id.

Image

and my model are like this:

  1. Anggota.php
class Anggota extends Model
{
    protected $table = "anggota";

    public function hadiah()
    {
    	return $this->belongsToMany('App\Hadiah');
    }
}
  1. Hadiah.php
class Hadiah extends Model
{
    protected $table = "hadiah";

    public function anggota()
    {
    	return $this->belongsToMany('App\Anggota');
    }
}

3.Anggota_Hadiah.php

class Anggota_hadiah extends Model
{
    protected $table = "anggota_hadiah";

    public function anggota()
    {
        return $this->belongsToMany('App\Anggota');
    }

    public function hadiah()
    {
        return $this->belongsToMany('App\Hadiah');
    }
}

im so sorry my english not well. but i try to learn from this case. Please help me. i really need help..

0 likes
5 replies
Sinnbeck's avatar

Why not just query what you need then

public function create()
    {
        $angotta = Anggota::get();
        return view('create', ['angotta' => $angotta ]);
    }

<form action="">
                <label> name </label>
                <select name="j_kel" type="text" class="form-control">
                <option selected> choose name mamber here</option>
                    @foreach($angotta as $a)
                        <option value="{{$a->id}}">{{ $a->nama }}</option>
                    @endforeach
               </select>
            </form>
meglo's avatar
Level 1

Sorry again and thank you for responding Mr. Sinnback. In the future I want to do CRUD in the table "anggota_hadiah (model:Anggota_hadiah.php)"

like this example if i doing insert: I enter the name of the member, and then I enter the prize. Isn't it only possible to do CRUD in the table anggota_hadiah (model: Anggota_hadiah.php)? if I do and call the "Anggota.php" model in the controller as suggested $angotta = Anggota::get(); , can I still do CURD on the Member_hadiah.php model?

sorry for my nescience and my stupidity

Sinnbeck's avatar

Yes you have relationships set up so you can find that on the other tables if you need to. But I am a bit confused as to what you are trying to do (what should be saved in the store method?)

meglo's avatar
Level 1

if i doing add new member in "anggota" table (id:7 name:meglo). imgae

i want to save new data in table ("anggota_hadiah") with $request form with that new member (id=7, name=meglo + hadiah_id= 1, which mean it will be filled like this in the table "anggota_hadiah" table)

image

so if i doing form $request view in create.blade.php

<form role="form" action="/store" method="POST" enctype="multipart/form-data">
                <div class="form-group">
                        <input name="_token" type="hidden" value="{{ csrf_token() }}" />
                        <input name="_method" type="hidden" value="POST" />
                </div>
                <div class="form-group row col-md-3">
                <label> name </label>
                <select name="nama" type="text" class="form-control">
                <option selected> choose name mamber here</option>
                    @foreach($angotta as $a)
                    <option value="{{$a->id}}">{{ $a->nama }}</option>
                    {{-- This part Form should store in anggota_hadiah table witch ex: anggota_id : 7 (meglo) --}}
                    @endforeach
                </select>
                </div>

                <div class="form-group row col-md-3">
                <label> Prize </label>
                <select name="nama_hadiah" type="text" class="form-control">
                <option selected> choose prize here</option>
                    @foreach($angotta as $a)
                    <option value="{{$a->id}}">{{ $a->hadiah->nama_hadiah }}</option>
                    {{-- This part Form should store in anggota_hadiah table, ex: hadiah_id: 1 (kulkas) --}}
                    @endforeach 
                </select>
                </div>
            <button type="submit" class="btn btn-primary waves-effect">Add Data</button>
            </form>

Same as picture above @foreach($angotta as $a)<option value="{{$a->id}}">{{ $a->nama }}</option>{{-- This part Form should store in anggota_hadiah table witch ex: anggota_id : 7 (meglo) --}}@endforeach and @foreach($angotta as $a)<option value="{{$a->id}}">{{ $a->hadiah->nama_hadiah }}</option>{{-- This part Form should store in anggota_hadiah table, ex: hadiah_id: 1 (kulkas) --}} @endforeach i want it store in "anggota_hadiah" table

and my controller

public function create()
    {
        $angotta = Anggota::get();
        return view('create', ['angotta' => $angotta ]);
    }

    public function store(Request $request)
    {
    Anggota::create($request->all());
    return redirect('anggota');
    }

and my route

Route::get('/anggota', 'DikiController@index');
Route::get('/create', 'DikiController@create');
Route::post('/store', 'DikiController@store');

and i add $fillable in model anggota_hadiah.php

protected $table = "anggota_hadiah";

    protected $fillable = ['anggota_id', 'hadiah_id'];

    public function anggota()
    {
        return $this->belongsToMany('App\Anggota');
    }

    public function hadiah()
    {
        return $this->belongsToMany('App\Hadiah');
    }

how do you think mr.sinnback? there must be a lot of things wrong

meglo's avatar
Level 1

mr sinnbeck im so sorry. Can you help me sir?

Please or to participate in this conversation.