Laravel schema sql - php

so simply i want to select user name from table users AS created_by, but after i put this {{ $result->created_by }} in blade tamplate it shows this error "Undefined property: stdClass::$created_by (View: /var/www/hots/resources/views/builds.blade.php)". Please help me.
Schema
public function index() {
$builds = DB::table('Build_Talents')
->join('Builds', 'Builds.BuildID', '=', 'Build_ID')
->join('BuildTalents', 'BuildTalents.TalentsID', '=', 'Talents_ID')
->join('Heroes', 'Builds.hero', '=' ,'Heroes.HeroID')
->join('users', 'Builds.created_by', '=', 'users.id')
->select('Heroes.icon', 'Heroes.name',
DB::RAW('Builds.name AS build', 'users.name AS created_by')
)
->get();
return view('builds', compact('builds'));
}
Blade
#extends('home')
#section('content')
#if($builds)
#foreach($builds as $result)
<div class="col-md-12">
<div class="builds">
<img src="{{ $result->icon }}" alt="Build icon" />
<h2>{{ $result->name }}-{{ $result->build }}</h2>
<i>{{ $result->created_by }}</i>
</div>
</div>
#endforeach
#else
<p>Nic nenalezeno</p>
#endif
#endsection

Instead this:
DB::RAW('Builds.name AS build', 'users.name AS created_by')
use this:
DB::raw('Builds.name AS build, users.name AS created_by')
raw() has only one parameter and you are using two -
Api.

Related

How to get the index of the field from my array properly in Laravel blade?

This is the "show" function in my Controller :
public function show($nform)
{
$plan = PlanFormation::findOrFail($nform);
$plan_props = PlanFormation::select(
'clients.raisoci',
'clients.nrc_entrp',
'intervenants.nom',
'intervenants.prenom')
->join('clients', 'clients.nrc_entrp', '=', 'plan_formations.nrc_e')
->join('intervenants', 'intervenants.id_interv', '=', 'plan_formations.id_inv')
->where('plan_formations.n_form', '=', $nform)
->get();
return view('planformation.detail', ['plan_props' => $plan_props, 'plan' => $plan]);
}
Here is my Blade :
<div class="card-header">
<a class="btn btn-dark btn-sm bu-lg-ic" href="/planformation"><i class="fa fa-arrow-left"></i></a>
<h3 class="card-title card-h3">Plan N° {{ $plan->n_form }} > {{ $plan_props['raisoci'] }}</h3>
</div>
I got "Undefined index 'raisoci' when I use $plan_props['raisoci'] in the blade
In your example $plan_props is not an array with element ['raisoci'] but a collection of items. So you can get the first element of this collection and call its 'raisoci' property:
{{ $plan_props[0]['raisoci'] }}
or
{{ $plan_props[0]->raisoci }}
You may also call first() method instead of get() in your query. In this case you'll get exactly what you expected in your example:
$plan_props = PlanFormation::select(...)
...
->where('plan_formations.n_form', '=', $nform)
->first();
It worked when I used ->first() and I left {{ $plan_props['raisoci'] }} as it's
The third answer is the correct answer
Thank you Manssor for your help

How can I get data from database in laravel?

