Este es mi archivo show.blade.php
@extends('layouts.app')
@section('content')
<h1>{{ $user['name'] }}</h1>
<form action="/{{ $user['name'] }}/follow" method="post">
{{ csrf_field() }}
@if(session('success'))
<span class="text-success">{{ session('success') }}</span>
@endif
<button class="btn btn-primary">Follow</button>
</form>
<div class="row">
@foreach($user->messages as $message)
<div class="col-6">
@include('messages.message')
</div>
@endforeach
</div>
@endsection
Este es el codigo de mi controller: UsersController.php
<?php
namespace App\Http\Controllers;
use App\User;
use Illuminate\Http\Request;
class UsersController extends Controller
{
public function show($username)
{
$user = $this->findByUsername($username);
//Aquí estamos haciendo una Query a la base de datos
//Y que solamente me traiga el primer username
return view('users.show', [
'user' => $user,
]);
}
public function follow($username, Request $request)
{
$user = $this->findByUsername($username);
$me = $request->user();
$me->follows()->attach($user);
return redirect("/$username")->withSuccess('Siguiendo usuario');
}
public function follows($username)
{
$user = $this->findByUsername($username);
return view('users.follows', [
'user' => $user,
]);
}
private function findByUsername($username)
{
$user = User::where('username', $username)->first();
//dd($user);
}
}