Pagination in laravel returning method undefined - php

The pagination method returns App\Article::link undefined, any ideas? I thought this was a helper method
public function index()
{
$data = Article::paginate(5);
return view('datatypes.articles.index', compact('data'));
}
in my view:
<tbody>
#foreach($data as $data)
<tr>
<th scope="row">{{$data->id}}</th>
<td>{{$data->title}}</td>
</tr>
#endforeach
</tbody>
{{ $data->links() }}

<tbody>
#foreach($data as $_data)
<tr>
<th scope="row">{{$_data->id}}</th>
<td>{{$_data->title}}</td>
</tr>
#endforeach
</tbody>
{{ $_data->links() }}

try this in your view
<tbody>
#foreach($data as $item)
<tr>
<th scope="row">{{$item->id}}</th>
<td>{{$item->title}}</td>
</tr>
#endforeach
</tbody>
{{ $data->links() }}

Related

Laravel - ErrorException Undefined variable

I have tried to refer all the possible solutions in stackoverflow but could not solve this.
This is my Controller.
public function getMessages(){
$messages = Message::all()->toArray();
return View('admin.thesis', ['messages' => $messages]);
}
This is my route/web.php
Route::get ('/thesis','MessagesController#getMessages');
And this is my view
<div class="panel-heading">Thesis Details</div>
<div class="panel-body">
<table class="table table-bordered">
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>E-Mail</th>
<th>Contact</th>
<th>State</th>
<th>Country</th>
<th>Article</th>
<th>Author</th>
<th>Uploaded File</th>
</tr>
#foreach($messages as $row)
<tr>
<td>{{$row['firstName']}}</td>
<td>{{$row['lastName']}}</td>
<td>{{$row['email']}}</td>
<td>{{$row['contactNumber']}}</td>
<td>{{$row['state']}}</td>
<td>{{$row['country']}}</td>
<td>{{$row['article']}}</td>
<td>{{$row['author']}}</td>
<td>
<a href="{{ URL::to('/') }}/uploads/{{$row['file_path']}}">
{{$row['file_path']}}
</a>
</td>
</tr>
#endforeach
</table>
</div>
</div>
This is my error
ErrorException
Undefined variable: messages (View:
C:\xampp\htdocs\fileupload\resources\views\admin\thesis.blade.php)
How do I resolve this?
Try this on your Controller
public function getMessages(){
$messages = Message::all();
return view('admin.thesis')->with(['messages' => $messages]);
}
And in your blade file
#foreach($messages as $row)
<tr>
<td>{{ $row->firstName }}</td>
<td>{{ $row->lastName }}</td>
<td>{{ $row->email }}</td>
<!-- etc... -->
<td>
<a href="{{ URL::to('/') }}/uploads/{{ $row->file_path }}">
{{ $row->file_path }}
</a>
</td>
</tr>
#endforeach
Try this on your Controller
public function getMessages(){
$messages = Message::all()->toArray();
return view('admin.thesis', compact('messages'));
}
I used following code and its work for me :
class SidebarController extends Controller
{
public function news_widget() {
$posts = Post::take(5)->orderBy('updated_at', 'DESC')->take();
return view('index', array('data'=>$posts));
}
}
And within the view I simply use $data and its working fine for me.

Nested Loop in Eloquent in Laravel

I am having trouble with my code. I have two tables Stocks and StockLists however, I want to get all the related stock in the stocks list based on the stock code. But the result is repeated in each stocks....
Here is the link of the image https://www.screencast.com/t/rHdT2gigh
public function index()
{
$stocks = Stock::all();
foreach ($stocks as $stock) {
$stockInfos = DB::table('stocksList')
->where('stockCode', $stock->stockCode)
->take(3)
->get();
}
return view('welcome', compact('stocks', 'stockInfos'));
}
Here is my blade.php
#forelse($stocks as $stock)
<tr align="left">
<th scope="row">{{ $stock->stockCode }}</th>
<td><strong>{{ $stock->stockName }}</strong></td>
<td><strong>
#forelse($stockInfos as $stockInfo)
{{ $stockInfo->stockDate }} : {{ $stockInfo->stockPrice }}
<br>
#empty
<em>No Data</em>
#endforelse
</strong></td>
</tr>
#empty
#endforelse
You are overwritting the $stockInfos variable try with
public function index()
{
$stocks = Stock::all();
$stockInfos = [];
foreach ($stocks as $stock) {
array_push($stockInfos,DB::table('stocksList')
->where('stockCode', $stock->stockCode)
->take(3)
->get());
}
return view('welcome', compact('stocks'))->with('stockInfos',collect($stockInfos));
}
And in your view :
#forelse($stocks as $stock)
<tr align="left">
<th scope="row">{{ $stock->stockCode }}</th>
<td><strong>{{ $stock->stockName }}</strong></td>
<td><strong>
#forelse($stockInfos->where('stockCode',$stock->stockCode) as $stockInfo)
{{ $stockInfo->stockDate }} : {{ $stockInfo->stockPrice }}
<br>
#empty
<em>No Data</em>
#endforelse
</strong></td>
</tr>
#empty
#endforelse
Try this :
$stockInfos = DB::table('Stock')
->join('stocksList','stocksList.stockCode','=','Stock.stockCode')
->get();

