meglo
1 week ago
102
5
Laravel

i have case about views and relation eloquent please..

Posted 1 week ago by meglo

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..

Please sign in or create an account to participate in this conversation.