我的web.php
文件中有简单的路由组:
Route::group(['prefix' => 'inbox', 'middleware' => 'auth'], function(){
Route::get('/', ['uses' => 'InboxController@inbox',])->name('inbox');
Route::get('/message/{id?}', 'InboxController@message')->name('message');
Route::get('/read/{id?}', 'InboxController@read')->name('read');
});
我有看法:
<a href="{{ route('read'). '/' .$message->id }}">
<div class="letter-icon-title text-default">{{$message->messageSender->name}}</div>
</a>
这里是read()
方法的代码:
public function read(Request $request, $id)
{
$getMessage = Inbox::where('id', $id)->get();
$user_id = Auth::user()->id;
$sender_id = $getMessage->first()->sender;
$recipient_id = $getMessage->first()->recipient;
$seen = $getMessage->first()->seen;
$data = $this->getMessage($id);
return view("layouts/inbox/theme-limitless/message", $data, $this->countMessages());
}
我的方法getMessage()
代码:
public function getMessage($id)
{
$getMessage = Inbox::where('id', $id)->get();
$date = $getMessage->first()->created_at;
$status = $getMessage->first()->seen;
$subject = $getMessage->first()->subject;
$message = $getMessage->first()->message;
$user_id = Auth::user()->id;
$sender_id = $getMessage->first()->sender;
$message_id = $getMessage->first()->id;
$recipient_id = $getMessage->first()->recipient;
$sender = $getMessage->first()->messageSender->name;
$sender_email = $getMessage->first()->messageSender->email;
$recipient = User::where('id', $recipient_id)->first()->name;
$data = [
'id' => $message_id,
'date' => $date,
'sender' => $sender,
'subject' => $subject,
'message' => $message,
'user_id' => $user_id,
'sender_id' => $sender_id,
'recipient' => $recipient,
'recipient_id' => $recipient_id,
'sender_email' => $sender_email,
];
return $data;
}
如果我点击阅读链接,则不打开所需的带有消息的视图,而不是给出错误。只有重定向到后退或当前路线。为什么我无法返回所需的视图并重定向?
您可以看到我的文件here
答案 0 :(得分:0)
尝试将其更改为:
[ 64 3.2 3.2 4 5 6 7 8 64]
确保您已通过身份验证。
答案 1 :(得分:0)
您可以使用{{ action('InboxController@read', $message_id) }}
答案 2 :(得分:0)
您将href atrr传递给错误的网址。使用名称调用路由只是在视图中呈现为完整URL。按照你的方式,它呈现为/read/{id?}/id
。
要通过URL传递ID,您可以通过两种方式进行:
您可以将ID作为以下代码传递:
<a href="{{ route('read', ['id'=>$message->id]) }}">
<div class="letter-icon-title text-default">{{$message->messageSender->name}}</div>
或者您只需编写URL并以URL方式传递:
<a href="url('inbox/read'.'/'.{{$message->id}})">
<div class="letter-icon-title text-default">{{$message->messageSender->name}}</div>
</a>
请阅读以下这篇文章:它反对这篇文章:How to Pass Id in Laravel URL Parameter
{{ url('/inbox/read/', [$message->id]) }}
但是使用相对路径是一个坏主意,因为如果您想要更改URL方案,则需要在视图和控制器中手动查找数十或数百个链接并手动更改它们。在这种情况下,您肯定会忘记更改其中一些内容,并且应用程序的某些页面将被破坏。
一个好的做法是使用链接路由:
{{ route('name.route', ['id' => $message->id]) }}
或指向行动的链接:
{{ action('MyController@show', $message->id) }}
答案 3 :(得分:0)
尝试<a href="{{ url('inbox/read'). '/' .$message->id }}">