How to search for the last 5 pictures inserted in the bank and show on a carousel? [closed]

0

I want to get the last 5 pictures in the bank and show them in a carousel, with their title, and those marks that indicate the image being viewed. I'm working with Laravel 5.1 and mySql. For more clarification, here are some tips:

To search the 5 records (getNoticia)

Controller.php

abstract class Controller extends BaseController
{
use DispatchesJobs, ValidatesRequests;

protected function getLemas() {
    return Lema::where('status_lm', 'Activo')->get();
}

protected function getIgreja() {
    return Igreja::All();
}

protected function getNoticia() {
    return Noticia::all()->take(5);
}

}

NewsController.php

class NoticiaController extends Controller
{

 // 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()
{
    return view('noticias.create-noticia')->with('lema', $this->getLemas())->with('igreja',$this->getIgreja());
}

/**
 * Store a newly created resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return \Illuminate\Http\Response
 */
public function store(Request $request)
{
    if($request->hasFile('imagem'))
    {
        $imagem = $request->file('imagem');
        $extensao = $imagem->getClientOriginalExtension();

        if($extensao != 'jpg' && $extensao != 'jpeg' && $extensao != 'png')
        {
            return back()->with('erro','Erro: Este arquivo não é uma imagem JPG ou PNG');
        }
    }

    $noticia = new Noticia();
    $noticia->titulo = $request->titulo;
    $noticia->conteudo = $request->conteudo;

    $noticia->imagem = "";
    $noticia->save();

    if(Input::file('imagem'))
    {
        File::move($imagem,public_path().'/imagem-noticia/noticia-id_'.$noticia->id.'.'.$extensao);
        $noticia->imagem = '/imagem-noticia/noticia-id_'.$noticia->id.'.'.$extensao;
        $noticia->save();
    }

    return redirect('/');
}

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

}

Just to point out that in NoticiaController.php, I did not implement anything for this same search, because I did not know how to do it. I've tried so much, but I have not found any material that can help me.

HomeController.php

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

    return view('home')
            ->with('lema', $this->getLemas())
            ->with('igreja',$this->getIgreja())
            ->with('noticia',$this->getNoticia());

}

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

}

View Home.blade.php

<div id="carousel-example-generic" class="carousel slide carousel-home" data-ride="carousel">
        <!-- Indicators -->
        <ol class="carousel-indicators">
            <li data-target="#carousel-example-generic" data-slide-to="0" class="active"></li>
            <li data-target="#carousel-example-generic" data-slide-to="1"></li>
            <li data-target="#carousel-example-generic" data-slide-to="2"></li>
        </ol>

        <!-- Wrapper for slides -->
        <div class="carousel-inner" role="listbox">
            <div class="item active">
                <img src="/imagem-noticia/noticia-id_11.jpg" alt="...">
                <div class="carousel-caption" style="background-color: rgba(4,162,183,0.9)">
                    <p><h3>Preparativos para o 2º Aniversário da Igreja</h3></p>
                </div>
            </div>
            <div class="item">
                <img src="/imagem-noticia/noticia-id_12.jpg" alt="...">
                <div class="carousel-caption" style="background-color: rgba(4,162,183,0.9)">
                    <p><h3>Coral das moças estará na Igreja Baptista da Paz</h3></p>
                </div>
            </div>
            <div class="item">
                <img src="/imagem-noticia/noticia-id_13.jpg" alt="...">
                <div class="carousel-caption" style="background-color: rgba(4,162,183,0.9)">
                    <p><h3>EBF - Inscrições Abertas</h3></p>
                </div>
            </div>
        </div>

        <!-- Controls -->
        <a class="left carousel-control" href="#carousel-example-generic" role="button" data-slide="prev">
            <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
            <span class="sr-only">Previous</span>
        </a>
        <a class="right carousel-control" href="#carousel-example-generic" role="button" data-slide="next">
            <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
            <span class="sr-only">Next</span>
        </a>
    </div>

In this view, I did not get the merry-go-round to take the data dynamically in the bank, but rather to take the images from a folder, as shown in the following image.

Wheninfact,mypurposeistodynamicallyloaddataanddisplayitonthecarousel(slides).

Thepagelookslikethis

    
asked by anonymous 30.03.2018 / 14:12

1 answer

1

To return the last record of a table with we have that having an order, I believe that with% auto_log% already solved, another caveat is that you used the wrong technique, because, you brought the mass of data from your table, then you just got what you need with class id , this is an error in development, then:

return Noticia::orderBy('id', 'DESC')->take(5)->get();

In its Collection :

<div id="carousel-example-generic" class="carousel slide carousel-home" data-ride="carousel">
    <!-- Indicators -->
    <ol class="carousel-indicators">
        @for($i=0;$i<$noticia->count();$i++)
        <li data-target="#carousel-example-generic" data-slide-to="{{$i}}"></li>
        @endfor
    </ol>

    <!-- Wrapper for slides -->
    <div class="carousel-inner" role="listbox">
        @foreach(@noticia as $item)
        <div class="item active">
            <img src="{{$item->image}}" alt="...">
            <div class="carousel-caption" style="background-color: rgba(4,162,183,0.9)">
                <p><h3>Preparativos para o 2º Aniversário da Igreja</h3></p>
            </div>
        </div>
        @endforeach
    </div>

    <!-- Controls -->
    <a class="left carousel-control" href="#carousel-example-generic" role="button" data-slide="prev">
        <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
        <span class="sr-only">Previous</span>
    </a>
    <a class="right carousel-control" href="#carousel-example-generic" role="button" data-slide="next">
        <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
        <span class="sr-only">Next</span>
    </a>
</div>
    
04.04.2018 / 02:16