In controller:
function show($feast_id)
{
$feastMenu = Feast::find($feast_id);
$feastMenu->load('images', 'menu', 'users');
$feastMenu->chef->load('chefMeta');
$feastMenu = $feastMenu->toArray();
return view('feasts.showFeastMenu', compact('feastMenu'));
}
in views
#foreach($feastMenu['menu'] as $k => $menu)
Menu“{{$feastMenu['name']}}”({{count($feastMenu['menu'])}} course meal)
{{$menu['name']}}
#foreach($menu['menu_items'] as $item)
{{$item['name']}}
{{$item['description']}}
#endforeach
#endforeach
How can I set pagination into this please suggest in laravel I am using laravel 5. I have used relation into the controller and pass those toArray
There are a few things i have questions about... but first you should really take a look at the documentation https://laravel.com/docs/5.4/pagination it does a really good job explaining how to do what you want...
Anyways to answer you question i would do it this way... (note how i changed the views due to the returning collection instead of array). Also this will be under the assumption that you are using bootstrap.. if you are not you will need to look over the documentation to see how to add the pagination links
Controller:
function show($feast_id)
{
$feastMenu = Feast::find($feast_id)->simplePaginate(5);
$feastMenu->load('images', 'menu', 'users');
$feastMenu->chef->load('chefMeta');
return view('feasts.showFeastMenu', compact('feastMenu'));
}
View:
#foreach($feastMenu->menu as $k => $menu)
Menu“{{$feastMenu->name}}”({{$feastMenu->menu->count())}} course meal)
{{$menu->name}}
#foreach($menu->menu_items as $item)
{{$item->name}}
{{$item->description}}
#endforeach
#endforeach
{{ $feastMenu->links() }}
i'm new in laravel, i have some problem to make database as variable to be shown on blade template,
example i want to get any data from database :
{{$get = DB::table('perangkat')->get()}}
then:
#foreach ($get as $got)
{{$got->jabatan}}
#endforeach
if the result of the $got->jabatan is kepala, But i want the result is Change To "Kepala Desa", How can i do ??
Please Help Mee ...
Write database related stuff in model or controller in a method and pass that value to view. It will be very neat and clear
public function getData(){
$get = DB::table('perangkat')->get();
return view('myblade', ['data' => $get]);
}
In your view
#foreach ($data as $got)
{{$got->jabatan}} Desa
#endforeach
Also can you update where you get Desa .so i can update answer according to that.
Updated
you keep different roles in array and do some think like this
<?php
$role=['a'=>'operator','b'=>'admin'] ; ?>
#foreach ($data as $got)
<?php $result = isset($role[$got->jabatan]) ? $role[$got->jabatan] : null; ?>
{{$result}}
#endforeach
company.php
function showdata{
$sql= "this syntax sql";
}
//show data
$company = new company();
#foreach ($company->showdata() as $got)
{{$got->jabatan}}
#endforeach
I have a problem with Laravel. So what am I trying to do? I'm trying to get the location from all my examples. This actually works, so as you can see I get the id of all the examples and find the locations of it from another table in my project. The var_dump in the foreach gives what I need. But I need this data outside the foreach, I need to send it to my view afterwards. But the second var_dump, the one outside the foreach only gives the location of the first id.
So my question: is there a way to get the full $locations outside the foreach.
$ids= DB::table('example')
->select('id')
->get();
foreach($ids as $id)
{
$locations = example::find($id->id)->locations()->get();
var_dump($locations);
}
var_dump($locations);
This is my view:
#foreach($examples as $example)
<h1>{{$example->name}}</h1>
#foreach($locations as $location)
{{$location}}
#endforeach
#endforeach
I see al the locations from al the $examples in every $example if I print it like this, I hope you understand my question.
Try this:
// Define locations array
$locations = [];
// Then do the following for each example:
// Define the array for locations of each example outside of the loop
$locations[$example->name] = [];
foreach ($ids as $id) {
// Add each location to the $locations array
$locations[$example->name][] = example::find($id->id)->locations()->get();
}
Or if you want to get a little bit more fancy, you can use array_map instead of foreach:
$locations[$example->name] = array_map(function ($id) {
return example::find($id->id)->locations()->get();
}, $ids);
Then in the view, you simply take out the locations from the right key for each example:
#foreach ($examples as $example)
<h1>{{ $example->name }}</h1>
#foreach ($locations[$example->name] as $location)
{{ $location }}
#endforeach
#endforeach
You don't have to use the $example->name as the key, just make sure it's unique for each example you want to fetch locations for.
I hope this helps.
There is no need to build the $locations array. You can use the relationship in the view just fine.
In your controller:
// whatever your logic is to get the examples
$examples = example::with('locations')->get();
In your view:
#foreach($examples as $example)
<h1>{{$example->name}}</h1>
#foreach($example->locations as $location)
{{$location}}
#endforeach
#endforeach
I want to get category name of articles. This is my CategoriesController:
public function show(Category $category)
{
$articles = $category->articles()->latest()->paginate(5);
return view('categories.index', compact('articles'));
}
When I try to do it like this: {{$articles->category()}}
I'm getting this error: call_user_func_array() expects parameter 1 to be a valid callback, class 'Illuminate\Database\Eloquent\Collection' does not have a method 'category'
You already have the category getting passed to your controller action, so just pass that instance to your view rather than trying to grab it from every article item and potentially adding more database queries to your controller action.
public function show(Category $category)
{
return view('categories.index', compact('articles', 'category'));
}
$articles would, in this case, be a collection of several articles. Therefore you can't simply do {{ $articles->category }}.
This should work, however:
#foreach ($articles as $article)
{{ $article->category }}
#endforeach
Good afternoon
I recieve this JSON
{"id":130,"nif":"47812480B","name":"Bernat","cognoms":"Fernandez","file":"uploads\/fondo.jpg","birthday":"0000-00-00","presentacion":"presentacion","email":"bernat#gmail.com","password":"$2y$10$GfP1DYkTqyohjVzRcuuxaeKBSu7iJUBWJK6UDj7p681307uI6Ersq","idempresa":1,"id_poblacion":889,"id_online":0,"remember_token":null,"created_at":"2015-05-28 15:25:04","updated_at":"2015-05-27 15:25:04","municipio":{"id":889,"idprovincia":33,"poblacion":"Art\u00e9s","poblacionseo":"artes","postal":8271,"latitud":"41.798479","longitud":"1.954828"},"subastas":[{"id":16,"nombre":"HTC Wildfire","descripcion":"Descripcion","precio_salida":125,"cant_actual":125,"id_estado":0,"id_metode_envio":1,"id_metodo_pago":1,"id_creador":130,"id_ganador":125,"id_categoria":33,"id_adquirido":1,"data_inici":"2015-06-13","data_final":"2015-06-13","durada":1,"created_at":"2015-06-06 09:09:54","updated_at":"2015-06-06 09:55:49"}]}<p>34.884533408812</p> {"id":131,"nif":"263","name":"Lalo","cognoms":"Lelo","file":"uploads\/count.png","birthday":"0000-00-00","presentacion":"jejjsjsj","email":"vsdfsfaf#aasa.com","password":"$2y$10$QtrolUS9emCS7bfirsly9.JXt9AsYfAc.\/vA0iJCs47\/3g\/ypc8d6","idempresa":1,"id_poblacion":175,"id_online":0,"remember_token":"B1vEqdX8w42i7sLNvG201EArwSTrODe0DDzkVmjzj48ahMMV8oOLmzYRM5Mp","created_at":"2015-05-20 06:52:59","updated_at":"2015-06-06 08:35:13","municipio":{"id":175,"idprovincia":9,"poblacion":"Benej\u00fazar","poblacionseo":"benejuzar","postal":3390,"latitud":"38.083525","longitud":"-0.836494"},"subastas":[{"id":15,"nombre":"Nombre","descripcion":"Descripcion","precio_salida":142,"cant_actual":142,"id_estado":1,"id_metode_envio":1,"id_metodo_pago":1,"id_creador":131,"id_ganador":131,"id_categoria":33,"id_adquirido":1,"data_inici":"2015-06-18","data_final":"2015-06-17","durada":1,"created_at":"2015-06-06 00:00:00","updated_at":"2015-06-06 10:00:57"}]}<p>475.51158492016</p>
When I use the following foreach I show the information
#foreach ($subastas as $subasta)
{{$subasta}}
#endforeach
Now I try to use the following code
#foreach ($subastas as $subasta)
#foreach ($subasta->subastas as $sub)
{{$sub}}
#endforeach
#endforeach
And I recieve this error
Trying to get property of non-object (View: C:\xampp3\htdocs\laravel\resources\views\prodcercanos.blade.php)
Any solution for this problem ?
The question is , how I can render the json in laravel ?
The controller to recieve the JSON
public function productoscercanos ($lat,$long){
$subastas = Subasta::all();
$categorias = Categoria::all();
$provincias = Provincia::all();
$cercanos = User::with(array('municipio', 'subastas'))->get();
$coordA = Geotools::coordinate([$lat,$long]);
$output = [];
foreach ($cercanos as $p) {
if(count($p->subastas)>0){
$coordB = Geotools::coordinate([$p->municipio->latitud,$p->municipio->longitud]);
$distance = Geotools::distance()->setFrom($coordA)->setTo($coordB);
$dis = $distance->in('km')->haversine();
$output[$dis] = $p."<p>$dis</p>";
}
}
ksort($output);
return view('prodcercanos')->with('categorias',$categorias)->with('provincias',$provincias)->with('subastas',$output);
}
$json = (your json file);
$subasta = json_decode($json);
then you should be able to use the json as a object now.
Try array syntax in the second foreach loop.
#foreach ($subasta['subastas'] as $sub)
{{$sub}}
#endforeach
Your code is outputting exactly what is defined. First look at how the controller is building the array.
$output[$dis] = $p."<p>$dis</p>";
$p is a User model from the variable $cercanos, appending it to $dis results in it being converted into JSON and added to your output array which your view iterates and outputs.
It looks like you are trying to add the distance to the user and pass it on.
Instead use an Accessor & Mutator to add an extra attribute to your model then pass on the $cercanos list to the view, from there you can render using a for each and grab the value.
#foreach($cercanos as $c)
{{ $c->username }} {{ $c->dis }}
#endforeach
Update
Once you have added your accessor and mutator you will be able to sort using Collections built in method.
$cercanos->sortBy('dis');