Home > Net >  Toggle display of data returned from the database in the view
Toggle display of data returned from the database in the view

Time:11-26

I have two tables in my database:

"standard" table

 ---- --------- -------------- ---------------------- ------------------------------------ 
| id | user_id | category     | title                | content                            |
 ---- --------- -------------- ---------------------- ------------------------------------ 
| 1  |       1 | Minus        | Architecto aperiam   | Architecto aperiam odio mollitia.  | 
| 2  |       1 | Onmnis       | Est aspernatur       | Est aspernatur.                    |
| 3  |       1 | Dolor        | Delectus ratione     | Delectus ratione.                  |
| 4  |       1 | Aests        | Fugit occaecati aut  | Fugit occaecati aut.               |


"quote" table

 ---- --------- ------------------------------------------ ------------------ 
| id | user_id | content                                  | author           | 
 ---- --------- ------------------------------------------------------------- 
| 1  |       1 | Architecto aperiam odio mollitia         | Author           |
| 2  |       1 | Evitar pecados na frente dos outros      | Author           | 

My HomeController looks like this:

class HomeController extends Controller
{
    public function index()
    {
        return view('home',
        [
        'standard' => Standard::limit(5)->orderby('id', 'desc')->get(),
        'quote' => Quote::limit(3)->orderby('id', 'desc')->get()
        ]);

    }
}

My "home" view looks like this:

@foreach($standard as $standardPost)
        <div>
            <p>{{ ($standardPost->content) }}</p>
        </div>
@endforeach

@foreach($quote as $quotePost)
        <div>
            <p>{{ ($quotePost->content) }}</p>
        </div>
@endforeach

In my homepage it is showing like this:

Last row of the "standard" table
Penultimate row of the "standard" table
Antepenultimate row of the "standard" table
 ...
Last row of "quote" table
Penultimate row of "quote" table
Antepenultimate row of "quote" table

But I would like the data returned from the database to be organized alternately on my homepage, as shown below:

Last row of the "standard" table
Last row of "quote" table

Penultimate row of the "standard" table
Penultimate row of "quote" table

Antepenultimate row of the "standard" table
Antepenultimate row of "quote" table

CodePudding user response:

If $standard is you longer collection, you can do something like that:

@for ($i = 0; $i < $standard->count(); $i  )
    <div>
        <p>{{ $standard->slice($i, 1)->first()?->content }}</p>
        <p>{{ $quote->slice($i, 1)->first()?->content }}</p>
    </div>
@endfor
  • Related