Two array in one foreach Laravel - php

I have two table the first called codes, and second is company.
Codes table structure:
id| title | desc | company | order
Company table structure:
id| name
Here is code from my controller
$codes= DB::table('codes')->orderBy('company', 'ASC')->get();
$company = Company::all();
return view('codes.index',compact('codes', 'company'));
And here is codes from my view.
#foreach($codes as $code)
<tr>
<td>{{$code->title}}</td>
<td>{{$code->code}}</td>
<td>{{$company->name}}</td>
<td>{{$code->order}}</td>
</tr>
#endforeach
I can not get name of company. How is possible? Thanks for all

$services = Services::orderBy('company', 'ASC')->with('company')->get();
return view('services.index',compact('services'));
view
#foreach($codes as $code)
<tr>
<td>{{$code->title}}</td>
<td>{{$code->code}}</td>
<td>{{$code->company->name}}</td>
<td>{{$code->order}}</td>
</tr>
#endforeach
I did not know the names exactly because I wrote them because of that approximation.
If the company is defined as id, you can get company information using.
You just have to define this model in the service model.
public function company()
{
return $this->belongsTo(Companies::class);
}

Replace your view code with this
#foreach($codes as $key => $code)
<tr>
<td>{{$code->title}}</td>
<td>{{$code->code}}</td>
<td>{{$company[$key]->name}}</td>
<td>{{$code->order}}</td>
</tr>
#endforeach
And it'll work

Related

Attempt to read property "nama" on int

im trying to get the latest data on database, im using this on my views :
#foreach ($shows as $s)
<tbody>
<tr>
<th scope="row">{{$loop->iteration}}</th>
<td>{{$s->nama}}</td>
<td>{{$s->umur}}</td>
<td>{{$s->alamat}}</td>
<td>{{$s->nama_ortu}}</td>
<td>{{$s->posyandu}}</td>
<td>{{$s->result}}</td>
</tr>
</tbody>
#endforeach
and on controller :
public function showresultpasien()
{
$shows = DB::table('pasiens')->orderBy('id', 'DESC')->first();
return view('result', compact('shows'));
}
did i doin something wrong ?
The name of the function in the model cannot be the same as the name of a field in your table. Also in my case the column names do not follow the laravel/eloquent nomenclature so another parameter is added to belongsTo with the field name

How to get the username through an id in Laravel?

I am trying to create an offers forum, where some user can create their offers in order to provide their services and I want to show the name of the person that created that offer instead of the id.
In my database I have the two tables:
Offers table:
User table:
In offers I have a column of the professor_id, that is related to the id of users table.
This is what i have in my controller to show the offers:
public function ofertes(){
$ofertes = Oferta::all()->sortByDesc('id');
return view('create.ofertes')->with(compact('ofertes'));
}
and in the blade.php I have that code:
#foreach($ofertes as $oferta)
<tr>
<td>Nom : {{$oferta->professor_id}}</td> <br>
<td>Títol : {{$oferta->titol}}</td> <br>
<td>Descripció: {{$oferta->descripcio}}</td> <br>
<td>Data: {{$oferta->created_at}}</td> <br><br>
</tr>
#endforeach
and that is what is shown:
Where it says nom, how I can show the name instead of the id?
Thank you!
If you have specified the relationship to professor in your Oferta model you can use the following code:
public function ofertes(){
$ofertes = Oferta::with('professor')->latest()->get();
return view('create.ofertes')->with(compact('ofertes'));
}
Your blade:
#foreach($ofertes as $oferta)
<tr>
<td>Nom : {{$oferta->professor->nom}}</td> <br>
<td>Títol : {{$oferta->titol}}</td> <br>
<td>Descripció: {{$oferta->descripcio}}</td> <br>
<td>Data: {{$oferta->created_at}}</td> <br><br>
</tr>
#endforeach
If you haven't specified the relation you should add the following method to your Oferta model (you might need to tweak this a little bit based on your namespaces):
public function professor()
{
return $this->belongsTo(User::class);
}
Very easy
public function ofertes(){
$ofertes = Oferta::with('professor')->latest()->get();
$users = User::all();
return view('create.ofertes')->with(compact('ofertes', 'users'));
}
In blade file:
<td>Nom : #foreach ($users as $user)
#if ($oferta->professor_id === $user->id) $user->nom $user->cognom
#endif
#endforeach
</td><br>
Join two table like
$oferta = DB::table('users')
->join('offers','users.id','=','offers.professor_id')
->select('offers.*','users.nom')
->orderby('offers.id','DESC')->get();
Then in place of {{$oferta->professor_id}}, replace {{$oferta->nom}}
Your problem should be solved

Laravel 5 Model Relation to Query in blade