pagination not working when clicking on page number

I am using laravel pagination. When I click on next page or any page number, nothing happens.
Controller function
public function getIndex()
{
$articles = Inspector::paginate(15);
return view('admin.inspector.test', compact('articles'));
}
View file
<table class="table table-bordered" id="mytable">
<thead>
<tr>
<th>Sr. No. </th>
<th>Email</th>
</tr>
</thead>
<tbody>
#foreach ($articles as $article)
<tr>
<td>{{ $article->id }}</td>
<td>{{ $article->email }}</td>
</tr>
#endforeach
</tbody>
</table>
{!! $articles->render() !!}
Any help is much appreciated...Thanks...
public function getIndex()
{
return view('admin.inspector.test');
}
just return view while calling the action. dont send any data from the controller, just load the view,
when the view loads, fetch the db connection with pagination method. and render the pagination as below
<?php $articles = DB::connection('table_name')->where('if any condition')->get(); ?>
<table class="table table-bordered" id="mytable">
<thead>
<tr>
<th>Sr. No. </th>
<th>Email</th>
</tr>
</thead>
<tbody>
#foreach ($articles as $article)
<tr>
<td>{{ $article->id }}</td>
<td>{{ $article->email }}</td>
</tr>
#endforeach
</tbody>
</table>
{!! $articles->render() !!}

Undefined property: Illuminate\Pagination\LengthAwarePaginator::$name

I am trying to get data from my table and show it in my view and paginate this data. I received this problem and couldn2t find any solution. I did the exactly same thing in my previous project and it worked well.
here is my Controller
public function hadiBirlikteCalisalimShow (){
$users =DB::table('birlikte_calisalim')->paginate(15);
return view('admin.birliktecalisalim',compact(['users']));
}
and this is my view
<table class="table table-bordered table-hover table-striped">
<thead>
<tr>
<th>İsim</th>
<th>Email</th>
<th>Tarih</th>
<th>İstek</th>
</tr>
</thead>
<tbody>
#foreach($users as $row)
<tr>
<td>{{$users->name}}</td>
<td>{{$users->email}}</td>
<td>{{$users->tarih}}</td>
<td>{{$users->message}}</td>
</tr>
#endforeach
</tbody>
</table>
{{$users->links()}}
#foreach($users as $row)
<tr>
<td>{{$row->name}}</td>
<td>{{$row->email}}</td>
<td>{{$row->tarih}}</td>
<td>{{$row->message}}</td>
</tr>
#endforeach
should be $row->name,$row->email, etc... not $users->name, and change {{$users->links()}} to {!! $users->render() !!}

laravel 4.1 fill table with data from data base

I'm trying to fill atable with data from my data base ive done that with laravel 4.2 but in 4.1 it won't work
the error is
Undefined variable: questions (View: C:\wamp\www\happy_Road\app\views\home\home.blade.php)
this is the view
<div class="col-md-12">
<table class="table table-striped table-bordered">
<thead>
<tr>
<th>Thémathique</th>
<th>Question</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
#foreach ($questions as $question)
<tr>
<td>{{ $question->theme }}</td>
<td>{{ $question->question }}</td>
<td >
</td>
</tr>
#endforeach
</tbody>
<tfoot></tfoot>
</table>
{{ $questions->links(); }}
</div>
this is the controller
public function index()
{
if (Auth::check())
{
$questions = questionList::paginate(10);
return View::make('home.home')->with('user',Auth::user());
}
else
{
return Redirect::to('login')->with('message',"Vous devez vous connecter d'abord");
}
}
what is wrong with this method !! any help please ! thx :)
Pass questions through to your view
return View::make('home.home')
->with('questions', $questions)
->with('user', Auth::user());

Categories