<repo_name>F1703/proyecto-b<file_sep>/app/Http/Controllers/MateriaController.php
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Materia;
use Laracasts\Flash\Flash;

class MateriaController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
        $materias=Materia::all();
        return view('materia.index')
          ->with('materias', $materias);
    }

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

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $mat= new Materia($request->all());
        $mat->save();
        Flash::success('La materia '.$mat->nombre.' se creo con exito!!');
        return redirect()->route('materia.index');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

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

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

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
<file_sep>/app/Comentario.php
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Comentario extends Model
{
    //
    protected $table = 'comentarios';
    protected $fillable =['contenido','user_id','publicacion_id'];

    public function publicacion(){
        return $this->hasMany('App\Publicacion','publicacion_id','user_id');
    }

    public function user(){
      return $this->belongsTo('App\User');
    }

    public function scopeSearch($query,$id){
      return $query->where('publicacion_id','=',$id);
    }

}
<file_sep>/app/Http/Controllers/PublicacionController.php
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Categoria;
use App\Materia;
use App\Publicacion;
use App\Archivo;
use Laracasts\Flash\Flash;
class PublicacionController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
        $posts = Publicacion::orderby('id','ASC')->paginate(3);
        // $categorias=Categoria::all();
        // $materias=Materia::all();
        return view('post.index')
          ->with('posts', $posts);
          // ->with('categorias', $categorias)
          // ->with('materias', $materias);
    }

    /**
     * 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)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

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

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

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
<file_sep>/app/Http/Controllers/PostController.php
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Categoria;
use App\Materia;
use App\Publicacion;
use App\Archivo;
use App\Comentario;
use Laracasts\Flash\Flash;

class PostController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //

          $posts = Publicacion::searchUser(\Auth::user()->id)->orderby('id','asc')->paginate(2);
          return view('post.index')
          ->with('posts', $posts);


    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
        $categorias=Categoria::lists('nombre','id');
        $materias = Materia::lists('nombre','id');
        return view('post.create')
          ->with('categorias', $categorias)
          ->with('materias', $materias);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
        // dd($request->all());
        if ($request->file('archivo_id') !=null) {
          $file=$request->file('archivo_id');
          $name= date('d.m.Y').'_'.time().'_'.$file->getClientOriginalName().'_.'.$file->getClientOriginalExtension();
          $path=public_path().'/files';
          // $file->move($path,$name);
          $archivo= new Archivo;
          $archivo->nombre= $name;
          $archivo->save();
          $publicacion=new Publicacion($request->all());
          $archivo= Archivo::find($archivo->id);
          $publicacion->archivo_id=$archivo->id;
          $publicacion->save();
        }else {
          $publicacion=new Publicacion($request->all());
          $publicacion->save();
        }
        Flash::success('Se creo con exito el usario '.$publicacion->slug.' !!');
        return redirect()->route('post.create');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($slug)
    {
        //
        ;
        $post= Publicacion::Search($slug)->get();
        $post->each(function($post){
          // $comentario= Comentario::Search($post->id)->get();
          // $comentario->publicacion;
          // dd($comentario);
          $post->comentario;
        });
        return view('post.show')
          ->with('post', $post);
          // ->with('comentario', $comentario);
    }

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

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

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
<file_sep>/app/Materia.php
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;


class Materia extends Model
{
    //


    protected $table = "materias";
    protected $fillable =['nombre'];

    public function publicacion(){
      return $this->hasOne('App\Publicacion');
    }
}
<file_sep>/app/Publicacion.php
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Cviebrock\EloquentSluggable\Sluggable;

class Publicacion extends Model
{
    //
    use Sluggable;

    protected $table = 'publicacions';
    protected $fillable =['titulo','intro','cuerpo','slug','user_id','categoria_id','materia_id','archivo_id'];



    public function sluggable()
    {
        return [
            'slug' => [
                'source' => 'titulo'
            ]
        ];
    }

    public function archivo(){
      return $this->belongsTo('App\Archivo');
    }

    public function materia(){
      return $this->belongsTo('App\Materia');
    }

    public function categoria(){
      return $this->belongsTo('App\Categoria');
    }

    public function user(){
      return $this->belongsTo('App\User');
    }


    public function comentario(){
      return $this->hasMany('App\Comentario');
    }

    public function scopeSearch($query, $slug){
      return $query->where('slug','=',$slug);
    }
    public function scopeSearchUser($query,$user_id){
      return $query->where('user_id','=', $user_id);
    }
}
