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
Related
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');
}
I have two table SALARIES and POINTAGES And between them a relationship hasMany belongsTo, I want to display for each POINTAGE a SALARIES corresponds but it gives me this error:
Undefined property: stdClass::$salarie
consulter.blade.php
#foreach($pointages as $pointage)
<tr>
<td>{{ $pointage->datep }}</td>
<td>{{ $pointage->chantier }}</td>
<td>{{ $pointage->ouvrage }}</td>
<td>{{ $pointage->nbrj }}</td>
<td>{{ $pointage->solde }}</td>
<td>{{ $pointage->salarie->nom }}</td>
</tr>
#endforeach
Pointage.php
protected $fillable = [
'salarie_id', 'datep', 'solde', 'nbrj' , 'ouvrage' , 'chantier' , 'prime' ,
];
public function salarie(){
return $this->belongsTo('App\Salarie');
}
Salarie.php
public function pointages(){
return $this->hasMany('App\Pointage');
}
pointages migration:
public function up(){
Schema::table('pointages', function (Blueprint $table) {
$table->integer('salarie_id')->unsigned()->after('id');
$table->foreign('salarie_id')->references('id')->on('salaries');
});
}
SalarieController.php
public function consulter(){
$salaries = Salarie::with('pointages')->get();
$pointages = DB::table('pointages')->get();
return view('salarie.consulter', compact('salaries','pointages'));
}
If you want to use a relationship, like public function salarie() on your Pointage model, you need to actually query it. Don't use DB::table(), use Pointage:
$pointages = Pointages::get();
Also, to avoid additional queries when looping $pointages, eager load your relationship:
$pointages = Pointages::with(["salarie"])->get();
In summary, DB::table() does not return a Pointage model, so you can't use ->salarie. Use your model.
Edit: There is a chance that a Pointage doesn't have a Salarie associated, so you need to handle that. Either enforce the relationship with a ->has() query, or check while looping:
$pointages = Pointages::with(["salarie"])->has("salarie")->get();
Or, in your view:
#if($pointages->salarie)
<td>{{ $pointage->salarie->nom }}</td>
#else
<td>No Salarie...</td>
#endif
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
Controller:
public function index() {
$data = DB::table('tusers','tbills','tpackages')
->join('tbills', 'tusers.user_id', '=', 'tbills.user_id')
->join('tpackages', 'tbills.package_id', '=', 'tpackages.package_id')
->select('tusers.user_id','tusers.company_name','tusers.first_name', 'tbills.account_no', 'tpackages.package_name','tbills.start_date','tbills.end_date','tbills.bill_status')
->get();
return View::make('account')->with('data',$data);
}
`
view:
#forelse($data as $value)
<td>{{ $value->user_id }}</td>
<td>{{ $value->company_name }}</td>
<td>{{ $value->first_name }}</td>
<td>{{ $value->account_no }}</td>
<td>{{ $value->package_name }}</td>
<td>{{ $value->start_date }}</td>
<td>{{ $value->end_date }}</td>
<td>{{ $value->bill_status }}
I want to call $data from packagecontroller to account.blade but there is an error
SQLSTATE[42P01]: Undefined table: 7 ERROR: relation "tusers" does not exist
LINE 1: ... "tbills"."end_date", "tbills"."bill_status" from "tusers" i...
^ (SQL: select "tusers"."user_id", "tusers"."company_name", "tusers"."first_name", "tbills"."account_no", "tpackages"."package_name", "tbills"."start_date", "tbills"."end_date", "tbills"."bill_status" from "tusers" inner join "tbills" on "tusers"."user_id" = "tbills"."user_id" inner join "tpackages" on "tbills"."package_id" = "tpackages"."package_id")
Thanks for your help.
Remove the other 2 table names from table(). Something like this.
public function index() {
$data = DB::table('users','bills','packages')
->join('bills', 'users.id', '=', 'bills.user_id')
->join('packages', 'bills.package_id', '=', 'packages.id')
->select('users.id','users.company_name','users.first_name', 'bills.account_no', 'packages.package_name', 'bills.start_date', 'bills.end_date', 'bills.bill_status')
->get();
return view('account', ['data' => $data]);
}
Mind column naming please. I may have changed some names, so you have to make sure you have the correct names.
In your view do
#foreach( $data as $value )
<td>{{ $value->company_name }}</td>
...
#endforeach
It seems you are skipping some steps. Learn properly through this free video series how to work with Laravel: https://laracasts.com/series/laravel-from-scratch-2017
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