Laravel5.1, Relation, EloquentORM, Many to Many, Sort - laravel

<table border="true">
<tr>
<th style="width:100px">Table</th>
<th colspan="3" style="width:300px">Columns</th>
</tr>
<tr>
<td style="width:100px">Articles</td>
<td style="width:100px">id</td>
<td colspan="2" style="width:200px; color: #aaaaaa">other dependent variables</td>
</tr>
<tr>
<td style="width:100px">Article_Tag</td>
<td style="width:100px">id</td>
<td style="width:100px">article_id</td>
<td style="width:100px">tag_id</td>
</tr>
<tr>
<td style="width:100px">Tags</td>
<td style="width:100px">id</td>
<td style="width:100px">name</td>
<td style="width:100px"></td>
</tr>
</table>
Then, I want to sort Articles table by [name] of Tags table.
I tried eager-loading with closure below, but it did'nt work.
Model Article and Tag are connected with each other just by belongsToMany,
and I succeed in output their data, but they weren't sorted.Offcourse, I did'nt give getTag any argument.(When I gave argument, they weren't narrowed by [name] either.)
use App\Article;
use App\Tag;
...
public function __construct(Article $article)
{
$this->article = $article;
}
...
public function getTag($tag = NULL)
{
$flag = isset($tag);
$articles = $this->article->orderBy('created_at', 'desc')->with([
'tags' => function($query) use ($flag, $tag){
($flag)
? $query->where('name', $tag)
: $query->orderBy('name');
}
])->paginate(15);

Try this variant:
$articles = $this->article->with([
'tags' => function($query) use ($flag, $tag){
return $flag
? $query->where("name", $tag);
: $query->orderBy("name")
}
])->paginate(15);

Related

How to check if checkbox is checked for each row

If one row is checked if($isset->has('test')) returns all rows.
I only want to save the marked rows.
My view file:
<tbody>
#foreach($match->homeTeam->players as $player)
<tr>
<th scope="row">{{$player->id}}</th>
<td class = "col-md-6" name = "player[]" value = "{{$player->id}}">{{$player->name}} {{$player->surname}}</td>
<td align="center" class= "col-md-2"><input class="form-check-input" type="checkbox" name="test" value="{{$player->id}}"></td>
<td class = "col-md-2"><input name="minutes[]" class="form-control"></td>
<td class = "col-md-2"><input name="goals[]" class="form-control"></td>
</tr>
#endforeach
</tbody>
Controller:
public function storeMatchFacts(Match $match, Request $request, Match_fact $match_fact, Player $player)
{
$home_team_players=$match->homeTeam->players;
if($isset->has('test')) {
foreach ( $home_team_players as $k => $p ) {
$data[] = [
'match_id' => $match->id,
'player_id' => $player->id,
'minutes' => $request['minutes'][$k],
'goals' => $request['goals'][$k]
];
}
dd($data);
} else {
dd('error');
}

Laravel : "Object of class stdClass could not be converted to string" while pass variable into closure

I get an error like this
ErrorException
This my link index
<td>{{$cmface}}</td>
This my Routes
Route::get('/detail_face_to_face/{id}', 'FaceToFaceController#detail')->name('detail_face_to_face');
This my Controller
public function detail($id)
{
$xclass = DB::table('face_to_face')->select('Class')->where('id', $id)->first();
$tface = DB::table('tbclass')
->select('tbclass.F_Name','tbclass.Class')
->where('tbclass.Class', $xclass)
->whereNotExists(function ($query) use ($id) {
$query->select('id_user')
->from('absent')
->where([['absent.id_face_to_face', $id],['absent.type_face_to_face', '1'],])
->whereRaw('absent.id_user = tbclass.ID_No');
})
->orderBy('tbclass.F_Name', 'ASC')
->paginate(10);
return view('face_to_face.detail',['tface' => $tface]);
}
This my face_to_face.detail page
<table class="table">
<thead style="white-space:nowrap;">
<tr>
<th>No</th>
<th>Name</th>
<th>Class</th>
</tr>
</thead>
<tbody style="white-space:nowrap;">
#if($tface->count()===0)
<tr>
<td class="table-success text-center" colspan="10"><< Data is Empty >></td>
</tr>
#else
#foreach($tface as $no => $tm)
<tr>
<td>{{ ++$no + ($tface->currentPage()-1) * $tface->perPage() }}</td>
<td>{{$tm->F_Name}}</td>
<td>{{$tm->Class}}</td>
</tr>
#endforeach
#endif
</tbody>
</table>
{{ $tface->links() }}
if I click the link on the index page, i get that error
Can anyone help ???
Could you dd($xclass)?
Error is in, ->where('tbclass.Class', $xclass). You probably do ->where('tbclass.Class', $xclass->Class)
Regard

Trying to get property 'name' of non-object. Model relationship not working

I am getting an error message relating to my model relationship and have an identical section of my other app that works fine. I've looked through everything many times and cannot see any reason my model relationship should not work. When I try to access the model relationship in the view, I get "Trying to get property 'name' of non-object"
I have the following in my Controller:
public function edit($id)
{
//
$plansubmission = PlanSubmission::find($id);
$hradmins = PSPlanToHRAdminMapping::where('plan_submission_plan_id', $id)->get();
return view('planbuilder.add-hr-administrators', compact('plansubmission', 'hradmins'));
I have the following in my View:
<h3 class="py-3">Current HR Administrators with Online Access</h3>
<table class="table">
<thead>
<tr>
<th scope="col">Name</th>
<th scope="col">Email Address</th>
</tr>
</thead>
<tbody>
#foreach($hradmins as $hradmin)
<tr>
<td> {{$hradmin->hradmin->name }}</td>
<td> {{$hradmin->hradmin->email }}</td>
#endforeach
</tr>
</tbody>
</table>
I have the following in my Model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class PSPlanToHRAdminMapping extends Model
{
//
protected $guarded = [];
public function hradmin()
{
return $this->belongsTo('App\HRAdmin');
}
}
I have the following Migration:
{
Schema::create('p_s_plan_to_h_r_admin_mappings', function (Blueprint $table) {
$table->unsignedBigInteger('plan_submission_plan_id');
$table->unsignedBigInteger('h_r_admin_id');
$table->timestamps();
$table->foreign('plan_submission_plan_id')->references('id')->on('plan_submissions');
$table->foreign('h_r_admin_id')->references('id')->on('h_r_admins');
});
}
It's possible that for some records you don't have any record for hradmin relationship so you can try adding optional helper like so:
<td> {{optional($hradmin->hradmin)->name }}</td>
<td> {{optional($hradmin->hradmin)->email }}</td>
for example to avoid error

how to get data from laravel5.5?

I am using Laravel 5.5 and I want to view my data in database from my view page (home.blade.php)
<table>
<tbody>
#foreach($home as $key => $data)
<tr>
<th>{{$data->created_at}}</th>
<th>{{$data->category}}</th>
<th>{{$data->reportitle}}</th>
<th>{{$data->reportdetails}}</th>
<th>{{$data->seenstat}}</th>
<th>{{$data->actionstat}}</th>
<th>{{$data->donestat}}</th>
</tr>
#endforeach
</tbody>
</table>
In my home.blade.php I have an empty table and I want to show data from database fyp:
Route::get('home', function () {
$fyp = DB::table('reports')->get();
return view('home', ['fyp' => $fyp]);
});
This must work (as #Nazmul said):
#foreach($fyp as $data)
<tr>
<th>{{$data->created_at}}</th>
<th>{{$data->category}}</th>
<th>{{$data->reportitle}}</th>
<th>{{$data->reportdetails}}</th>
<th>{{$data->seenstat}}</th>
<th>{{$data->actionstat}}</th>
<th>{{$data->donestat}}</th>
</tr>
#endforeach
just follow the code
Route::get('home', function () {
$home = DB::table('reports')->get();
return view('home', compact('home'));
});
After in your view page
<table>
<tbody>
#foreach($home as $key => $data)
<tr>
<th>{{$data->created_at}}</th>
<th>{{$data->category}}</th>
<th>{{$data->reportitle}}</th>
<th>{{$data->reportdetails}}</th>
<th>{{$data->seenstat}}</th>
<th>{{$data->actionstat}}</th>
<th>{{$data->donestat}}</th>
</tr>
#endforeach
</tbody>
</table>
I hope this will help you.

ASP.NET MVC Razor Headers and Views for Each Column Dynamically

I have the following Razor lines for now:
<table border=1 cellpadding=3 cellspacing=1 rules="rows" frame="box">
<tr>
<th>Türkçe Söz Dizisi</th>
<th>English Word Sequence</th>
<th></th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.Tr)
</td>
<td>
#Html.DisplayFor(modelItem => item.En)
</td>
<td>
#Html.ActionLink((String)#ViewBag.Edit, "Edit", new { id=item.ID }) |
#Html.ActionLink((String)#ViewBag.Delete, "Delete", new { id=item.ID })
</td>
</tr>
}
</table>
However, I will add some columns automatically from the program to database table. I need a way to print this th's and td's accordingly. In short, I need a way to go through Model's dynamic columns. How can I achieve this?
EDIT: My Model type here is "Proposal". However, I want to reach dynamic attribute of Proposal.Type.Word(Tr, En, or any other added Language Enum). How can I?
#foreach (var item in Model) {
Type objectType = Type.GetType(typeof(RexamOneriSistemi.Models.Word).AssemblyQualifiedName);
System.Reflection.PropertyInfo[] fields = objectType.GetProperties();
<tr>
<td>#Html.DisplayFor(modelItem => item.ID)</td>
<td>#Html.DisplayFor(modelItem => item.Owner.Name)</td>
#foreach (System.Reflection.PropertyInfo f in fields) {
if (f.Name.ToString() == #HttpContext.Current.Session["Language"].ToString())
{
<td>
// What to do here exactly to get item.Type.Word.[dynamic attribute]?
</td>
}
</tr>
}
I can get Razor String
string s = "#Html.Displayfor(modelItem => item.Type.Word." + System.Web.HttpContext.Current.Session["Language"] + ")"
Resulting: #Html.Displayfor(modelItem => item.Type.Word.Tr)
Can I insert a string to be rendered as Razor Syntax? If yes, how?
I have tried this code and it works
<table border=1 cellpadding=3 cellspacing=1 rules="rows" frame="box">
<tr>
#{
Type objectType = Type.GetType(typeof(yourProjectNamespace.Models.Language).AssemblyQualifiedName);
System.Reflection.PropertyInfo[] fields = objectType.GetProperties();
foreach (System.Reflection.PropertyInfo f in fields)
{
<th> #f.Name.ToString() </th>
}
}
</tr>
#foreach (yourModelType item in Model) {
<tr>
foreach (System.Reflection.PropertyInfo f in fields)
{
<td>#Html.Display(f.Name.ToString())</td>
}
<td>
#Html.ActionLink((String)#ViewBag.Edit, "Edit", new { id=item.ID }) |
#Html.ActionLink((String)#ViewBag.Delete, "Delete", new { id=item.ID })
</td>
</tr>
}
</table>
To get Assemply Qualified Name of your class , see this link here
Came across this question, wrote this maybe it will help someone else. This code will dynamically load your model into a table.
#model System.Collections.Generic.List<App.Client.Models.MyModel>
<table>
<thead>
<tr>
#foreach (var property in Model.GetType().GetGenericArguments()[0].GetProperties())
{
<th>#property.Name</th>
}
</tr>
</thead>
<tbody>
#foreach (var modelItem in Model)
{
<tr>
#foreach (var property in modelItem.GetType().GetProperties())
{
<td>#property.GetValue(modelItem)</td>
}
</tr>
}
</tbody>
</table>

Resources