Laravel - Select data to Display in Table - php

I want to ask if ever I can limit my data in table. I want to remove one data out of my 6 data.
here is my table
Now I want to remove number 3 row which is SESSION_VALIDITY how can I remove that data and make it just 5 data in my table? I want to limit my data coz' I dont need to view SESSION_VALIDITY. help thanks
my Index
#foreach ($settings as $setting)
<tr>
<td>{{ $setting->settings_code }}</td>
<td>{{ $setting->subject}}</td>
<td>{{ $setting->description }}</td>
<td></td>
</tr>
#endforeach
my Controller
public function index()
{
$settings = Setting::all();
return view('admin.settings.index', compact('settings'));
}

If you just want to not show that row in your $settings data, you can exclude it using your query like this.
public function index()
{
$settings = Setting::where('settings_code', '<>', 'SESSION_VALIDITY')->get();
return view('admin.settings.index', compact('settings'));
}
If you want to delete it entirely, you can run a one-time function to delete that row from the table.
public function deleteSessionValidity()
{
$setting = Setting::where('settings_code', 'SESSION_VALIDITY')->first();
$setting->delete();
}

You should filter your query in the controller.
Instead of Setting::all(), you should use:
$settings = Setting::where('settings_code', '!=', 'SESSION_VALIDITY')->get();

You should try this:
public function index()
{
$settings = Setting::where('settings_code', '!=', 'SESSION_VALIDITY')->get();
return view('admin.settings.index', compact('settings'));
}
OR
#foreach ($settings as $setting)
<tr>
#if($setting->settings_code != "SESSION_VALIDITY")
<td>{{ $setting->settings_code }}</td>
<td>{{ $setting->subject}}</td>
<td>{{ $setting->description }}</td>
<td></td>
#endif
</tr>
#endforeach

inside foreach put if statement
like:
#foreach ($settings as $setting)
#if ($setting->settings_code != "SESSION_VALIDITY")
your <td> here
#endif
#endforeach

Related

Attempt to read property "match_name" on null laravel-8

I have
ErrorException
Attempt to read property "match_name" on null (View: C:\xampp\htdocs\Cbangla\resources\views\admin\manage\score\index.blade.php)
Error
I want to fetch all data from my score tables This is my scores table database view
To fetch, all data from the scores table This is what I have in my ScoreController.php
public function index()
{
$data=Score::all();
$team=Team::all();
$match=Matchh::all();
$player=Player::all();
return view('admin.manage.score.index',compact('data','team','match','player'));
}
This is my Score.php model
protected $fillable = ['score_name','score_slug','team_id','match_id','player_id'];
public function team(){
return $this->belongsTo(Team::class);
}
public function matchh(){
return $this->belongsTo(Matchh::class);
}
public function playre(){
return $this->belongsTo(Player::class);
}
This is my index.blade.php
#foreach ($data as $key => $row)
<tr>
<td>{{ $key + 1 }}</td>
<td>{{ $row->score_name }}</td>
<td>{{ $row->score_slug }}</td>
<td>{{ $row->matchh->match_name }}</td>
<td>{{ $row->team->team_name }}</td>
<td>{{ $row->player->player_name }}</td>
</tr>
#endforeach
As I see, there is problem when score has not match or match not found.
First of all, you have to specify relation column, because your model is named Matchh and laravel is looking for matchh_id column.
Solution:
# Score.php model
public function matchh(){
return $this->belongsTo(Matchh::class,'match_id');
}
Tip 1:
You can use optional function, when you're not sure if model has relation or not
#foreach ($data as $key => $row)
<tr>
<td>{{ $key + 1 }}</td>
<td>{{ $row->score_name }}</td>
<td>{{ $row->score_slug }}</td>
<td>{{ optional($row->matchh)->match_name }}</td>
<td>{{ optional($row->team)->team_name }}</td>
<td>{{ optional($row->player)->player_name }}</td>
</tr>
#endforeach
Tip 2:
You can use whereHas function to be sure that Score has Matchh
public function index()
{
$data=Score::query()->whereHas('matchh')->get();
$team=Team::all();
$match=Matchh::all();
$player=Player::all();
return view('admin.manage.score.index',compact('data','team','match','player'));
}
Please add match_id to matchh relationship case the name of your relationship is different than the match id you stored in database
public function matchh(){
return $this->belongsTo(Matchh::class,'match_id');
}
You must provide key in relation because your method name is matchh and in that case relation except matchh_id but in you case it must be match_id.
So:
public function matchh(): BelongsTo
{
return $this->belongsTo(Matchh::class, 'match_id');
}

