我想将帖子提取到用户的个人资料中

问题描述 投票:0回答:1

我想将帖子提取到用户的个人资料,个人资料已在获取个人简历我是编程和laravel的新手,请帮助

这里是我的控制器:-

public function index($post)
     {

       $post = Post::findOrFail($post);
       return view('profile.index', [
         'post' => $post,
       ]);


     }

这是我的观点:-

@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">
              <button type="button" class="btn btn-success"><a href="/bio-create">.Add Bio</a>  </button>

                <div class="card-header"><h2>{{Auth::user()->username}}</h2></div>
                <div class="card-body">
                    @if (session('status'))
                        <div class="alert alert-success" role="alert">
                            {{ session('status') }}
                        </div>
                    @endif

                    {{$user->profile->bio}}

                </div>
            </div>
        </div>
    </div>
    <div class="form-group row mb-0">
        <div class="col-md-6 offset-md-4">
            <button type="button" class="btn btn-primary"> <a href="/post">Add Info</a>

            </button>
        </div>
    </div>
</div>

<h1> Posts </h1>
@foreach($post as $row)
{{$row->title}}

@endforeach

@endsection

无法弄清楚如何完成@froeach循环

这里是我的路线:-

Route::get('/', function () {
    return view('welcome');
});

Auth::routes();


Route::get('/profile/{user}', 'ProfilesController@index')->name('profile.show');

Route::get('/bio-create', 'ProfilesController@create');
Route::post('/b', 'ProfilesController@store');
Route::get('/post', 'PostsController@create');
Route::post('/p', 'PostsController@store');
Route::get('/profile/{user}/{post}', 'PostsController@index');

最后一条路线是我正在使用的路线我什至不知道这样做是否正确]

请帮助预先感谢

php laravel routing
1个回答
0
投票

您想要的是带有UserPostController方法的ìndex,看起来像这样:

public function index(User $user)
{
    return view('profile.index', [
        'posts' => $user->posts,
    ]);
}

一条看起来像这样的路线:

Route::get('/your-name/{user}/', 'UserPostController@index');

为此,您需要在PostUser之间建立一种关系,其中User最有可能是父级,因此Owning-Side

© www.soinside.com 2019 - 2024. All rights reserved.