How to show TeamController#index and ProductController#index both show list of team and product inside one view main.blade.php
Looks like you want to show two datasets on one page. Basically, it means you have to execute two controller methods but it's not necessary to follow each and everything that official documentation says.
For example, if Products belong to a team, you can execute only TeamController#index and show products as given below.
#foreach($teams as $team)
#foreach($team->products as $product)
{{ $product->name }}
#endforeach
#endforeach
If no teams and products are two different entities and does not have any relation, you can just pass teams and products like this:
TeamController.php
public function index()
{
$teams = Team::all();
$products = Product::all(); // Don't forget to include 'use App\Product'
return view('index',compact(['teams','products']);
}
and then you can show teams and products like this:
index.blade.php
#foreach($teams as $team)
{{ $team->name }}
#endforeach
#foreach($products as $product)
{{ $product->name }}
#endforeach
Getting information from two different models does not mean you have to execute two different controller functions.
Still, if you want to get data from two different controllers, you can setup index.blade.php and create two ajax requests that will get data from two different URLs (two different controller methods).
Let me know if you have any more questions.
You can't show results from two controllers like that. Create a view that includes both the view that TeamController#index and ProductController#index return. be aware that both might be extending a layout which will probably try to load your page twice, so keep in mind to split the views into smaller components and include only those.
More info here
https://laravel.com/docs/5.6/views#creating-views
Related
I am new to laravel. I want to send data as id of existing data. Id comes from products.blade I send via href tag as shown below to gallery page. I have tried to find a way through other sites but it still doesn't work
<a class="btn btn-success" href="/dashboard/galleries/{{ $product->id }}"><i class="ri-image-add-line text-white"></i></a>
then i create a route like this
Route::resource('/dashboard/galleries', DashboardGalleryController::class)->middleware('admin')->shallow();
in the controller gallery, I made like this
public function index($id)
{
$gallery = Gallery::where('products_id', $id)->get();
return view('dashboard.galleries.index', compact('gallery'));
}
then inside the gallery page, I want to display a table to add images based on that id.
dashboard/galleries/index.blade.php
<h1>{{ $gallery->id }}</h1>
should i add data inside foreach or outside?
A restful resource controller sets up some default routes for you and even names them.
and ur case the logic should be inside show() function not index()
check this issue it will help u same issue solved here
I've created file GlobalVariable.php inside app\Composers
public function compose($view)
{
$categories = \App\Models\Category::all();
$view->with('globCategory', $categories);
}
then register to AppServiceProvider the code view()->composer('*', GlobalVariable::class);
I use global $globCategory for creating dynamic navbar
<ul class="nav nav-tabs border-0 flex-column flex-lg-row">
#foreach ($globCategory as $item)
<li class="nav-item">
{{$item->name}}
</li>
#endforeach
</ul>
the only problem here when I see laravel debuggar it show repetition of categories query.
here is the result
How to avoid this looping query? is there correct way?
The way you're registering your view composer (using '*' instead of a particular view name), it's going to call the compose method for every single rendered view + subview.
What you can do is instead of this:
view()->composer('*', GlobalVariable::class);
Have this:
\View::share('globCategory', \App\Models\Category::all());
This will globally share your categories (within views), and run the query only once.
View composers, as described from the laravel documentation, bind data to a view every time it is rendered. They clean our code by getting fetching data once and passing it to the view.
While it is possible to get the data in every controller method and pass it to the single view, this approach may be undesirable.
Replace the view name with an asterisk wildcard.
We have two tables room and room images. I am using relation in both tables and want to get the first image from room images table (For thumbnail). Below foreach loop is working fine on single detail page. Now i want to get the thumbnail image for main page.
#foreach ($room->tbl_roomimages as $single_img)
<img src="/storage/cover_images/{{$single_img->room_image}}" alt="">
#endforeach
I am using hasMany relation for Room images in room model.
You can create a function inside your model to get the first image.
Example:
public function getFirstImageAttribute()
{
return $this->hasOne('RoomImageModel');
}
And use that function to get the first image for your room.
<img src="/storage/cover_images/{{ $room->firstImage }}" alt="">
Thanks!
Although i get the first image like this.
#foreach($room->tbl_roomimages as $single_img)
#if($room->tbl_roomimages->first()==$single_img)
{{$single_img->room_image}}
#endif
#endforeach
I have a question related to this LINK:
Laravel / Eloquent eager loading
Can you help me with the output in blade now with this? I implement this eloquent relation and add this in controller correctly, But how to output this in view now - in blade file!?
Can you write a little code for this example Comments - Tags. If we want to show that in a blade. To see this like child's into parents in some way.?
For example to output Questions and related tags what belongs in particular question. Like for example: Question1 [ Tag1 - Tag4 - Tag12 ] - - - Question2 [ Tag1 Tag 8 Tag5 ] ... and so on, like in some tree in view like in olx we see categories inside that it shows subcategories. olx.com.om/en.
Or another example when we have: COUNTRIES AND CATEGORIES (MANY TO MANY RELATION), and want to list CATEGORIES in view above, and countries that belong to particular category below.
Thanks in advance,
I am new to laravel want to start my own blog I am learning laravel for 2 + months.
In your controller function, will look like this.
$questions = Question::with('tags')->get();
$title = "List of questions";
return view('test', compact('questions', 'title'));
In your blade will look like this and see how the tags relationships being called.
<!-- ouput: List of questions-->
<h2> {{ $title }} </h2>
<!-- ouput:list of questions -->
#foreach($questions as $question)
Question Name : {{ $question->name }}
<b> Tags: </b>
#foreach($question->tags as $tag)
{{ $tag->name }}
#endforeach
#endforeach
That's how you output the data to your blade file. Hope that gives you an idea.
I want to save comments, posts and profiles that a user views in a viewable table. Posts and comments are displayed in a stream similar to social networks. How can I store the data in a viewable table in the background?
Table:
viewables
user_id
viewable_id
viewable_type
For example, if a user views or sees a post in the stream, the database should store the user_id and post_id. The same should be possible with comments and profiles.
EDIT:
The posts are issued in a forelse loop and the comments in a foreach.
#forelse ($posts as $post)
#if ($post->type === 1)
{{$post->body}}
#foreach ($post->comments as $comment)
{!!$comment->body!!}
#endforeach
#elseif ($post->type === 2)
{{$post->image}}
#foreach ($post->comments as $comment)
{!!$comment->body!!}
#endforeach
#elseif ($post->type === 3)
show post with type 3
#elseif ($post->type === 4)
show post with type 4
#else
show remaining
#endforelse
Based on your edit, there are a few different ways to handle this. Technically, since all posts are included on the page, it could be assumed that the user has read them all and simply save the user id for all of the posts loaded before rending the view.
If you wanted something more refined, however, you could use an ajax method to post the user id, post id, etc. when scrolling over it, though I don't find that very practical, as that could cause problems if someone scrolls through too fast.
The best option (without requiring user interaction) would be to load a couple posts at a time. Either automatically when scrolling (see this answer), or with the help of Laravel's pagination (https://laravel.com/docs/5.7/pagination) and set the user id when loading the data.
Example:
$posts = Posts::paginate(5);
foreach($posts as $p) {
DB::table('viewables')->insert([
'user_id' => $user->id,
'viewble_id' => $p->id,
`viewable_type` => $p->type,
]);
}