Undefined property: stdClass - LARAVEL

I'm trying to make an admin page where the owner of the store can check the info from users that singed on the website, but I always get the Undefined property: stdClass::$name error
Here's the controller function:
public function listar(){
$users = DB::table('users')->select('name')->orderBy('updated_at', 'desc')->get();
$users = DB::table('users')->select('email')->orderBy('updated_at', 'desc')->get();
$users = DB::table('users')->select('phone')->orderBy('updated_at', 'desc')->get();
}
Here's part of the form:
#foreach ($users as $u)
<tr>
<td>{{ $u->name }}</td>
<td>{{ $u->email }}</td>
<td>{{ $u->phone}}</td>
</tr>
#endforeach
I just want it to be able to show this info from the database.
You're actually running the query three separate times, overwriting the $users variable each time. You end up with the last one, and it doesn't have a name property because you only selected phone. You should run it just once and specify all the columns you want in select().
public function listar(){
$users = DB::table('users')->select('name', 'email', 'phone')
->orderBy('updated_at', 'desc')->get();
}
public function listar(){
$users = //Your user model class ::all();
return $users;
}
Then try to view
#foreach ($users as $u)
<tr>
<td>{{ $u->name }}</td>
<td>{{ $u->email }}</td>
<td>{{ $u->phone}}</td>
</tr>
#endforeach

How do I display the name from another model in my view, by the ID in laravel

So i have created 2 models 'Team' & 'Match', 1 controller 'MatchController' and a view 'matches/index.blade.php'.
The Team model has an ID & name.
The Match model has an ID, homeTeam_id & awayTeam_id.
The MatchController has an index method.
The view shows all the matches in de database correctly, but with the homeTeam_id, what I would like is show the name for the teams, from the Team model.
How do i do that? This is what i have now in my view:
#foreach ($matches as $key => $value)
<tr>
<td>{{ $value->id }}</td>
<td>{{ $value->date }}</td>
<td>{{ $value->homeTeam_id }}</td>
<td>{{ $value->awayTeam_id }}</td>
</tr>
#endforeach
You can create two relationships between Team and Match models:
public function homeTeam()
{
return $this->belongsTo('App\Team', 'homeTeam_id', 'id');
}
public function awayTeam()
{
return $this->belongsTo('App\Team', 'awayTeam_id', 'id');
}
And then load the data:
$matches = Match::with('homeTeam', 'awayTeam')->get();
To display team name do this:
{{ $value->homeTeam->name }}

Passing a variable to a view from a different controller in Laravel 4

