Fecthing the data from database in Laravel 5.1 - php

Good day
I'm facing some errors while trying to fetch data from database
here are my codes
my table name is meeting and I used the formal documentation to write the codes
controllers.php
public function meetingdet()
{
$mretive = \DB::table('meeting')-> $name = $request->input('meetingname');}
view
<table border="1" >
<tr>
<td><b>blah</b> </td>
<td><b>blah</b> </td></tr>
<tr>
<td>
</td>
<td><li>foreach ($name as $retrive) {
echo $retrive-> meeting;}
#endforeach
</li>
</td>
</tr>
</table>
but this what is showing for me
syntax error, unexpected 'endforeach' (T_ENDFOREACH) and whan I remove
endforeach it display for me the for each without fetching the data
By the way I'm working on laravel 5.1
Regards

You can try with the following sample code:
controllers.php
//include your model file
use App\model;
public function meetingdet()
{
$meetingData = model::getMeetingData();
$view = array(
'name'=>$meetingData
);
return view('test', compact('meetingData'));
}
Model.php
public static function getMeetingData()
{
$value=DB::table('meeting')->orderBy('meeting_id', 'asc')->get();
return $value;
}
view.blade.php
<table border="1" >
<tr>
<td><b>blah</b> </td>
<td><b>blah</b> </td></tr>
<tr>
<td>
</td>
<td>
<ul>
<li>
#foreach ($meetingData as $retrive)
{{{ $retrive->meeting_id }}}
#endforeach
</li>
</ul>
</td>
</tr>
</table>

<li>
#foreach ($name as $retrive)
{{ $retrive->meeting }}
#endforeach
</li>
you didn't add # to foreach..

Related

Laravel : Passing value from view to controller

My View:
<table border="1" style="text-align: left;">
<td>
<b>Firstname</b>
</td>
<td>
<b>Actions</b>
</td>
#foreach($Newslist as $News)
<tr>
<td>
{{ $News->lastname }}
</td>
<td>
<button type="button" class="btn btn-primary">Edit</button>
</td>
</tr>
#endforeach
</table>
My routes/web.php:
Route::Post('NewsEdit/{{$News->id}}/EditForm','NewsControllere#EditForm');
My Controller.php:
<?php
namespace App\Http\Controllers;
use App\Newsmodel;
use Illuminate\Http\Request;
class NewsControllere extends Controller
{
public function EditForm($NewsId)
{ dd(request()->all());
echo "deepakkeynes";exit();
//$Newsmodel = Newsmodel::find($NewsId);
//return view('/News')->with('News',$Newsmodel);
}
}
While clicking the edit button in the view, the following result is obtained:
Result: 404 page
Expected Result: The Id of the edit button along with the echo value!
Can anyone help me out? I am a newbie in laravel..!
Have a look at this LaraCast tutorial on Route Model Binding. This explains the underlying documentation well.
Essentially:
routes/web.php:
Route::get('NewsEdit/{news}/EditForm','NewsControllere#EditForm');
newsController.php:
public function EditForm(Newsmodel $news)
{
return view('/News')->with('News',$news);
}

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

I am trying to get data from my table and show it in my view and paginate this data. I received this problem and couldn't find any solution. I did the exactly same thing in my previous project and it worked well.
here is my controller
foreach ($userDetails->wanttoread as $key => $bookid) {
$bookDetails = DB::table('books')->where('_id', $bookid)->paginate(10);
array_push($wanttoreadArr, $bookDetails);
}
$data['wanttoread'] = $wanttoreadArr;
return view('frontend.user.userShelve')->with($data);
And this is my view
<tbody>
#foreach($wanttoread as $book)
<tr>
<td><img src="{{ URL::to($book->thumbnailLink) }}" class="img-thumbnail img-responsive"></td>
</tr>
#endforeach
</tbody>
You can solve this in two ways
1 solution-change blade
<tbody>
#foreach($wanttoread as $books)
#foreach($books as $book)
<tr>
<td><img src="{{ URL::to($book["thumbnailLink"]) }}" class="img-thumbnail img-responsive"></td>
</tr>
#endforeach
#endforeach
</tbody>
2 solution chage controller logic(best solution getting paginated result and all time same count items)
$bookDetails = DB::table('books')->whereIn('_id', $userDetails->wanttoread)->paginate(10);
$data['wanttoread'] = $wanttoreadArr;
return view('frontend.user.userShelve')->with($data);
you can try this (Leave your controller code as it is)
<tbody>
#foreach($wanttoread as $row)
#foreach($row->items() as $book)
<tr>
<td><img src="{{ URL::to($book['thumbnailLink']) }}" class="img-thumbnail img-responsive"></td>
</tr>
#endforeach
#endforeach
</tbody>
Assuming that you are getting a LengthAwarePaginator, you need to get the items that are currently being paginated, so you could do the following:
<tbody>
#foreach($wanttoread->items() as $book)
<tr>
<td><img src="{{ URL::to($book->thumbnailLink) }}" class="img-thumbnail img-responsive">
</td>
</tr>
#endforeach
The items() function will return the dataset. You can find out more at the official API docs.

