Missing required parameter for [Route: conversations.show] [URI: conversations{user}] [Missing parameter: user].
I dont know how to solve this problem bc its only when i sent a message when i get back i have no problems and the message is there.
ConversationsController.php
<?php
namespace AppHttpControllers;
use IlluminateHttpRequest;
use AppModelsUser;
use IlluminateSupportFacadesAuth;
use AppRepositoryConversationsRepository;
use IlluminateAuthAuthManager;
use vendorlaravelframeworksrcIlluminateDatabaseEloquentBuilder;
class ConversationsController extends Controller
{
/**
* @var ConversationRepository
*/
private $r;
/**
* @var AuthManager
*/
private $auth;
public function __construct(ConversationsRepository $conversationRepository, AuthManager $auth){
$this->r = $conversationRepository;
$this->auth = $auth;
}
public function index (){
return view('conversations/index', [
'users' => $this->r->getConversations($this->auth->user()->id)
]);
}
public function show (User $user){
return view('conversations/show', [
'users' => $this->r->getConversations($this->auth->user()->id),
'user' => $user,
'messages' => $this->r->getMessagesFor($this->auth->user()->id, $user->id)->get()->reverse()
]);
}
public function store (User $user, Request $request){
$this->r->createMessage(
$request->get('content'),
$this->auth->user()->id,
$user->id
);
return redirect(route('conversations.show', ['id'=> $user->id]));
}
}
ConversationsRepository.php
<?php
namespace AppRepository;
use AppModelsUser;
use AppModelsMessage;
use IlluminateDatabaseEloquentBuilder;
use CarbonCarbon;
class ConversationsRepository{
/**
* @var User
*/
private $user;
/**
* @var Message
*/
private $message;
public function __construct(User $user, Message $message){
$this->user =$user;
$this->message =$message;
}
public function getConversations(int $userId){
return $this->user->newQuery()
->select('name', 'id')
->where('id', '!=', $userId)
->get();
}
public function createMessage(string $content, int $from, int $to){
return $this->message->newQuery()->create([
'content' => $content,
'seller_id' => $from,
'buyer_id' => $to,
'created_at' => Carbon::now()
]);
}
public function getMessagesFor(int $from, int $to): Builder{
return $this->message->newQuery()
->whereRaw("((buyer_id = $from AND seller_id = $to) OR (buyer_id = $to AND seller_id = $from))")
->orderBy('created_at', 'DESC');
}
}
show.blade.php
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row">
@foreach($users as $user)
<div class="list-group">
<a class="list-group-item" href="{{ route('conversations.show', $user->id )}}">{{$user->name}}</a>
@endforeach
<div class="col-md-9">
<div class="card">
<div class="card-header">{{ $user->name }}
<div class="card-body conversations">
@foreach($messages as $message)
<div class="row">
<div class="col-md-10 {{ $message->from->id !== $user->id ?'offset-md-2 text-right' : ''}}">
<p>
<strong>{{($message->from->id !== $user->id ? 'Moi' : $message->from->name) }}</strong><br>
{{ $message->content }}
</p>
</div>
</div>
@endforeach
<form action="" method="post">
@csrf
<div class="from-group">
<textarea name="content" placeholder="Ecrivez votre message" class="form-control"></textarea>
</div>
<button class="btn btn-primary" type="submit">Envoyer</button>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
@endsection
I try to have a page where you have yours conversations but when i sent message a error happen
3
Answers
In show.blade.php
this should do it.
Instead of passing the ID pass the whole object.
The error is mostly in the routes folder as everything provided seems fine.
So you have to provide it please
but i think you are using somthing like
instead of :
Please check either your defined route has a parameter or the function you are pointing to, required a parameter.
For example if you are using language/ locale