Jumat, 16 Februari 2018

Relation table via controller

1. has a table to be related, example : table berita and kategori berita
2.  we have 2 tables here
a. Fileds on table berita
            $table->increments('id');
            $table->string('judul');
            $table->longText('isi');
            $table->string('img');
 b. Field on table kategori acara
            $table->increments('id');
            $table->string('kategori_berita');

addition of filed id_kategori on table berita
            $table->increments('id');
            $table->string('judul');
            $table->longText('isi');
            $table->string('img');
            $table->integer('id_kategori');
3. php artisan migrate on terminal
4. then open controller berita
use App\KategoriBerita;

a. Add on Funtion index
        $berita = Berita::leftjoin('kategori_beritas', 'kategori_beritas.id', '=', 'beritas.id_kategori')
        ->orderBy('beritas.created_at', 'desc')
        ->select(
            'beritas.id',
            'beritas.judul',
            'beritas.id_kategori',
            'beritas.isi',
            'beritas.img',
            'kategori_beritas.kategori_berita')
        ->paginate(10);
        return view('admin.berita.list', compact('kategori_berita', 'berita','user'));
b. add on funtion create
        $user = Auth::user();
        $kategori_berita = KategoriBerita::pluck('kategori_berita', 'id');
        return View::make('admin.berita.create',compact('kategori_berita'),array('user' => $user));
c. add on function store
        $berita = new Berita;
        $berita->judul        = Input::get('judul');
        $berita->id_kategori  = Input::get('id');
        $berita->isi          = Input::get('isi');

        //add foto
        $img = $request->file('img');
        $imageName = time().'.'.$img->getClientOriginalExtension();

        //thumbs
        $destinationPath = public_path('images/berita/thumbs');
        $img = Image::make($img->getRealPath());
        $img->fit(1200, 1200);
        $img->save($destinationPath.'/'.$imageName);

        //original
        $destinationPath = public_path('images/berita');
        $img = Image::make($img)->encode('jpg', 100);
        $img->save($destinationPath.'/'.$imageName);
        //save data image to db
        $berita->img = $imageName;
        $berita->save();

        // redirect
        return Redirect::action('admin\BeritaController@index')->with('flash-success','The user
        has been added.');
d. add on function edit
        $user = Auth::user();
        $berita = Berita::where('id', $id)->firstOrFail();
        $kategori_berita = KategoriBerita::pluck('kategori_berita', 'id');
        return view('admin.berita.edit', compact('berita', 'kategori_berita'),array('user' => $user));
5. open blade berita and add form
a. create
         <div class='form-group clearfix'>
            {{ Form::label("id", "Kategori", ['class' => 'col-md-2 control-label']) }}
              <div class='col-md-4'>
                {{ Form::select("id", $kategori_berita, null,['class' => 'form-control required']) }}
                <span class="error">{{$errors->first('id')}}</span>
              </div>
          </div>
b. edit
          <div class='form-group clearfix'>
            {{ Form::label("id", "Kategori", ['class' => 'col-md-2 control-label']) }}
              <div class='col-md-4'>
                {{ Form::select("id", $kategori_berita, null,['class' => 'form-control required']) }}
                <span class="error">{{$errors->first('id')}}</span>
              </div>
          </div>
c.list
         <th><b>Kategori</b></th> on table
         <td> {{ $beritas->kategori_berita }} </td> on @foreach
d.show
           <tr>
            <td>Kategori</td>
            <td>
              {{{$berita->kategori_berita}}}
            </td>
          </tr>

info : red writing to note



0 komentar:

Posting Komentar

luvne.com resepkuekeringku.com desainrumahnya.com yayasanbabysitterku.com