I have a search function in my laravel project, when I navigate to the search page it shows all the entries in the database. I would like it to show nothing until I put something in the search field. Furthermore, when the results are displayed I would like an image to be dispalyed.
Here are the relavent files:
public function search(Request $request){
$query = $request->input('query');
$result = Photo::search($query)->get();
return view('search.results')->with('result', $result);
}
Here is the model
class Photo extends Model
{
use SearchableTrait;
protected $searchable = [
'columns' => [
'photos.title' => 10,
'photos.description' => 10,
'photos.photo' => 10,
],
];
protected $fillable = array('photo','title','description','album_id');
public function album(){
return $this->belongsTo('App\Album');
}
}
The display.blade.php
<div class="row">
<div class="col-8">
<table class="table table-bordered table-stripped">
<thead>
<tr>
<th>Image</th>
<th>Title</th>
<th>Description</th>
</tr>
</thead>
<tbody>
#foreach($result as $res)
<tr>
<th><img src="/storage/photos/."{{$album_id}}/{{$res->photo}}</a></th>
<th>{{$res->title}}</th>
<th>{{$res->description}}</th>
</tr>
#endforeach
</tbody>
</table>
In the above code I get an error message about not being able to find $album_id.
As always thank you for your help
Related
Hi Everyone I am creating a web portal. I want to add display a datatable. I use Datatable plugin for this but I am facing some issue. please help me with this.
This is my Contorller code
public function fetchTeams() {
$data = $this->Teams_model->getAllTeams();
echo json_encode(array("data" => $data));
}
My HTML view
<div class="card-body">
<table id="teamDatatable" class="display table table-bordered table-hover" width="100%">
<thead>
<tr>
<th>Employee name</th>
<th>Email</th>
<th>Gender</th>
<th>Salary</th>
<th>City</th>
</tr>
</thead>
</table>
</div>
My Model
public function getAllTeams() {
$query = $this->db->get("teams");
return $query->result_array();
$query1 = $this->db->get('Teams');
$data1 = $query->num_rows();
$response = array(
"iTotalRecords" => $data1,
"aaData" => $data
);
return $response;
}
My view
My View
No record
Hope this helps Check my server side codeigniter script here
Server side datatable codeigniter
so i'm trying to fetch data from my database into a datatable i got from getbootstrap
but i get the error:
Undefined variable: issue (View: C:\Users...\resources\views\dashboard.blade.php)
below ive listed the code where i use the variable: issue
dashboard.blade.php
<table id="datatable" class="table table-bordered table-striped table-dark">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Issue</th>
<th scope="col">begrootte tijd</th>
<th scope="col">beschrijving</th>
<th scope="col">Action</th>
</tr>
</thead>
<tfoot>
<tr>
<th scope="col">#</th>
<th scope="col">Issue</th>
<th scope="col">begrootte tijd</th>
<th scope="col">beschrijving</th>
<th scope="col">Action</th>
</tr>
</tfoot>
<tbody>
#foreach($issue as $issues)
<tr>
<th> {{ $issues->id }} </th>
<th> {{ $issues->iname }} </th>
<th> {{ $issues->begroting }} </th>
<th> {{ $issues->description }} </th>
<th>
START
STOP
</th>
</tr>
#endforeach
</tbody>
DashboardController.php
<?php
namespace App\Http\Controllers;
use App\Dashboard;
use Illuminate\Http\Request;
class DashboardController extends Controller
{
public function store(Request $request)
{
$this->validate($request,[
'iname' => 'required',
'begroting' => 'required',
'description' => 'required',
]);
$issue = new Issue;
$issue->iname = $request->input('iname');
$issue->begroting = $request->input('begroting');
$issue->description = $request->input('description');
$issue->save();
return redirect('/issue')->with('success', 'Issue opgeslagen');
}
}
the model
dashboard.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Dashboard extends Model
{
protected $table = 'issue';
}
You should pass the variable to the view.
Lets say you have a HomeController, and want to pass some variable to a view from the controller.
HomeController.php
public function show()
{
$someVariable = "An Awesome Example";
return view('example', [
'someVariable' => $someVariable,
]);
}
example.blade.php
<b>{{ $someVariable }}</b>
You have to pass data to a view. Then inside your view you can show that data to the user. In my example I've created a array with the key someVariable and passed $someVariable to the value of that key.
Inside my view I can then use the key to show the value.
You didn't send $issues to blade page, change your DashboardController like this:
<?php
namespace App\Http\Controllers;
use App\Dashboard;
use Illuminate\Http\Request;
class DashboardController extends Controller
{
public function store(Request $request)
{
$this->validate($request,[
'iname' => 'required',
'begroting' => 'required',
'description' => 'required',
]);
$issue = new Issue;
$issue->iname = $request->input('iname');
$issue->begroting = $request->input('begroting');
$issue->description = $request->input('description');
$issue->save();
$issues=Dashboard::all();
return redirect('/issue')->with('success', 'Issue opgeslagen')
->with('issues',$issues);
}
}
I have two different tables, answers table and question table, i need to get question ID and answer selected by a user which i have managed to retrieve, my problem is from the same form view i need to retrieve the correct answer and the point that i created in question table.From my view the first two column are retrieved from answers table, therefore i need the second two column from question table to be retrieved
please anyone who can help me, below is how i have tried to do
Here is my view:
<table class="table table-hover">
<thead class="thead-dark">
<tr>
<th scope="col">Question No</th>
<th scope="col">Answer</th>
<th scope="col">Correct Answer</th>
<th scope="col">Marks</th>
</tr>
</thead>
<tbody>
<?php $i = 1; ?>
#foreach($results as $data)
<tr>
<td>{{$i++}}</td>
<td>{{$data->question_id}}</td>
<td>{{$data->answer}}
<td>{{$data->qns_ans}}
<td>{{$data->qns_point}}
</tr>
#endforeach
</tbody>
</table>
Here is my controller
public function index()
{
$results = Answers::all();
$qns = Questions::all();
return view('test.result')
->with('results', $results)
->with('qns', $qns);
}
first you need to make relation one to many between questions and answers
check first that answers table has question_id column
then add this method to your Question Model to make relation
function answers(){
return $this->hasMany(\App\Answer::class);
}
So now you can easily from an instance of Question Model say that
$question = Question::first()->get();
dd($question->answer);
you will see now when you dump it, it will give you the answers that belongs to this question
Here is how I did to query my two tables data
This is my controller
public function index()
{
$results=Answers::all();
$qns = Questions::all();
return view('test.result')->with('results',$results)
->with('qns',$qns);
}
This is my view
<table class="table table-hover">
<thead class="thead-dark">
<tr>
<th>S/N</th>
<th scope="col">Question No</th>
<th scope="col">Answer</th>
<th scope="col">Correct Answer</th>
<th scope="col">Marks</th>
</tr>
</thead>
<tbody>
<?php $i=1; ?>
#foreach($results as $data)
<tr>
<td>{{$i++}}</td>
<td>{{$data->question_id}}</td>
<td>{{$data->answer}} </td>
<td>{{$data->question['ans']}} </td>
<td>{{$data->question['point']}}
</tr>
#endforeach
</tbody>
</table>
Here is my model
class Answers extends Model
{
protected $fillable = ['question_id','answer'];
public function question(){
return $this->belongsTo(Questions::class);
}
}
class Questions extends Model
{
protected $fillable =
['question_name','opt1','opt2','opt3',
'opt4','ans','point'];
public function answer(){
return $this->hasOne(Answer::class);
}
}
I used laravel hasmany relationship in my user model. I just want to convert my table like below (row to column):
To:
Here is my User model:
public function meal(){
return $this->hasMany(Meal::class);
}
My controller:
public function index()
{
$members = User::all();
return view('admin.meal')->with([
'members' => $members,
]);
}
and #blade
<table class="table table-striped #if(count($members)>7) table-responsive #endif ">
<thead>
<tr class="text-uppercase">
{{--<th class="font-w700">Date</th>--}}
#foreach($members as $member)
<th class="font-w700">{{str_limit($member->fname,5,'...')}}</th>
#endforeach
</tr>
</thead>
<tbody>
#foreach($members as $member)
<tr>
#foreach($member->meal as $meal)
<td><span class="font-w600">{{$meal->user_id}}</span></td>
#endforeach
</tr>
#endforeach
</tbody>
</table>
I'm trying to make a dashboard and I want to display the last 5 users that are registered on the site, So the most recent 5 registered people. How can I accomplish this? I need to have access to all their attributes, for instance, Name, Lastname etc.
This is my route: Route::get('/adminpanel', 'AdminController#index')->name('adminpanel');
And this is the controller:
public function index()
{
Carbon::setLocale('nl');
$tijd = Carbon::today();
$posts = Post::count();
$users = User::count();
$replies = Reply::count();
return view('admin.dashboard', compact('posts', 'users', 'replies', 'tijd'));
}
They need to be displayed in a table like this:
<table class="table table-striped">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Lastname</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">1</th>
<td>Rainier</td>
<td>Laan</td>
<td>rainier.laan#home.nl</td>
</tr>
</tbody>
</table>
Thanks in advance
You can use take() to get specific number of records.
$users = User::orderBy('id', 'desc')->take(5)->get();
And on your view-
#foreach($users as $user)
{{$user->name}}
#endforeach
Hope it helps :)
This one will take 5 latest users.
$users = User::latest()->take(5)->get();