1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

vue + laravel

Last updated at Posted at 2019-10-04

#axiosを使った通信
viewのaxiosを使用してTestsControllerのcreateメソッドに値を渡す。
またTestsControllerのcreateメソッドからviewに値を返し(res.data)viewで受け取っています。

welcome.blade.php

        axios
			.post('http://localhost/public/test/create', postParam)
			.then((res) => {
				console.log(res.data);
				alert('TestController@createから帰ってきたデータは' + res.data.test);
			}).catch((ex) => {
				console.log('failed');
			});

TestsController

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class TestsController extends Controller
{
    /**
	 * Display a listing of the resource.
	 *
	 * @return \Illuminate\Http\Response
	 */
	public function index()
	{
		Log::info('Showing user profile for user: ');
		return view('welcome');
	}
	/**
	 * Show the form for creating a new resource.
	 *
	 * @return \Illuminate\Http\Response
	 */
	public function create(Request $request)
	{
		Log::info('Showing user profile for user: ');
		$data = $request->all();

		return $data;
	}
}
web.php
Route::get('/test', 'TestsController@index');
Route::get('/test/create', 'TestsController@create');
Route::post('/test/create', 'TestsController@create');

#バリデーション
1.バリデーション定義を作成

StoreBlogPostは適当な名前です。

cmd
php artisan make:request StoreBlogPost

app\Http\Requestsの中に生成される

  • ルールの追加
StoreBlogPost
public function rules()
	{
                // 必須かつ最大文字数10文字
		return [
			'test' => 'required|max:10',
		];
	}

参考資料:https://www.slideshare.net/ssuser817ccb/laravel-bladevuejs

  • laravel mix
  • dockerを使っている場合はlocalとdockerのhttpd.confのdocumentrootは合わせること
    pathが合わずにapp.jsなどが読み込まれないこともある
1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?