Schema User Table
ID|NAME
1 |John
2 |Doe
Schema Financial Table
ID|USER_ID|PROFIT |DATE
1 |1 |1000 |2016-12-22
2 |2 |-2000 |2016-12-22
3 |1 |2000 |2016-12-24
4 |2 |-2000 |2016-12-24
User Model
class User extends Model
{
public function Financial()
{
return $this->hasMany('App\Financial');
}
}
Financial Model
class Financial extends Model
{
public function financial()
{
return $this->belongsTo('App\User');
}
}
My Controller
class MyController extends Controller
{
public function index()
{
$user = User::get();
$financial = Financial::get();
return view('page.index',compact('user','financial'));
}
}
My Blade
#foreach ($user as $u)
<tr>
<td>{{$u->id}}</td>
<td>
{{$u->financial->sum('gross')}}
{{-- Above code doesn't work --}}
{{-- Run something link --}}
{{select (sum(profit) as total) from financial where user_id = $u->id}}
</td>
</tr>
#endforeach
QUESTION
How can i achieve that select from my blade? i'm planning to use #inject('DB','Illuminate\Support\Facades\DB') so i can use DB::raw in my blade, but i'm confused on how to execute the query to achieve the select :(
Solved by using Laravel Collection Method Pluck and Laravel Blade Service Injection.Below is the code i've done to archive what i want:
#inject('financial','App\Financial') {{-- inject before foreach --}}
#foreach ($user as $u)
<tr>
<td>{{$u->id}}</td>
<td>{{$financial->where('user_id','=',$u->id)->get()->pluck('profit')->sum()}}</td>
</tr>
#endforeach
To Evaluate what happen:
$financial->where('user_id','=',$u->id)->pluck('profit')->sum()
// get financial where user_id = $u->id // get the profit // sum the profit
Cheers!
You could have simply done this:
{{$u->financial()->sum('gross')}}
If you are running a query on a relationship, add function brackets while calling it.

Laravel 5.2 Eloquent relationship return Multidimensional Array

this is may route code
Route::get('/dashboard',['middleware' => 'auth', function () {
$links = App\Website_links::where('website_id',1)->get();
return view('user.dashboard',['links_data'=>$links]);
}]);
and website_link model code is
class Website_links extends Model
{
public $timestamps = false;
protected $table = 'website_links';
public function project()
{
return $this->belongsTo('App\Website','website_id')
->select('project_name','project_status');
}
}
and view code
#foreach($links_data as $links)
<tr>
<td>{{$links['page_url']}}</td>
<td>{{$links['project']}}</td>// here i want to display project_name from website table
</tr>
#endforeach
databse schema:
website : id,project_name,project_status
website_links: id, page_url, website_id //here website_id is forign key.
now its shows page_url correctly but
{{$links['project']}} display {"project_name":"Project1","id":45}
and i want to show project_name value like project1 instead of whole array
{"project_name":"Project1","id":45}
Change
$links['project'] to $link->project->project_name
#foreach($links_data as $links)
<tr>
<td>
{{$links['page_url']}}
</td>
<td>
{{$links->project->project_name}}
</td>// here i want to display project_name from website table
</tr>
#endforeach
And change your query to the following to prevent the N+1 query problem
$links = App\Website_links::with('project')->where('website_id',1)->get();

relationship and blade in laravel

I have 3 table as mentioned below.
Table 1(user):
id username password Name Age
Table 2(tasks):
id task_name description
Table 3(logs)
id user_id task_id date hours
Table Relationships:
user has_many logs
task has_many logs
logs belongs_to user
logs belongs_to task
what i am trying to achieve is to get the logs with the user Name, task Name, date and hours.
Controller:
return View::make('log.index')
->with('logs',log::all());
Blade template
#foreach($logs as $log)
<tr>
<td>{{$log->id}}</td>
<td>{{$log->users()->name}}</td>
<td>{{$log->tasks()->name}}</td>
<tr>
#endforeach
but unable to fetch users Name and Tasks name from the respective table. any help is appreciated.
A better way is to define inverse hasMany relation in your Model, as documented here
So in your logs model, probably you need to define:
class Log extends Eloquent {
protected $table = "logs";
public function user(){
return $this->belongsTo('User');
}
public function task(){
return $this->belongsTo('Task');
}
}
Then in your view you can either use :
$log->user()->first()->name
or even better, by using Dynamic Properties:
$log->user->name
$log->users() and $log->tasks() returns a query object. Below, each call returns the result which is the same as calling $log->users()->get() and $log->tasks()->get(). Because the relationships are many to many, you'll need to iterate over $log->users and $log->tasks to retrieve each record.
#foreach($logs as $log)
<tr>
<td>{{$log->id}}</td>
<td>
#foreach($log->users as $user)
{{$user->name}},
#endforeach
</td>
<td>
#foreach($log->tasks as $task)
{{$task->name}},
#endforeach
</td>
<tr>
#endforeach
If you want a specific user/task attached to a log you'll have to build a query.
#foreach($logs as $log)
<tr>
<td>{{$log->id}}</td>
<td>{{$log->users()->where('id', '=', $userID)->first()->name}} </td>
<td>{{$log->tasks()->where('id', '=', $taskID)->first()->name}} </td>
<tr>
#endforeach

Categories