Why do I keep getting 'Undefined variable: charities' in Laravel 5.4?

I keep getting this undefined variable error. I am trying to display all the charities to the users, I am doing the exact same function for the 'charityowners' type of user and it is working for them.
Route:
Route::get('charities', 'HomeController#displayCharities');
Home Controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use Auth;
use Image;
use App\Charities;
class HomeController extends Controller
{
public function displayCharities()
{
$charities = Charities::all();
return view('charities', ['charities' => $charities]);
}
}
View:
<table class="table">
<tr>
<td> <strong> Name </strong> </td>
<td> <strong> Image </strong> </td>
<td> <strong> Description </strong> </td>
<td> <strong> Action </strong> </td>
</tr>
#foreach($charities as $charity)
<tr>
<td> {{ $charity->name }} </td>
<td> Not Available </td>
<td> {{ $charity->description }} </td>
</tr>
#endforeach
</table>
The error I keep getting is:
ErrorException in d9249250b321fd33ae875d6ca2417a0420928492.php line 39:
Undefined variable: charities (View: C:\wamp64\www\EasyDonationNew\laravel\resources\views\charities.blade.php)
What am I missing??
You can try this
return view('charities')->with('charities', $charities);
It works now by adding:
#if(isset($ownedCharities))
Before the foreach loop.
Try initializing the variable $charities globally as an empty string or as null.
On the global scope a var is only seen if it is defined.

Displaying multiple columns of data [from a PHP list] in a table in Laravel View

I currently am using a Laravel View with the following HTML that displays data onto a table using the <td> tag.
#if($unrostered)
<table class="table table-hover table-striped">
<thead>
<tr>
<th>#</th>
<th>Name</th>
</tr>
</thead>
<tbody>
<?php $index = 1; ?>
#foreach($unrostered as $name => $profile)
<tr>
<td>
<?= $index; $index++; ?>
</td>
<td>
<a
href="{{ $profile }}">{{ $name }}
</a>
</td>
</tr>
#endforeach
</tbody>
</table>
#endif
I am currently just using one associative list called $unrostered which contains the key and value called name and profile. This shows two columns of data, like this:
As you can see, the name variable is the name of the person, and the profile is a URL that links to their profile.
My HTML skills aren't good. I want to pass in multiple lists [of the same length] into my HTML table, so each list occupies their own column.
I tried juggling the row and data tags, however, all I was able to achieve were that all the data points occupied only one row instead of columns.
Assume the name of your other lists are $list1, $list2, ... and they have the same format as your $unrostered. Then your foreach code should be changed to:
<?php $index = 1; ?>
#foreach($unrostered as $name => $profile)
<tr>
<td>
<?= $index; $index++; ?>
</td>
<td>
<a
href="{{ $profile }}">{{ $name }}
</a>
</td>
<td>
<a
href="{{ $list1[$name] }}">{{ $name }}
</a>
</td>
<td>
<a
href="{{ $list2[$name] }}">{{ $name }}
</a>
</td>
</tr>
#endforeach

Server side pagination of laravel 5

I am having some trouble implementing server side pagination in Laravel 5.
Do you have any idea please tell
try with this one
//in controller
$users = \App\User::paginate(15)
return view('your desired view file name', compact('users'));
// in view
<div class="text-center text-muted" role="status" aria-live="polite">Showing {{$users->firstItem()}} to {{$users->lastItem()}} of {{$users->total()}} entries</div>
<div style="display:flex;justify-content:center;align-items:center;" >
<p></p>
{!! with(new Illuminate\Pagination\BootstrapThreePresenter($users))->render()!!}
</div>
In controller you do somrthing like following
$users = \App\User::paginate(15)
return view('template.file', compact('users'));
and in View file you put following
<table>
<thead>
<tr>
<th>Name</th>
</tr>
</thead>
<tbody>
#foreach ($users as $user)
<tr>
<td> {{ $user->name }} </td>
</tr>
#endforeach
</tbody>
</table>
{!! $users->appends(\Input::except('page'))->render() !!}
Last line renders pagination links.

Categories