Laravel 6 pagination - php

Hi i am trying to paginate data from the database and i am getting the error below
"Call to undefined method App\Material::links() (View: C:\xampp\htdocs\project104\resources\views\swapview.blade.php)"
I don`t know where i am getting this wrong, i tried changing variables but still got an error
below is my controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Material;
class SwapViewController extends Controller
{
public function index()
{
return view('swapview');
}
public function show()
{
$inf = Material::paginate(4);
$info = Material::orderBy('created_at', 'DESC')->get();
return view('swapview',['info'=>$info]);
}
}
below is my view
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
#foreach($info as $inf)
<div style="background-color:#162238; color:white;" class="card mb-3" style="max-width: 540px;">
<div class="row g-0">
<div class="col-md-4">
<img
src="{{asset('images/' . $inf->imogi)}}"
alt="Trendy Pants and Shoes"
/>
</div>
<div class="col-md-8">
<div class="card-body">
<div style="text-align: right;">
<i style="color:#3490dc" class="fa fa-building-o" style="font-size:24px"></i> <small>{{$inf->organazation}}</small> <small><i class="fa fa-calendar" style="color:#3490dc" aria-hidden="true"></i> {{ $inf->created_at->format('d/m/Y') }}</small>
</div>
<br/>
<!--LIVEWIRE online status-->
#if($inf->isOnline())
<small><div class="circle"></div></small> <small style="color:#22bb33;">operative</small>
#else
<small><div class="ocircle"></div></small> <small>inoperative</small>
#endif
<h5 class="card-title"><i class="fa fa-user-circle-o" style="color:#3490dc" aria-hidden="true"></i> {{$inf->name}}</h5>
<small style="color:#3490dc;"><i class="fa fa-clock-o" style="color:#3490dc;"></i></small> <small style="color:#3490dc;"> posted {{ \Carbon\Carbon::parse($inf->created_at)->diffForHumans() }}</small><br/>
<small><i class="fa fa-map-marker" style="color:#3490dc" aria-hidden="true"></i> {{$inf->location}}</small> | <small><i class="fa fa-phone" style="color:#3490dc" aria-hidden="true"></i>{{$inf->contact}}</small>
<hr color=3490dc>
<p class="card-text">
<small><small style="color:#3490dc;"><i class="fa fa-exchange" aria-hidden="true"></i></small> {{$inf->swap}}</small>
</p>
<hr color=3490dc>
<p class="card-text">
<small><small style="color:#3490dc;"><i class="fa fa-exchange" style="font-size:10px"></i></small> {{$inf->exchange}}</small>
</p>
</div>
</div>
</div>
<a style="color:red;" href="{{ route('report') }}"><i class="fa fa-flag" style="color:red;" aria-hidden="true"></i> <small style="color:#3490dc;">Report</small></a>
</div>
<br/>
#endforeach
{{ $inf->links() }}
</div>
</div>
</div>
#include('footer')
#endsection
I will appreciate any help, thanks in advance

In your controller, this is your show method:
public function show()
{
$inf = Material::paginate(4);
$info = Material::orderBy('created_at', 'DESC')->get();
return view('swapview',['info'=>$info]);
}
So in your return, you send the $info variable to your view, but not you $inf.
And then, in your view, you are using $info as $inf, so your $inf variable is one Material item, not the paginate Material items you get in the controller. So then, when you use $inf->links(), Laravel can't get you some pagination links... because you are not on a paginate element.
It seems there is some confusion in variable naming.

Related

update a section of a view

I have a problem updating a section of a laravel view. that section is in another view and is filled with the data of a db. What I want to do is that when clicking on a link that content is filtered and the other parts of the page remain as they were.
thanks
coursesCategories.blade.php
#extends('courses')
#section('content')
#extends('courses')
#section('content')
<div class="tab-pane fade show active" id="pills-todas" role="tabpanel" aria-labelledby="pills-todas-tab">
<div class="row justify-content-sm-center">
#foreach($cursos as $curso)
<div class="col col-12 col-sm-12 col-md-6 col-lg-4 vista_curso">
<div class="card column_course">
<a><img class="card-img-top img_course" src="{{ asset('assets/images/pictureDefault.png') }}" alt="Foto de curso" width="240" height="135"></a>
<div class="card-body column_course_interna">
<div class="include">
<div class="card-title titulo_curso">
<h3><b>{{ $curso->nombre }}</b></h3>
</div>
<ul>
<li>
<i class="fas fa-file col-1"></i>
<span class="lista_contenido col-11"> 4 documentos explicativos </span>
</li>
<li>
<i class="fas fa-clock col-1"></i>
<span class="lista_contenido col-11"> 32 minutos </span>
</li>
</ul>
</div>
<div class="compra">
<div><button type="button" class="btn btn-lg btn_suscribe btn_ver" style="width: 84%;">Ver Curso</button></div>
<div><button type="button" class="btn btn-lg btn_suscribe" style="width: 84%;">Suscribirse</button></div>
</div>
<div class="texto_precio">
<span class="price">20.99€</span>
</div>
</div>
</div>
</div>
#endforeach
</div>
</div>
#stop#stop
courses.blade.php
<div id="mostrar_cursos" class="container-fluid justify-content-center mostrar_cursos">
<div class="btn_filtros"> <button id="btn_filtros" type="button" class="btn btn-primary btn-sm btn-block d-md-none" onclick="viewfilter();ocult()">Filtrar</button></div>
<div class="row">
<div class="d-none d-md-block col col-md-4 col-lg-3 filter" style="max-width: 304px">
<div class="nav flex-column nav-pills" id="pills-tab" role="tablist" aria-orientation="vertical">
<a class="nav-link active" id="pills-todas-tab" href="{{ action('CoursesController#showList') }}" role="tab" aria-controls="pills-todas" aria-selected="true">Todas</a>
#foreach($categorias as $categoria)
<a class="nav-link" id="pills-ofimatica-tab" href="{{ action('CoursesController#showCourses', $categoria->nombre) }}" role="tab" aria-controls="pills-".$categoria aria-selected="false">{{ $categoria->nombre }}</a>
#endforeach
</div>
<div class="rango_precio">
<form>
<label for="customRange">Rango de precio</label>
<input type="range" class="custom-range" id="customRange">
</form>
<div id="result">Valor menor que:
<b id="valor"></b>
<strong>€</strong>
</div>
</div>
<div class="order_by">
<button id="btn_ordenar" class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenu2" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Ordenar por precio
</button>
<div class="dropdown-menu" aria-labelledby="dropdownMenu2">
<button class="dropdown-item" type="button">Precio más alto</button>
<button class="dropdown-item" type="button">Precio más bajo</button>
</div>
</div>
</div>
<div class="col col-md-8 col-lg-9 row">
<div class="navegador_category container">
<div class="tab-content" id="pills-tabContent">
#yield('content')
</div>
</div>
</div>
</div>
</div>
coursesController
class CoursesController extends Controller
{
// list all categories and courses
public function showList() {
// categories extracted from the bd
$categorias = \App\Categoria::listaTodasLasCategorias();
// filtering courses according to html
$cursos = \App\Curso::listaTodosLosCursos();
return view('courses.coursesCategories',['cursos' => $cursos, 'categorias' => $categorias]);
}
// list all categories and courses
public function showCourses(Request $request, String $category) {
// categories extracted from the bd
$categorias = \App\Categoria::listaTodasLasCategorias();
// filtering courses according to html
$cursos = \App\Curso::listarCursosPorCategoria($category);
$view = View::make('courses.coursesCategories', ['cursos' => $cursos]);
if(Request::ajax()) {
$sections = $view->renderSections(); // returns an associative array of 'content', 'head' and 'footer'
return $sections['content']; // this will only return whats in the content section
}
return $view;
}
}
routes
Route::get('/courses', 'CoursesController#showList');
Route::get('/courses/{category}', 'CoursesController#showCourses');

How to display data from database in laravel

Am trying to display all the data on the admin end but i have failed. How
can i display it?
This is the method that i want to display all the data(Workflows):
public function getUserWorkflows(){
$user = $this->userRepo->users();
$active_module = AppConstants::$ACTIVE_MOD_USERS;
$appraisals = Appraisal::with(
['academicBackgrounds','keyDuties','additionalAssignments',
'competences','workflow', 'assignments','keyDuties'] )->
where('user_id','=',$user->id)->orderBy('created_at','desc')->get();
$summaries = DataGenerator::getAppraisalSummaries($appraisals);
$user = User::with(['department'])->find($user->id);
$appraisalsAssigned = DataGenerator::getAssingedAppraisals($user->id);
$assignedSummaries = DataGenerator::getAppraisalSummaries($appraisalsAssigned);
return view('user.tasks',compact('appraisals','users','active_module','summaries','appraisalsAssigned','assignedSummaries'));
}
Below is the code snippet from the view where i want the data to displayed. But it has totally failed to display. Because i think there is something that am not getting right:
<div class="col s12">
<div class="col s12">
<ul id="issues-collection" class="collection">
<li class="collection-item avatar">
<i class="material-icons circle blue darken-4">folder</i>
<span class="collection-header">Workflow</span>
</li>
#if(isset($appraisals) && count($appraisals) > 0)
#foreach($appraisals as $appraisal)
#if($loop->iteration <= 3)
<li class="collection-item">
<div class="row row-custom-modal-footer collections-title">
<div class="col s12"><strong>{{$loop->iteration}}. </strong>
{{str_replace('_',' ',$appraisal->document_name)}}</div>
</div>
<div class="row row-custom-modal-footer valign-wrapper">
<div class="col s3"><p>Date: <span class="text-primary">{{$appraisal->created_at}}</span></p></div>
<div class="col s5">
<span class="task-cat blue-stepper">
#if(isset($summaries))
#foreach($summaries as $summary)
#if($summary['id'] == $appraisal->id)
{{$summary['stage']}}
#endif
#endforeach
#endif
</span>
</div>
<div class="col s2">
<div class="progress">
<div class="determinate blue darken-4"
#if(isset($summaries))
#foreach($summaries as $summary)
#if($summary['id'] == $appraisal->id)
style="width: {{$summary['percentage']}};"
#endif
#endforeach
#endif></div>
</div>
</div>
<div class="col s1">
<p class=" camel-case"><a class="text-primary bold-text" href="{{route('view_appraisal',[$appraisal->id])}}">View</a></p>
</div>
<div class="col s1">
<p class=" camel-case"><a class="text-primary bold-text" href="{{route('view_appraisal',[$appraisal->id])}}">Stop Workflow</a></p>
</div>
</div>
</li>
#endif
#endforeach
<li class="collection-item">
<div class="row">
<div class="col s12 spacer-top">
View all your tasks
</div>
</div>
</li>
#else
<li class="collection-item">
<div class="col s12 bold-text">
<p>There are no pending tasks</p>
</div>
</li>
#endif
</ul>
</div>
</div>
I want when the person logs in as an admin, they are able to see all the data from the database once they access the view. But it only displays the data when the person logs in as a user.
I have found the solution hence i just needed to make these edits in my controller method:
public function getUserWorkflows(){
$user = $this->userRepo->users();
$active_module = AppConstants::$ACTIVE_MOD_USERS;
// Edited this line of code
$appraisals = Appraisal::with(
['academicBackgrounds','keyDuties','additionalAssignments',
'competences','workflow', 'assignments','keyDuties'] )->
orderBy('created_at','desc')->get();
$summaries = DataGenerator::getAppraisalSummaries($appraisals);
$user = User::with(['department'])->find($user->id);
$appraisalsAssigned = DataGenerator::getAssingedAppraisals($user->id);
$assignedSummaries = DataGenerator::getAppraisalSummaries($appraisalsAssigned);
return view('user.tasks',compact('appraisals','users','active_module','summaries','appraisalsAssigned','assignedSummaries'));
}
Thanks so much, i have found the solution after carefully going through the controller method's lines of code

laravel return view check available

I'm working with laravel 5.6 to build my app.
When I visit the / my controller gets me an array of services like
#foreach($servicies as $service)
<div class="col-sm-4 wow fadeInDown" data-wow-duration="1000ms" data-wow-delay="300ms">
<a href="{{url("servicios/{$servicie->id}/{$service->name}")}}/">
<div class="service-icon">
<i class="fa fa-{{$service->icon}}"></i>
</div>
<div class="service-info">
<h3>{{$servicio->getAttribute('title-main')}}</h3>
<p class="btn-class">{{$servicio->getAttribute('text-main')}}</p>
</div>
</a>
</div>
#endforeach
that a I have is a route, but I'm not building all the services.
So, I would like to know if with laravel is there any easy way to check if a services is available or not, without having to put an special field in the database to check it
like
if($service->available){
return view....
}else{
return 404
}
I don't understand you very well, but from your question, you can do this in your views to resolve your problem
#if($services->count())
#foreach($servicies as $service)
<div class="col-sm-4 wow fadeInDown" data-wow-duration="1000ms" data-wow-delay="300ms">
<a href="{{url("servicios/{$servicie->id}/{$service->name}")}}/">
<div class="service-icon">
<i class="fa fa-{{$service->icon}}"></i>
</div>
<div class="service-info">
<h3>{{$servicio->getAttribute('title-main')}}</h3>
<p class="btn-class">{{$servicio->getAttribute('text-main')}}</p>
</div>
</a>
</div>
#endforeach
#else
<div class="alert alert-warning">No service available at this moment</div>
#endif
#if(!empty($services)
#foreach($servicies as $service)
<div class="col-sm-4 wow fadeInDown" data-wow-duration="1000ms" data-wow-delay="300ms">
<a href="{{url("servicios/{$servicie->id}/{$service->name}")}}/">
<div class="service-icon">
<i class="fa fa-{{$service->icon}}"></i>
</div>
<div class="service-info">
<h3>{{$servicio->getAttribute('title-main')}}</h3>
<p class="btn-class">{{$servicio->getAttribute('text-main')}}</p>
</div>
</a>
</div>
#endforeach
#else
Display whatever you want here
#endif

Undefined variable in phpstorm using laravel . Controller & blade template are provided

I'm getting this error message for my code:
Undefined variable: questions (View:
home/xxxx/PhpstormProjects/xxxxx/resources/views/home.blade.php)
Please, try to shed some light on what could be wrong.
I have provided my QuestionController & question template. Not sure, what my code is missing. Here is my code:
QuestionController:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Question;
class QuestionController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function show(Question $question)
{
return view('question')->with('question', $question);
}
question.blade.php
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row ">
<div class="col-md-8">
<div class="card">
<div class="card-header">Question</div>
<div class="card-body">
{{$question->body}}
</div>
<div class="card-footer">
<a class="btn btn-primary float-right"
href="#">
Edit Question
</a>
</div>
</div>
</div>
<div class="col-md-4">
<div class="card">
<div class="card-header"><a class="btn btn-primary `float-left"`
href="#">
Answer Question
</a></div>
<div class="card-body">
#forelse($question->answers as $answer)
<div class="card">
<div class="card-body">{{$answer->body}}`</div>`
<div class="card-footer">
<a class="btn btn-primary float-right"
href="#">
View
</a>
</div>
</div>
#empty
<div class="card">
<div class="card-body"> No Answers</div>
</div>
#endforelse
</div>
</div>
</div>
#endsection
home.blade.php
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-12">
<div class="card">
<div class="card-header">Questions
<div class="card-body">
<div class="card-deck">
#foreach($questions as $question)
<div class="col-sm-4 d-flex align-items-stretch">
<div class="card mb-3 ">
<div class="card-header">
<small class="text-muted">
Updated: {{ $question->created_at->diffForHumans() }}
Answers: {{ $question->answers()->count() }}
</small>
</div>
<div class="card-body">
<p class="card-text">{{$question->body}}</p>
</div>
<div class="card-footer">
<p class="card-text">
<a class="btn btn-primary float-right" href="{{ route('question.show', ['id' => $question->id]) }}">
View
</a>
</p>
</div>
</div>
</div>
#endforeach
</div>
</div>
<div class="card-footer">
<div class="float-right">
{{ $questions->links() }}
</div>
</div>
</div>
</div>
</div>
</div>
#endsection
The first think that comes to my mind is that you are giving a variable named question to your view in your controller and you are trying to use $questions in your foreach when it must be $question as $q
The other thing is that you may want to pass all Questions rows you have to you view, it can be accomplished (this may not be the best solution) by editing your AppServiceProvider.php
You have to add this.
use View;
use App\Question;
public function boot()
{
//
View::share('questions',Question::all());
}
But if you do this, it will share the rows obtained with all your views.
hope this helps.

