Мне нужно использовать переменные из двух контроллеров в одном представлении ..
В то время как вы могли бы использовать GuzzleHTTP , я чувствую, что это случай использования красноречивых один-ко-многим .
Может быть просто моя интерпретация, но у вас есть Пост Модель, которая связана с вашей Коментар Модель, предположительно, post_id
Возможно, что-то подобное может помочь вам!
Модель ваших сообщений
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
// Add this to your model
public function komentars()
{
return $this->hasMany('App\Komentar');
}
}
Модель вашего коментара
use Illuminate\Database\Eloquent\Model;
class Komentar extends Model
{
// Make sure your Komentar model has the post_id
protected $fillable = [
// ..
'post_id',
];
}
Контроллер ваших сообщений
use App\Komentar;
use App\Post;
// To return all of the posts
public function index()
{
$posts = App\Post::all();
return view('show', [
'posts' => $posts
];
}
Ваш веб / маршруты
// Return all posts and associated comments
Route::get('/posts', 'PostsController@index')->name('show.posts');
Show.blade.php
@if( $posts->count() >= 1 )
<!-- Loop through our records here -->
@foreach( $posts as $post )
<p class="alert alert-success">{{ $post->obsah }}</p>
@endforeach
@endif