Hi Folks,
I am new to laravel and I am stuck at this problem:
I have 3 tables:
User: id, name
Projects: id, name, user_id
Images: id, name, project_id
Using Eloquent I can fetch any projects connected to a user and the images connecting to the project:
public function ProjectShow () {
$user = Auth::user();
$projects = Project::with('images')
->whereHas('user', function ($query) use ($user) {
$query->where('id', '=', $user->id);
})->get();
return view('project-show', get_defined_vars())->with(['user' => $user]);
}
When I "return $projects;" then I will get this result:
{"id":73,"user_id":1,"name":"dfds","projektname":"1527685835","cat_id":1,"beschreibung":null,"youtube":null,"copyright":null,"testimonial":null,"check":0,"ort":null,"stat":0,"created_at":null,"updated_at":null,"deleted_at":null,"images":[{"id":4,"project_id":73,"filename":"15276858427.png","url":"/Users/sl/Developer/awa/public/images/1/1/15276858427.png","filesize":null,"created_at":"2018-05-30 13:10:42","updated_at":"2018-05-30 13:10:42","deleted_at":null},{"id":5,"project_id":73,"filename":"15276858428.png","url":"/Users/sl/Developer/awa/public/images/1/1/15276858428.png","filesize":null,"created_at":"2018-05-30 13:10:42","updated_at":"2018-05-30 13:10:42","deleted_at":null},{"id":6,"project_id":73,"filename":"152768584224Alle.png","url":"/Users/sl/Developer/awa/public/images/1/1/152768584224Alle.png","filesize":null,"created_at":"2018-05-30 13:10:42","updated_at":"2018-05-30 13:10:42","deleted_at":null},
Where id:73 is the ID from projects and id:4 is the connected Images ID
When I remove the "return $projects;" then I am not able to fetch the $projects Variable in Blade:
The Blade:
@extends('layouts.app')
@section('content')
@foreach ($projects as $project)
<p>This is Project Name: {{ $project->Name }}</p>
<p>This is Project FotoURL: {{ $project->url }}</p>
<p>--------</p>
@endforeach
@endsection
Error: No Error, but the variables are empty...
Thank you for any feedback!
Kind Regards,
Stefan