how do i apply condition through the table values?

I am making a carousel which should display the info dynamically in it. And here i am trying to apply condition through which the result can be filtered but it doesn't seems to be working. Below is the code for it.(what i really want to achieve through this is to appear the results in a carousel). Any kind of help would be immensely appreciated
<div class="container">
<div class="row" style="padding: 75px">
#if($tours->continent_id == 5)
<div class="row">
<div class="col-md-9">
<h3>Featured {{$tours->continent->name}}</h3>
</div>
<div class="col-md-3">
<!-- Controls -->
<div class="controls pull-right hidden-xs">
<a class="left fa fa-chevron-left btn btn-success"
href="#carousel-example"
data-slide="prev"></a><a class="right fa fa-
chevron-
right btn btn-success" href="#carousel-example"
data-slide="next"></a>
</div>
</div>
</div>
<div id="carousel-example" class="carousel slide hidden-xs" data-
ride="carousel">
<!-- Wrapper for slides -->
<div class="carousel-inner">
<div class="item active">
<div class="row">
#foreach($tours as $tour)
<div class="col-sm-3">
<div class="col-item">
<div class="photo">
<img src="{{$tour->photo->file ?
asset($tour->photo->file): asset('images/404.png')}}"
class="img-responsive" alt="a" />
</div>
<div class="info">
<div class="row">
<div class="price col-md-6">
<h5>{{$tour->location}}</h5>
<h5 class="price-text-
color">
{{$tour->service}}</h5>
</div>
<div class="rating hidden-sm
col-md-
6">
<i class="price-text-color
fa
fa-star"></i><i class="price-text-color fa fa-star">
</i><i class="price-text-color
fa
fa-star"></i><i class="price-text-color fa fa-star">
</i><i class="fa fa-star"></i>
</div>
</div>
<div class="separator clear-left">
<p class="btn-add"> <i class="fa fa-shopping-cart"></i>Add to cart
</p>
<p class="btn-details"> <i class="fa fa-list"></i>More details
</p>
</div>
<div class="clearfix">
</div>
</div>
</div>
</div>
#endforeach
</div>
</div>
#endif
</div>
</div>
Here is the controller part of the code. which is including all the fillable values in the form view. which later can be used to apply conditions on the form or return it's table values in the form
public function index()
{$tours = Tour::all();
return view('admin/tour/slider',compact('tours'));}
Error page error message
What you want is to filter the tours and only interact with the ones that contain continent_id equals to 5?
In that case, you should improve the query on database
$tours = Tour::where('continent_id', 5)->get();
Or filter the collection
$tours->filter(function ($tour) {
return $tour->continent_id == 5;
}
In both cases, you won't be able to do {{ $tours->continent->name }} since you're working with a Collection of Tour, not with a Tour instance.
In order to print the title of the first tour you should do
$tours->first()->continent->name
All i had to do was to put if condition after the foreach statement for its values to be available for the loop at before that foreach loop i had to check for the existence of the values in the table like #if($tours). and it all worked nicely :)

Categories