2017-02-27 110 views
1

在我網站的主頁上有一個鏈接「獲取隨機故事」<a href="#" class="btn-get-random-post">Get Random Story</a>,點擊我需要從數據庫中隨機發布並在同一個窗口中顯示。 我使用Laravel 5.4。Laravel 5隨機發帖

class PostsController extends Controller 
{ 

public function index() { 
    return redirect('/'); 
} 

public function show($id) { 
    $post = Post::findOrFail($id); 
    return view('posts.show', compact('post')); 
} 

public function getRandomPost() { 
    $post = Post::inRandomOrder()->first(); 
    return view('posts.show', compact('post')); 
} 
} 

路線

Route::get('posts', '[email protected]'); 
Route::get('posts/create', '[email protected]'); 
Route::get('posts/{id}', '[email protected]'); 
Route::post('posts', '[email protected]'); 
Route::post('publish', '[email protected]'); 
Route::post('delete', '[email protected]'); 
Route::post('get-random-post', '[email protected]'); 

JS

$(document).ready(function() { 
$.ajaxSetup({ 
    headers: { 
     'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') 
    } 
}); 

$('.btn-get-random-post').on('click', function(){ 

    $.ajax({ 
     type: 'post', 
     url: './get-random-post',    
     error: function(jqXHR, textStatus, errorThrown) { 
      console.log(JSON.stringify(jqXHR)); 
      console.log("AJAX error: " + textStatus + ' : ' + errorThrown); 
     } 
    }); 
    return false; 
}); 

}); 

而且我有2個問題在這裏
1.方法getRandomPost()返回崗位,但如何顯示呢?我想要得到結果頁面的網址mysite/post/{id}像來自方法show的網址。
2.是否有任何方式獲取並顯示隨機帖子(與網址mysite/post/{id})沒有AJAX?

UPD
控制器

class PostsController extends Controller 
{ 


public function index() { 
    return redirect('/'); 
} 

public function show($id) { 
    $post = Post::findOrFail($id); 
    return view('posts.show', compact('post')); 
} 

public function getRandomPost() { 
    $post = Post::inRandomOrder()->first(); 
    return redirect()->route('posts.show', ["id" => $post->id]); 
} 
} 

主頁上的鏈接 <a href="{{ action('[email protected]') }}">Random Story</a>

路線

Route::get('/', '[email protected]'); 

Route::get('posts', '[email protected]'); 
Route::get('posts/create', '[email protected]'); 
Route::get('posts/{id}', '[email protected]')->name('posts.show'); 
Route::post('posts', '[email protected]'); 
Route::post('publish', '[email protected]'); 
Route::post('delete', '[email protected]'); 
Route::post('get-random-post', '[email protected]'); 
Route::post('dashboard/delete', '[email protected]'); 
Route::post('dashboard/unpublish', '[email protected]'); 
Route::post('dashboard/restore', '[email protected]'); 

錯誤 enter image description here

+0

該方法不允許錯誤通常指的是您在控制器上使用無效方法的情況,例如使用post調用get方法。看到你的路由定義,情況就是這樣。更改get-random-post爲GET – zedling

回答

1

肯定的是,如果不是在getRandomPost 返回返回視圖重定向響應路線

但首先,

你似乎並沒有把分配的名稱你的路線。

變化Route::get('posts/{id}', '[email protected]');Route::get('posts/{id}', '[email protected]')->name('posts.show');

此外,您可能要指定一個名稱的隨機佈線後也一樣,你就可以生成視圖網址,它

這樣,您就能夠爲它生成路由。

return redirect()->route('posts.show', ["id" => $post->id]);

這一切後,你可以只使用一個鏈路上的路徑,或點擊一個按鈕到路由後重定向頁面:get-random-post

沒有AJAX需要。

+0

這是行不通的,因爲他使用ajax – Jerodev

+0

加載響應,這是他想避免的(AJAX) – zedling

+0

我試過使用你的答案,但在這種情況下,我得到錯誤和重定向不起作用(我更新了我的問題並添加了代碼和屏幕截圖)。我究竟做錯了什麼? – Heidel

1

您也可以通過將url更改爲mysite/post來完全避免使用javascript。如果有一個ID,路線顯示,如果沒有,路由到getRandomPost()方法。

+0

那個問題就是在這種情況下url只會留下'mysite/get-random-post';重定向是必須的 – zedling

+0

對不起,我的意思是評論回覆你的答案。正在增加你最初的說法。 –