I want to be able to get data from database.
I need table named role_users, column role_id
and table functions, column function.
Here is my View:
#foreach($contact_users as $contact)
{{ $contact->username }}</h3>
<p>
<strong><i class="icon-real-estate-020 u-line-icon-pro"></i> : </strong>{{ $contact->country }}<br>
<strong><i class="icon-screen-tablet fa-" aria-hidden="true"></i> : </strong>{{ $contact->industry }}<br>
<strong><i class="icon-frame fa-" aria-hidden="true"></i> : </strong>{{ $contact->organization_type }}<br>
#endforeach
and here is my Controller:
$data['contact_users'] = DB::table('contacts')
->join('users' , 'users.id', '=', 'contacts.contact_id')
->join('industries' , 'industries.id', '=', 'users.industry_id')
->join('countries' , 'countries.id', '=', 'users.country_id')
->join('organization_types' , 'organization_types.id', '=', 'users.organization_type_id')
->select('users.*','industries.industry','countries.country','organization_types.organization_type')
->where('contacts.contact_id','!=',$id)
->where('users.deleted_at','=',NULL)
->whereIn('contacts.user_id', $contact_id)
->whereNotIn('contacts.contact_id', $contact_id)
->whereNotIn('contacts.contact_id', $inviter_id)
->groupBy('contact_id')
->take(4)
->get();
I want to add function below organization_type.Thank you.
Why are you using DB query joins?
You need to define your Models first then you can have clearer queries using Eloquent. Please look at the documentation for more.
(https://laravel.com/docs/5.8/eloquent)https://laravel.com/docs/5.8/eloquent

How to get a specific value from a table in a view when you have conflicting names[Laravel]

Im making an application for a non-profit organisation for a school project and we are using laravel, the first time for me
I tried {{$instructiekaart->instructiekaarten.name}} but I get an error message when using this code
Controller
$instructiekaarten = DB::table('instructiekaarten')
->join('instructiekaart_set', 'instructiekaart_set.instructiekaart_id' ,'=', 'instructiekaarten.id')
->join('sets', 'sets.id' ,'=', 'instructiekaart_set.set_id')
->join('instructiekaart_stap', 'instructiekaart_stap.instructiekaart_id' ,'=', 'instructiekaarten.id')
->join('stappen', 'stappen.id' ,'=', 'instructiekaart_stap.stap_id')
->join('instructiekaart_user', 'instructiekaart_user.instructiekaart_id' ,'=', 'instructiekaarten.id')
->join('users', 'users.id' ,'=', 'instructiekaart_user.user_id')
->join('instructiekaart_niveau', 'instructiekaart_niveau.instructiekaart_id' ,'=', 'instructiekaarten.id')
->join('niveaus', 'niveaus.id' ,'=', 'instructiekaart_niveau.niveau_id')
->select('sets.name','instructiekaarten.name', 'stappen.name', 'niveaus.name')
->get();
return view('instructiekaarten.index')->with('instructiekaarten', $instructiekaarten);
View
#if(count($instructiekaarten) > 0)
#foreach($instructiekaarten as $instructiekaart)
<div class="card">
<a href="/instructiekaarten">
<h5 class="card-header">{{$instructiekaart->instructiekaarten.name}}</h5>
</a>
<div class="card-body">
<h5 class="card-title"></h5>
<p class="card-text"></p>
</div>
</div>
#endforeach
#else
<p>Geen instructiekaarten gevonden</p>
#endif
This is the error message I get when using {{$instructiekaart->instructiekaarten.name}}
Undefined property: stdClass::$instructiekaarten (View:
D:\wamp64\www\TZC\resources\views\instructiekaarten\index.blade.php)
Try Giving alias
->select('sets.name AS sets_name','instructiekaarten.name AS instru_name', 'stappen.name AS stappen_name', 'niveaus.name AS niveaus_name')
& then
{{ $instructiekaart->instru_name }}

Laravel 5.1 Order by multiple times

I'm trying to make something 'like facebook'...
Now, I want to order the posts using the created_at table. But also, if the post has a comment, order it by the comments. So actually, the posts need to be ordered by the comment and if the post doesn't have a comment, then it needs to order by the post (acting as a fallback).
To make it all clear, I have made some screenshots:
So this is just one post, nothing to mention about it.
But when I add a second post with 456 as content, that post needs to be on top, wich works:
All good, but now, when I add a comment, the message with the comment needs to be on top (since it has a newer post). This is my problem, it doesn't work:
So here is my code:
Controller:
public function index()
{
$getallposts = DB::table('social_posts')->select('users.*', 'social_posts.created_at as PostAt', 'social_posts.description', 'users_images.*', 'social_posts.id as PostID')
->join('users', 'social_posts.user_id', '=', 'users.id')
->join('users_images', 'social_posts.user_id', '=', 'users_images.user_id')
->orderBy('social_posts.created_at', 'DESC')
->whereNull('social_posts.deleted_at')
->get();
//var_dump($getallposts);
$getallcomments = DB::table('social_comments')->select('users.*', 'social_comments.created_at as PostAt', 'social_comments.description', 'social_comments.post_id', 'users_images.*')
->join('users', 'social_comments.user_id', '=', 'users.id')
->join('users_images', 'social_comments.user_id', '=', 'users_images.user_id')
->orderBy('social_comments.created_at', 'ASC')
->whereNull('social_comments.deleted_at')
->get();
//var_dump($getallposts);
$getrandomusers = DB::table('users')->select('users.*', 'users.id as UID', 'users_images.*')
->join('users_images', 'users.id', '=', 'users_images.user_id')
->orderByRaw('RAND()')
->where('users.id', '!=', Auth::id())
->take(16)
->get();
return view('dashboard', ['posts' => $getallposts, 'comments' => $getallcomments, 'randomuser' => $getrandomusers]);
}
View:
#foreach($posts as $post)
<div class="row row-sm">
<div class="col-sm-12">
<div class="card">
<div class="card-heading">
<a href class="pull-left w-32 m-r" href="{!! url(Auth::user()->slug) !!}">
<img src="{!! asset('avatars/'.$post->image) !!}" class="w-full img-circle">
</a>
<div class="clear">
{!! ucwords($post->firstname) !!} {!! ucwords($post->lastname) !!}
<div class="text-xxs font-thin text-muted">{!! \Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $post->PostAt)->diffForHumans() !!}</div>
</div>
</div>
<div class="card-body">
<p>
{!! nl2br(e($post->description)) !!}
</p>
<p style="color:grey;font-size:10px;">Aantal likes - {!! \Cussion\SocialReaction::where('post_id', $post->PostID)->count() !!} {!! (\Cussion\SocialReaction::where('post_id', $post->PostID)->count() == 1) ? 'reactie' : 'reacties' !!} </p>
<p style="font-size:14px;">Leuk vinden</p> <!-- KNOP OM STATUS TE LIKEN -->
</div>
<div class="list-group no-radius no-border" style="background-color:#F5F5F5;">
#foreach($comments as $comment)
#if($comment->post_id == $post->PostID)
<div class="md-list-item">
<div class="md-list-item-left">
<img src="{!! asset('avatars/'.$comment->image) !!}" class="w-full circle">
</div>
<div class="md-list-item-content">
<small class="font-thin">{!! ucwords($comment->firstname) !!} {!! ucwords($comment->lastname) !!}</small>
<div class="text-xxs font-thin text-muted" style="font-size:12px;">{!! nl2br(e($comment->description)) !!}</div>
<div class="text-xxs font-thin text-muted" style="font-size:10px;">{!! \Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $comment->PostAt)->diffForHumans() !!}</div>
</div>
</div>
#endif
#endforeach
<div class="md-list-item">
<form action="{!! url('/dashboard') !!}" method="post" role="form">
{!! csrf_field() !!}
<div class="input-group">
<input type="text" class="form-control" name="message" placeholder="Wat wil je reageren?">
<input type="hidden" name="post_id" value="{!! $post->PostID !!}">
<span class="input-group-btn">
<button class="btn btn-default" type="submit">Reageer</button>
</span>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
#endforeach
And my database structure:
Demo can be found on http://cussion.org
You can easily use the concept of "touching parent timestamps", read more about it here https://laravel.com/docs/5.1/eloquent-relationships#touching-parent-timestamps
But here is a simple explanation, since the comments are related to posts, you can instruct the comments model to update the updated_at field of the parent post whenever a comment is added or even edited, so now you have the post's updated_at always up-to-date.
I would defer the sorting to your PHP script. Writing that kind of sorting algorithm in MySQL could get very complicated and hard to read. Instead, I would store the results in a collection and then sort using a custom function. Something like this:
$getallposts = DB::table('social_posts')
->select('users.*', 'social_posts.created_at as PostAt', 'social_posts.description', 'users_images.*', 'social_posts.id as PostID')
->join('users', 'social_posts.user_id', '=', 'users.id')
->join('users_images', 'social_posts.user_id', '=', 'users_images.user_id')
->whereNull('social_posts.deleted_at')
->get();
$getallcomments = DB::table('social_comments')
->select('users.*', 'social_comments.created_at as PostAt', 'social_comments.description', 'social_comments.post_id', 'users_images.*')
->join('users', 'social_comments.user_id', '=', 'users.id')
->join('users_images', 'social_comments.user_id', '=', 'users_images.user_id')
->orderBy('social_comments.created_at', 'ASC')
->whereNull('social_comments.deleted_at')
->get();
//add comments to their posts and put post
foreach($getallposts as $post){
$post->comments = [];
foreach($getallcomments as $comment){
if($comment->post_id == $post->PostID){
$post->comments[] = $comment;
}
}
}
$getallposts->sortBy(function($item, $key){
//run your sorting logic here based on comments and created_at date
});
Even better yet, convert your sql queries to Eloquent objects and this gets MUCH MUCH easier.

