This question already has answers here:
Laravel Collection paginate does not exist
(2 answers)
Closed last year.
This is my controller file's index function
public function index()
{
$projects = Projects::All()->paginate( 5 );
return view('projects.index')->with('projects', $projects);
}
And, This is my blade file's pagination code
#foreach ($projects as $project)
{{ $project->links('pagination::tailwind') }}
#endforeach
But, Still, there's an error, that is saying
BadMethodCallException
Method Illuminate\Database\Eloquent\Collection::paginate does not exist.
You have a typo. This is not correct:
{{ $project->links('pagination::tailwind') }}
Error 1: You are missing a 's':
{{ $projects->links() }}
Error 2: You do not have to put your links in the loop but outside the loop. normaly after the loop.
#foreach($records as $record)
{{ $record->field }}
#endforeach
{{ $records->links() }}
Suggestion: I prefer to tell laravel which paginator I am using in this file:
App/Providers/AppServiceProvider.php
public function boot()
{
Paginator::useBootstrap(); //bootstrap in this case
}
For tailwind, there is no need to specify anything, since it is the default.
This question already has answers here:
Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?
(3 answers)
Closed 1 year ago.
I want build a new collection from an existing collection using an each() loop:
$spot = Spot::where('slug', $slug)->first()->load('features.group');
$test = collect();
$spot->features->each(function ($item, $key) {
$current = collect([
$item->group->name => $item->name,
]);
$test->mergeRecursive($current);
});
dd($test);
I am getting the error Undefined variable $test from the code inside the loop. How can I access the $test collection from inside the loop?
Thanks!
You can pass data inside the closure by use(). Try this:
$spot = Spot::where('slug', $slug)->first()->load('features.group');
$test = collect();
$spot->features->each(function ($item, $key) use(&$test) {
$current = collect([
$item->group->name => $item->name,
]);
$test->mergeRecursive($current);
});
dd($test);
This question already has answers here:
Laravel Eloquent Pluck without losing the key
(7 answers)
Closed 1 year ago.
I would like Laravel query-builder to return an array of key => value.
For example, for users table which has columns username, email return something like:
Array
(
[email1] => username1
[email2] => username2
)
Now, I can achieve each one of the with either pluck('username') or with keyBy('email').
I can use php array functions to manipulate result returned by keyBy in order to achieve what I want:
$tmpRes = DB::table('users')
->select(['username', 'email'])
->get()->keyBy('email')->toArray();
$res = array_map(function ($el) {
return $el->username;
}, $tmpRes);
I wonder whether there is a direct way to do so.
Pluck actually allows a second parameter as a key
public static function pluck($column, $key = null)
So all you have to do is pass that in
$tmpRes = DB::table('users')
->pluck('username', 'email')
->toArray();
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');
This question already has answers here:
How to pass data to view in Laravel?
(15 answers)
Closed 2 years ago.
I have made a list query that populates my drop down box. I have tried var_dump on the controllers part and it all went well, but whenever I tried to call my function on my blade template, it would return me an error: Undefined variable: categories (View: C:\wamp\www\airlines\app\views\content\onewayflight.blade.php)
What seems to be the problem here?
OnewayflightController.php
public function onewayflight()
{
$categories = DB::table('oneways')->lists('destination-from');
return View::make('content.onewayflight')->with('destination-from', $categories);
}
onewayflight.blade.php
{{ Form::select('destination-from', $categories) }}
routes.php
Route::get('flight/onewayflight','OnewayflightController#onewayflight');
You should use in Blade:
{{ Form::select('destination-from', $destination-from) }}
because in your method you used:
with('destination-from', $categories)
so you told that in Blade $categories have to be named $destination-from
However you cannot use - in variable name, so you should probably change it into:
with('destinationFrom', $categories)
and in Blade:
{{ Form::select('destination-from', $destinationFrom) }}