I am building a simple voting application in Laravel 4 where candidates are being voted for. I have two entities Candidate and Vote with a one to many relationship. When someone clicks on a button, I get the id of the candidate and store it in the vote table with only one field of candidate_id. I have a VoteController function called votesuccess that looks like this
$vote = new Vote;
$candidate_id =Input::get('name');
$candidate = Candidate::find($candidate_id);
$vote = $candidate->votes()->save($vote);
$count = DB::table('votes')->where('candidate_id','==','$candidate_id')->count();
Session::flash('message', 'Successfully Cast your vote!');
return Redirect::to('voteresults')->with('count',$count);
From the above I have another VoteController function called voteresults which I pass in all the candidates like so
$candidates = Candidate::all();
return View::make('votes.voteresults',['candidates'=>$candidates]);
Am storing the votes as rows and I want to count the votes by counting the rows hence this code
$count = DB::table('votes')->where('candidate_id','==','$candidate_id')->count();
am attempting to access all the candidate information plus the number of votes they have got from a view called voteresults which looks like this
<tbody>
#foreach($candidates as $key => $value)
<tr>
<td><img src="{{$value->avatar->url('thumb')}}" alt="..."></td>
<td>{{ $value->name }}</td>
<td>{{ $value->manifesto }}</td>
<td>
{{ $count }}
</td>
</tr>
#endforeach
</tbody>
Am attempting to pass the $count variable to the view so that I can capture the number of votes for a candidate. I don't know if it can be done this way or there is a better way to do it.When I try to run this I get undefined variable count Any guidance is welcome.
If i get you correctly you want to pass two different things (candidates and votes) from your controller to your view?
Controller:
$candidates = Candidate::all();
$count = DB::table('votes')->where('candidate_id','=','$candidate_id')->count();
return View::make('votes.voteresults')->with('candidates', $candidates)->with('count',$count);
View:
<tbody>
#foreach($candidates as $key => $value)
<tr>
<td><img src="{{$value->avatar->url('thumb')}}" alt="..."></td>
<td>{{ $value->name }}</td>
<td>{{ $value->manifesto }}</td>
<td>
{{ $count }}
</td>
</tr>
#endforeach
</tbody>
See this documentation:
Views
I believe one issue you have is that within your where criteria for $count query you have your $candidate_id variable within single quotes. I didn’t test, but try the code below.
$vote = new Vote;
$candidate_id =Input::get('name');
$candidate = Candidate::find($candidate_id);
$vote = $candidate->votes()->save($vote);
Session::flash('message', 'Successfully Cast your vote!');
return Redirect::to('voteresults');
Add a method to Candidate model similar to this:
public function getTotalVotesAttribute()
{
return $this->hasMany('Vote')->whereCandidateId($this->id)->count();
}
In your VoteResults controller:
$candidates = Candidate::all();
return View::make('votes.voteresults',['candidates'=>$candidates]);
In your voteresults view:
<tbody>
#foreach($candidates as $key => $value)
<tr>
<td><img src="{{$value->avatar->url('thumb')}}" alt="..."></td>
<td>{{ $value->name }}</td>
<td>{{ $value->manifesto }}</td>
<td>
{{ $value->total_votes }}
</td>
</tr>
#endforeach
</tbody>

Laravel hasMany relationship not looping through to display results

An alert can have many messages associated with it, through a foreign key. Each message sent is also attached to a user, through a foreign key. On viewing the alert, if such messages exist (they are not required), I want to display each message, along with the associated user's details.
User model:
public function alerts()
{
return $this->hasMany('Alert');
}
public function messages()
{
return $this->hasMany('Message');
}
Alert model:
public function user()
{
return $this->belongsTo('User');
}
public function messages()
{
return $this->hasMany('Message');
}
I have noticed if the alert doesn't have any messages associated with it, the forloop doesn't work!
Within my show view, I have:
#foreach($alerts as $alert)
<tr>
<td>{{ $alerts->messages->first()->firstname }}</td>
<td>{{ $alerts->messages->first()->user->email }}</td>
<td>{{ $alerts->messages->first()->user->phone_number }}</td>
<td>{{ $alerts->messages->first()->message }}</td>
<td>{{ date("j F Y", strtotime($alerts->messages->first()->created_at)) }}</td>
<td>{{ date("g:ia", strtotime($alerts->messages->first()->created_at)) }}</td>
</tr>
#endforeach
Which works great, if there are messages to show, but it only loops through the first message, not the rest of them. The controller pulling in the data is:
public function show($id)
{
$alert = Alert::where('id','=',$id)->first();
$this->layout->content = View::make('agents.alert.show',
array('alerts' => $alert));
}
Any guidance as to why the forloop doesn't work when there is less 2 results, and why it only loops through the first result. Thank you.
First I suggest using eager loading for related models or you will run many db queries that you don't want nor need:
public function show($id)
{
$alert = Alert::with('messages.user')->where('id','=',$id)->first();
$this->layout->content = View::make('agents.alert.show', array('alert' => $alert));
}
Then in you view spin through messages, not alerts as you don't have many of them:
#foreach($alert->messages as $message)
<tr>
<td>{{ $message->firstname }}</td>
// if you are sure there is a user for each message, otherwise you need a check for null on $message->user
<td>{{ $message->user->email }}</td>
<td>{{ $message->user->phone_number }}</td>
<td>{{ $message->message }}</td>
<td>{{ date("j F Y", strtotime($message->created_at)) }}</td>
<td>{{ date("g:ia", strtotime($message->created_at)) }}</td>
</tr>
#endforeach

Categories