How to find the last 5 images inserted in the seat and show in a carousel?

Asked

Viewed 307 times

0

I want to take the last five images in the bank and show them on a carousel, with the respective title, and those marks that indicate the image being viewed. I’m working with Laravel 5.1 and mysql. For further clarification, here are some tips:

To fetch 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);
}

}

Noticiacontroller.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 didn’t implement anything for that same search, because I don’t know how to do it. I’ve searched so much, but I haven’t found a subject 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 put the carousel to take the data dynamically in the database, but rather to take the images from a folder, as shown in the following image.

inserir a descrição da imagem aqui

When in fact, my purpose is to load the data dynamically and present them on the carousel (slides).

The page looks like this

inserir a descrição da imagem aqui

  • You made the model? you have the table name if you didn’t make the model? and you made the code in which controller you can make available?

  • I just edited @Virgilionovic

  • return Noticia::all()->take(5); would be on that line ?

  • I didn’t get it. Would that be?

  • then you want to return the last record of the model Noticia? and it is on this line ???

  • Yes, that’s right. What would it be like?

Show 1 more comment

1 answer

1

To return the last record of a table with we must have an ordination, I believe that by id of the auto increment record already solve, another caveat is that used the wrong technique, because, brought the mass of data from your table, then took only what you need with the class Collection, That’s a development mistake, so:

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

In his View:

<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>
  • OK @Virgilio! Now, how to pass this in Homecontroller and then in view, to slide in carousel?

  • @Danieldossantos IF HE DOESN’T HAVE THE CANVAS ???

  • I didn’t get @Virgilio. What a canvas?

  • I’m the one who doesn’t understand, if you ask one thing then ask another thing and then in your controller already have the code and you asked me how do I pass it to view, if your code is already doing this !!! @Danieldossantos I don’t understand anything else!

  • If you look, the view code is not set to take the data dynamically, but yes, I entered the image path, as well as the title. And my question is, how do I send to the carousel these images with their respective bank titles?

  • @Danieldossantos made the edition of view

  • I did it the way you said, it was a mistake. But I only changed the way to insert "for" and "foreach", using " <?php foreach($noticia as $item): ? >" and it worked, but the images are intact, do not move, Controls and indicators do not appear, and the 3 images appeared simultaneously, as in the image that I am going to load now.

  • @Danieldossantos your problem is bigger and too wide, and the way I did it was supposed to work, maybe yours view don’t be with blade.php so ... good is very broad

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.