How to retrieve data from multiple tables in Laravel 5.4 - php

I have two tables and i would like to retrieve data from them and pass it to my table.
For this I've created 2 models with an one to one relationship:
[Adress]
class Adress extends Model
{
public function KontoKorrent()
{
return $this->hasOne(KontoKorrent::class, 'Adresse');
}
}
[KontoKorrent]
class KontoKorrent extends Model
{
public function Adresse()
{
return $this->belongsTo(Adress::class,'Adresse');
}
}
My controller look like this:
class AdressesController extends Controller
{
public function index()
{
$adresses = Adress::with('KontoKorrent')->paginate(2);
return view('welcome', compact('adresses'));
}
}
When I use tinker
App\Adress::
Every adress has relation to the kontokorrent. This is working.
App\Adress {#698
Adresse: "3030",
Anrede: "Company",
Name1: "A Company Name",
LieferStrasse: "Dummystreet",
KontoKorrent: App\KontoKorrent {#704
Location: "1",
Adresse: "3030",
Kto: "S0043722",
In my view:
<ul>
#foreach($adresses as $adress)
<li>{{ $adress->Name1 }}</li> //this is working
<li>{{ $adress->KontoKorrent->Kto }}</li> //this is NOT working
#endforeach
</ul>
{{ $adresses->links() }}
The relation is showing me an error:
Trying to get property of non-object
What I'm doing wrong ?

The error that you are getting:
Trying to get property of non-object
Is related to some Adress model that doesn't have a KontoKorrent, then your $adress->KontoKorrent returns null, and null isn't a object, that the reason of the message.
To fix it, you should do an if to check if adress have the relationship:
<ul>
#foreach($adresses as $adress)
<li>{{ $adress->Name1 }}</li> //this is working
<li>
#if($adress->KontoKorrent)
{{ $adress->KontoKorrent->Kto }}
#else
<!-- Put something here if you want, otherwise remove the #else -->
#endif
</li> //this is NOT working
#endforeach
</ul>
This can be shortened to:
{{ $adress->KontoKorrent ? $adress->KontoKorrent : 'the else content' }}
or in PHP >= 7.0, you can use the null coalesce operator:
{{ $adress->KontoKorrent ?? 'the else content' }}

Related

Undefined variable Issue with laravel blade

In my laravel application, I'm having a list of regions in my DB
This is my controller method
public function index()
{
$region_options = Region::get();
return view('home',compact('region_options'));
}
and I have following in my blade,
#if(!$region_options->isEmpty())
<ol class="region-pop">
#foreach ($region_options as $key => $region)
<li>{{ $region->name }}</li>
#endforeach
</ol>
#endif
But when I run this it gave me an error saying, Undefined variable: region_options
I'm struggling to find what the issue is...

Pass data to view in Laravel

I am new to Laravel I just want to pass variable to view but every time I have got Undefined variable: companies error
this is my Controller:
public function index()
{
$companies = Company::all();
return view(
'companies.index', [
'companies' => $companies
]);
}
and this is my view:
<ul class="list-group">
#foreach($companies as $company)
<li class="list-group-item">{{ $company->name }}</li>
#endforeach
</ul>
I use Laravel version 5.5
Make sure in your route file on routes/web.php you have a route to the index function like so:
Route::get('urlToYourView', 'YourController#index');

simplify this #foreach/if in blade and laravel

I am fairly new to laravel (L5 specifically) and I am making my own version of a todo app rather than following one of the tutorials out there. I've learned quite a bit so far but the way I have this piece of code currently laid out in my blade template makes me think their might be a simpler way of doing this.
My TodosController#index fn is
public function index()
{
$todos = Todo::get();
return view('todos', compact('todos'));
}
App\Todo extends an Eloquent model which makes data handling very easy!
My route is:
Route::bind('todos', function($slug)
{
return App\Todo::whereSlug($slug)->first();
});
So my page simply displays an unorded list of "todos". I want to have two separate lists. One that is for completed todos and one for incomplete. My blade template looks like this so far and looks a bit messy. Also I am looping over the results twice which is where I think I can improve on.
<h3>Incomplete</h3>
<ul>
#foreach ($todos as $todo)
#if ($todo->completed == 'No')
<li>
{{ $todo->title }}
</li>
#endif
#endforeach
</ul>
<h3>Complete</h3>
<ul>
#foreach ($todos as $todo)
#if ($todo->completed == 'Yes')
<li>
{{ $todo->title }}
</li>
#endif
#endforeach
</ul>
Any suggestions to simplify that blade template?
DRY your code out. You could streamline it by moving the actual item mark-up to a partial template since it’s repeated in both the complete and incomplete lists:
<h3>Incomplete</h3>
<ul>
#foreach ($todos as $todo)
#if ($todo->completed == 'No')
#include('partials.items.todo')
#endif
#endforeach
</ul>
<h3>Complete</h3>
<ul>
#foreach ($todos as $todo)
#if ($todo->completed == 'Yes')
#include('partials.items.todo')
#endif
#endforeach
</ul>
And partials.items.todo would look like this:
<li>
{{ $todo->title }}
</li>
I would also re-factor your loops. Instead of looping over the same list twice, you could split them in your controller:
public function index()
{
$todos = Todo::where('user_id', '=', Auth::id())->get();
$complete = $todos->filter(function ($item) {
return $item->completed = 'Yes';
});
$incomplete = $todos->filter(function ($item) {
return $item->completed = 'No';
});
return view('todos', compact('complete', 'incomplete'));
}
Looking at your Todo model, I’d also make your completed column in the database a boolean field instead of a column containing “Yes” or “No” strings. You could then cast that column value to a proper boolean (since MySQL doesn’t have a native boolean field type):
class Todo extends Model
{
protected $casts = [
'completed' => 'boolean',
];
public function isComplete()
{
return $this->completed;
}
}
And then re-factor your controller action to use this instead:
public function index()
{
$todos = Todo::where('user_id', '=', Auth::id())->get();
$complete = $todos->filter(function ($item) {
return $item->isComplete() === true;
});
$incomplete = $todos->filter(function ($item) {
return $item->isComplete() === false;
});
return view('todos', compact('complete', 'incomplete'));
}
You could even move those collection filters to a custom TodoCollection class:
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
class TodoCollection extends EloquentCollection
{
public function complete()
{
return $this->filter(function ($item) {
return $item->isComplete() === true;
});
}
public function incomplete()
{
return $this->filter(function ($item) {
return $item->isComplete() === false;
});
}
}
Sorry for the lengthy reply, but should give you food for though on how to re-factor your code.
Only a bit simplified but...
You can try in your controller:
public function index()
{
$completed = Todo::where('completed','Yes')->get();
$incompleted = Todo::where('completed','No')->get();
return view('todos', compact('completed', 'incompleted'));
}
in Your template:
<h3>Incomplete</h3>
<ul>
#foreach ($incompleted as $todo)
<li>
{{ $todo->title }}
</li>
#endforeach
</ul>
<h3>Complete</h3>
<ul>
#foreach ($completed as $todo)
<li>
{{ $todo->title }}
</li>
#endforeach
</ul>
Another approach using a subtemplate like this:
//_list_todos.blade.php
#foreach ($todos as $todo)
<li>
{{ $todo->title }}
</li>
#endforeach
And your main template like this:
<h3>Incomplete</h3>
<ul>
#include('_list_todos',['todos'=>$incompleted] )
</ul>
<h3>Complete</h3>
<ul>
#include('_list_todos',['todos'=>$completed] )
</ul>
The advantege to use a subtemplate like the last one is you can reuse the code, and simplify your main templates.

Having trouble creating a Like system for comments in a Laravel Project

I'm creating a project using Laravel. Users are able to like comments. I want to display a "like" button so a user can like a comment and if the user has already liked the comment I want that button to be "unlike" so a user can unlike the liked comment
In my database I have a likes table:
| id | user_id | comment_id |
My Like Model looks like this:
class Like extends \Eloquent {
protected $fillable = ['user_id', 'comment_id'];
protected $table = 'likes';
public function owner()
{
return $this->belongsTo('Acme\Users\User', 'user_id');
}
}
Comment Model looks like this:
class Comment extends \Eloquent {
protected $fillable = ['user_id', 'post_id', 'body'];
protected $table = 'comments';
public function owner()
{
return $this->belongsTo('Acme\Users\User', 'user_id');
}
public function likes()
{
return $this->hasMany('Acme\Likes\Like');
}
}
User model:
class User extends Eloquent {
public function comments()
{
return $this->hasMany('Acme\Comments\Comment');
}
public function likes()
{
return $this->hasMany('Acme\Likes\Like');
}
}
Likes controller :
class LikesController extends \BaseController {
use CommanderTrait;
/**
* Like a comment
* #return Response
*/
public function commentLike()
{
// using a command bus. Basically making a post to the likes table assigning user_id and comment_id then redirect back
extract(Input::only('user_id', 'comment_id'));
$this->execute(new CommentLikeCommand($user_id, $comment_id));
return Redirect::back();
}
public function unlike()
{
$like = new Like;
$user = Auth::user();
$id = Input::only('comment_id');
$like->where('user_id', $user->id)->where('comment_id', $id)->first()->delete();
return Redirect::back();
}
}
In my view I'm able to get the comments via $comment, and I'm able to get likes via $comment->like have:
#foreach($post->comments as $comment)
<div class="user-comment">
<p class="comment">
{{ $comment->owner->first_name }} {{ $comment->owner->last_name }} {{ $comment->body }}
</p>
<div class="com-details">
<!-- how long ago the comment was posted -->
<div class="com-time-container">
{{ $comment->created_at->diffForHumans() }} ·
</div>
<!-- HERE IS WHERE I WANT THE LIKE AND UNLIKE BUTTONS TO DISPLAY -->
#if ($comment->likes->owner->id === $currentUser->id)
{{ Form::open(['route' => 'like']) }}
{{ Form::hidden('user_id', $currentUser->id) }}
{{ Form::hidden('comment_id', $comment->id) }}
<button type="submit" class="com-like">Like</button>
{{ Form::close() }}
#else
{{ Form::open(['route' => 'unlike']) }}
{{ Form::hidden('user_id', $currentUser->id) }}
{{ Form::hidden('comment_id', $comment->id) }}
<button type="submit" class="com-like">Unlike</button>
{{ Form::close() }}
#endif
<!-- how many users like this comment -->
<span class="likes"> · {{ $comment->likes->count() }}</span>
</div>
</div><!--user-comment end-->
#endforeach
Im trying to set up an if statement to see if the current user has liked the status but im not sure how this is done? If the user has not liked the comment yet I want the "like" button to display. If the user has liked the comment I want the "unlike" button to display. I thought I could say #if($comment->likes->owner->id === $currentUser->id) but I get Undefined property. How would I go about doing this?
$comment->likes is a Collection of Like objects. To access the owner property, you would need to iterate the collection.
However, another option is to use the available methods on the Collection to do what you need:
#if (in_array($currentUser->id, $comment->likes->lists('user_id')))
$comment->likes->lists('user_id') will return an array of all the user_id values in the Collection of Likes for the Comment. in_array() will check if the $currentUser->id is in that array.
I haven't worked much with Laravel, so my syntax and terminology might be a bit off, but it should be the right idea.
Basically, $comment->likes is a list of likes on that comment. You need to iterate through those likes and check if one of them is by the current user. If one of them is by the current user, then show the unlike button. Otherwise, show the like button.
Not sure what that'd look like in Blade, but here's some pseudo-code:
$currentUserLiked = false;
// go through all of the comment's likes
foreach ($comment->likes as $like)
{
// check if this like is by the current user
if ($like->owner->id == $currentUser->id)
{
$currentUserLiked = true;
break;
}
}
if ($currentUserLiked)
{
showUnlikeButton();
}
else
{
showLikeButton();
}

"Use of undefined constant..." error in Laravel

I am a Laravel newbie. I want to pass the results of a database query to a view. I get an error message "Use of undefined constant tasks - assumed 'tasks'". What am I doing wrong?
My code is as follows:
class TasksController extends BaseController{
public function index(){
$tasks = Task::all();
//return View::make(tasks.index, ['tasks' => $tasks]);
return View::make(tasks.index, compact('tasks'));
}
A snippet from my template page is shown below:
<body>
<h1>All tasks!</h1>
#foreach($tasks as $task)
<li>{{ $task-title }} </li>
#endforeach
return View::make('tasks.index')->with(compact('tasks'));
also change:
<li>{{ $task-title }} </li>
to
<li>{{ $task->title }} </li>
should be like this.
Try this,
return View::make(tasks.index, $tasks);
instead of
return View::make(tasks.index, compact('tasks'));

Categories