i tried to show some data from database to my view. But i get undefined variable error.
Route::get('pages/tab-content-games', 'Admin\TournamentCrudController#get_data');
and this is my function
public function get_data(){
$data['data'] = \DB::table('tournament')->get();
return view('pages.tab-content-games', ['data' => $data]);
}
in tab-content-games.blade.php
i just print the variable
{{ print_r($data) }}
can someone helped me, what part i'm doing wrong. thankyou
Please try with this return view('pages.tab-content-games', compact('data'));
Try this:
i think you have to use rural name as your database name tournaments instead of tournament. also you can use compact.
public function get_data(){
$data = \DB::table('tournaments')->get();
return view('pages.tab-content-games', compact('data');
}
PS: consider using model in your controller instead of using DB for simple functions.
use App\Tournament;
public function get_data(){
$data = Tournament::all();
return view('pages.tab-content-games', compact('data')
}
Try this:
return view('pages.tab-content-games')->with('data', $data);
Related
so here is my model codes:
public function GetId()
{
$this->db->select('count(*) as total');
$this->db->from($this->table);
$this->db->where('dibaca', null);
$query = $this->db->get();
return $query->result_array();
}
and this is my code in html:
<?= $DataId['total']; ?>
i already call the function to my controller as DataId,
and i get error that Undefined array key "total"
can you guys tell me what is wrong in it?
Some untested advice:
Your model can be refined to:
public function countNullDibaca(): int
{
return $this->db
->where("dibaca", null)
->count_all_results($this->table);
}
Your controller should call for the model data and pass it to the view.
public function myController(): void
{
$this->load->model('my_model', 'MyModel');
$this->load->view(
'my_view',
['total' => $this->MyModel->countNullDibaca()]
);
}
Finally, your view can access the variable that associates with the first-level key in the passed in array.
<?= $total; ?>
Here is a related post which speaks on passing data from the controller to the view.
Replace the result_array() at the model with
num_rows()
and you can delete ['total'] from your code in html, or like this:
<?= $DataId; ?>
I am trying to get data from the database and simply display it in the view
public function index()
{
$messages = ProjectInterestedMessages::get();
return view('dashboard/projects-interests', compact($messages));
}
The view
#foreach ($messages as $message)
<h1>{{ $message->first_name }}</h1>
#endforeach
But I am getting this error
compact(): Argument #1 must be string or array of strings, Illuminate\Database\Eloquent\Collection given
The PHP method compact() is a little tricky with its syntax. I make this same mistake all the time.
Change:
return view('dashboard/projects-interests', compact($messages));
to:
return view('dashboard/projects-interests', compact('messages'));
compact() looks for a string representation of the variable.
There are multiple ways you could write this.
using compact where you pass strings representing the variable
public function index()
{
$messages = ProjectInterestedMessages::get();
return view('dashboard/projects-interests', compact('messages'));
}
using ->with
public function index()
{
$messages = ProjectInterestedMessages::get();
return view('dashboard/projects-interests')->with(['messages' => $messages]);
}
With using laravel magic
public function index()
{
$messages = ProjectInterestedMessages::get();
return view('dashboard/projects-interests')->withMessages($messages);
}
Personally, I prefer this form as it avoids pointless variables
public function index()
{
return view('dashboard/projects-interests')
->withMessages(ProjectInterestedMessages::get());
}
You don't need $
return view('dashboard/projects-interests', compact('messages'));
I'm trying to get data from my DB using a model, to then be used in the view. I return the query results to my controller and it gives me the undefined variable notice.
I tried first tried performing a select(get) statement in the controller, I then defined the result array as row before defining the specific row to be used, to which I pass on to my view. That threw an error, then I tried the same thing but with a model and a return to the controller:
controller.php
public function Home()
{
$this->load->model('Main');
$this->Main->getresults();
$this->load->view('header', $data);
}
model.php
public function getresults() {
$query = $this->db->get('table');
foreach ($query->result_array() as $row) {
$data = array(
'column' => $row["column"]
);
}
return $data;
}
view.php
<?php echo $column; ?>
I expect the return of $data to the controller for it to be used in the view, yet it still throws a notice of an undefined variable.
In your controller you don't assign and send data to view.
Change your code there:
public function Home()
{
$this->load->model('Main');
$data = $this->Main->getresults();
$this->load->view('header', $data);
}
In this Laravel query any one can search those fields but the problem is when someone doesn't select all of the searchable fields it will give an error. Because the whereIn method doesn't get the value of the variable. If I check it with if condition then it will be a very big sql. So, is there any easy way to do this easily. My query is below.Thanks in advance for your help.
public function filter(Request $r){
searchQuery = DB::table('jobs')->whereIn('division_id', $r->location)->whereIn('industrytype_id', $r->industry)->whereIn('category_id', $r->category)->whereIn('company_id', $r->company_id)->whereIn('created_at', $r->date)->whereIn('salary_range', $r->salary_range)->whereIn('jobType', $r->jobType)->orderByRaw($r->shortby)->get();
}
I think you just need to accept the (SQL) statement will get bigger. I guess you have something like this:?
public function filter(Request $r){
$searchQuery = DB::table('jobs');
if($r->location){
$searchQuery->whereIn('division_id', $r->location);
}
if($r->industry){
$searchQuery->whereIn('industrytype_id', $r->industry);
}
if($r->category){
$searchQuery->whereIn('category_id', $r->category);
}
if($r->company_id){
$searchQuery->whereIn('company_id', $r->company_id);
}
if($r->date){
$searchQuery->whereIn('created_at', $r->date);
}
if($r->salary_range){
$searchQuery->whereIn('salary_range', $r->salary_range);
}
if($r->jobType){
$searchQuery->whereIn('jobType', $r->jobType);
}
$searchQuery->orderByRaw($r->shortby)->get();
}
If you do this really often you could write your own DB class which inherits the laravel DB class. And write some function in your own class like $searchQuery->WhereInIfNotNull('industrytype_id', $r->industry);.
I would use an scope to add every filter to the query. Define it on your model. Quick samples:
public function scopeDivision($query,$searchParameter)
{
if(!is_null($searchParameter){
return $query->whereIn('division_id', $searchParameter);
}else{
return $query;
}
}
public function scopeIndustryType($query,$searchParameter)
{
if(!is_null($searchParameter){
return $query->whereIn('industrytype_id', $searchParameter);
}else{
return $query;
}
}
Then back in your filter use it like this:
Job::division($r->location)
->industryType($r->industry)
->category($r->category)
->company($r->company_id)
->created($r->date)
->salaryRange($r->salary_range)
->jobType($r->job_type)
->orderByRaw($r->shortby)
->get()
WhereIn is a method where you should use an array. Your method should be look like:
public function filter(Request $r){
$searchQuery=DB::table('jobs')->where('division_id',$r->location)->where('industrytype_id',$r->industry)->where('category_id',$r->category)->where('company_id',$r->company_id)->where('created_at',$r->date)->where('salary_range',$r->salary_range)->where('jobType',$r->jobType)->orderBy($r->shortby)->get();
}
I have this controller:
public function category($name)
{
$users = User::all()->where('category','$name');
return View('index',['users'=>$users]);
}
returning to the view:
You chose <mark> {{$users[0]->category}}</mark>
gives me error:
undefined offset:0
Oh boy. The problem is here '$name', change it to simple $name (without quotes).
If You want more explanations whats wrong with Your code (not related to this problem), just say it.
try this
Controller
public function category($name)
{
$user = User::where('category',$name)->select('category')->first();
return View('index')->with('user', $user);
}
VIEW
You chose <mark> {{ $user }}</mark>
Try this:
Controller :
public function category($name)
{
$users=User::where('category',$name)->get();
return View('index',['users'=>$users]);
}
View :
#foreach($users as $user) // to access all records
{{ $user->category }}
#endforeach
{{$users[0]->category}} // to access first record