How to output Eloquent Eager Loading in View (blade files) in Laravel? - laravel

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.

Related

Laravel Blade Helpers not pulling in columns with spaces

I have a table with many columns and some of those columns have spaces in their names (i.e. 'Provider First Name'). I know the syntax to use these in Blade helpers and am using this in other parts of my app: {{$provider->{'Provider First Name'} }}. This works fine in other parts.
I have the following in my ProviderController:
public function show($id)
{
$provider = NPIData::where('NPI', $id)->first();
$providers = NPIData::all();
return view ('profiles.provider', compact('provider', 'providers'));
}
I have brought in the NPIData Model to the controller.
I have the following in my provide.blade.php file:
#extends('layouts.profiles')
#section('content')
<div>
{{ $provider->NPI }}
{{$provider->{'Provider First Name'} }}
</div>
#endsection
Oddly, the NPI will pull in, but the 'Provider First Name' does not. I have tried many other columns with spaces and none of them work. I even copied and pasted from other parts of my app where the syntax to pull these in works and it does not work here.
Instead of:
{{$provider->{'Provider First Name'} }}
Try this:
#php
$providerFirstName = $provider->{'Provider First Name'};
#endphp
{{ $providerFirstName }}
UPDATE
If not you can always go with array access:
{{ $provider['Provider First Name'] }}

Laravel 5.7 Pagination Customization

$users = Student::paginate(2);
I was trying to paginate some data with paginate function() but am want to show only data no links
You can customize the way pagination is displayed, first step:
php artisan vendor:publish --tag=laravel-pagination
This command publishes the pagination views under the resources/views/vendor directory.
Here you can modify files as you want, the bootstrap-4.blade.php file is the default view for pagination.
You can find more info in the laravel official documentation
Using the $students->links() is optional if yo have a custom blade file showing pagination. However, if you do not need detailed pagination, you can use simplePaginate
$students = Student::simplePaginate(2);
And then in blade file just loop through it :
#foreach($students as $student)
Show student details
#endforeach
If you want to show just next or prev page links then you can use $students->links(), otherwise do not use links at all.
it's simple you must create a function to display your students in your template, add this code in your controller :
public function index()
{
$students = Student::paginate(10);
return view('student.index', ['students ' => $students ]);
}
And on the bottom of your template for example views\student\index.blade.php add :
<div class="row">
<div class="col-md-12">
{{ $students->links() }}
</div>
</div>
if you want display additional pagination information use the following methods: :
Paginator Instance Methods

laravel 5.5 multiple controller inside one view

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

Laravel blade creating url

I have a simple problem, basically I am getting name of the website from database and create a link according to it's name. it looks like:
#foreach ($websites as $website)
<a class="websites" href=" {{ asset ($website->name )}}"> {{ asset ($website->name )}}
</a>
#endforeach
Which gives for example: http://localhost/name
Howver links needs to be like this:
http://localhost/website/name how can I add /website into my URL using blade template in laravel?
Try this:
{{ url('website/' . $website->name) }}
This have some improvement on #Laran answer regarding best practices.
You would better use url parameters instead of concatenating the $name parameter
{{ url('website', [$name]) }}
And using named routes will be better to decouple the routing from the views.
// routes/web.php
Route::get('website')->name('website');
and write inside your {{ route('website', [$name]) }}

Sort an object's assocation in Ember.js

I'm looking to sort an object's association - for instance, I am on a Group (object) page and the Group has many Posts that I show on that page. How would I sort those Posts to arrange them by created_at, or some other property?
You can use an ArrayController for your collection of Posts and then use the built in sortProperties and sortAscending properties.
App.GroupRoute = Ember.Route.extend({
setupController : function(controller,model){
this._super(controller,model);
this.controllerFor('posts').set('content',model.get('posts'));
}
});
App.GroupController = Ember.ObjectController.extend({
needs : ['posts']
});
App.PostsController = Ember.ArrayController.extend({
sortProperties : ['createdAt'],
sortAscending : false
});
By calling render in your group template, you can get access to a controller that has been needed by your main controller. So then your group template looks something like this:
<h2>Group : {{name}}</h2>
{{ render 'posts' }}
And then the posts template might be:
<ul>
{{#each post in arrangedContent}}
<li>{{ post.title }}</li>
{{/each}}
</ul>
Note that it's using arrangedContent as the name of the collection. You can change the sort order dynamically by setting sortProperties and sortAscending on PostsController.
Here's a JSFiddle of the general idea (but with different model names).
http://jsfiddle.net/YeKCf/
Click on the column headings in the table to see dynamic sorting in action.

Resources