Laravel get profile name

I'm laravel newbie and I'm making simple CMS. So i have simple question:
I wan't when click on avatar - redirect to user profile (http://example.com/profiles/nickname/id)
In DB it saves that:
as you see author_id I have, now I need to get author name from users table:
And then generate url: http://example.com/profiles/Evaldas/2 (Evaldas, because author_id is 2 in topics table)
My routes file:
Route::get('topic/{tname}/{tid}', 'viewTopic#showTopic');
My viewTopic.php Controller:
<?php
namespace App\Http\Controllers;
use DB;
use View;
class viewTopic extends Controller
{
public function showTopic($tname, $tid)
{
return View::make('posts', [
'topics' => DB::table('topics')
->where('id', $tid)
->where('seo_title', $tname)
->first(),
'posts' => DB::table('posts')
->where('topic_id', $tid)
->select()
->get()
]);
}
}
And layout:
#extends('layouts.main')
#section('content')
<div class="media">
<div class="media-left">
<a href="HERE MUST BE HREF TO PROFILE">
<img class="media-object" src="http://localhost/uploads/avatars/2.jpg" style="width: 64px">
</a>
</div>
<div class="media-body" rel="#author{{ $topics->author_id }}">
<h4 class="media-heading">{{ $topics->title }}</h4>
#if(!empty($topics->text))
{{ $topics->text }}
#else
Message empty :(
#endif
</div>
#foreach($posts as $post)
<div class="media">
<div class="media-left">
<a href="HERE MUST BE HREF TO PROFILE">
<img class="media-object" src="http://localhost/uploads/avatars/1.png" style="width: 64px">
</a>
</div>
<div class="media-body" rel="#post{{ $post->pid }}">
{{ $post->text }}
</div>
</div>
#endforeach
</div>
#stop
Thanks so much in advance :)
If you want to use the Query Builder, you can use the join() method:
DB::table('posts')
->where('topic_id', $tid)
->join('users', 'user.id', '=', 'posts.author_id')
->select()
->get()
This way, the user information will be available:
$post->nickname
You can then build your URL using a laravel helper, for exemple, if you have a profile route:
<a href="{{route('profile', ['nickname' => $post->nickname, 'id' => $post->author_id]);}}">
An other method is to create models for your tables and define relationship between them.
See this: http://laravel.com/docs/5.1/eloquent-relationships
Using this method, you will be able to write things like $post->author->nickname

Categories