Laravel 5 系列入门教程(四)【最适合中国人的 Laravel 教程】【完结】

2015-3-11   /   字数:14658   /   阅读数:94871   /   分类: Laravel     

本教程示例代码见:https://github.com/johnlui/Learn-Laravel-5  

大家在任何地方卡住,最快捷的解决方式就是去看我的示例代码。

本文是本系列教程的完结篇,我们将一起给 Page 加入评论功能,让游客在前台页面可以查看、提交、回复评论,同时我们将在后台完善评论管理功能,可以删除、编辑评论。Page 和评论将使用 Eloquent 提供的“一对多关系”。最终,我们将得到一个个人博客系统的雏形,并布置一个大作业,供大家实战练习。

1. 初识 Eloquent

Laravel Eloquent ORM 是 Laravel 中非常重要的部分,也是 Laravel 能如此流行的原因之一。中文文档在:

1. http://laravel-china.org/docs/5.0/eloquent

2. http://www.golaravel.com/laravel/docs/5.0/eloquent/

在前面的教程中已经建立好的 learnlaravel5/app/Page.php 就是一个 Eloquent Model 类:

<?php namespace App;

use Illuminate\Database\Eloquent\Model;

class Page extends Model {

	//

}

若想进一步了解 Eloquent,推荐阅读系列文章:深入理解 Laravel Eloquent

2. 创建 Comment 模型

首先我们要新建一张表来存储 Comment,命令行运行:

php artisan make:model Comment

成功以后,修改 migration 文件 learnlaravel5/database/migrations/***_create_comments_table.php 的相应位置为:

Schema::create('comments', function(Blueprint $table)
{
	$table->increments('id');
	$table->string('nickname');
	$table->string('email')->nullable();
	$table->string('website')->nullable();
	$table->text('content')->nullable();
	$table->integer('page_id');
	$table->timestamps();
});

之后运行:

php artisan migrate

去数据库里瞧瞧,comments 表已经躺在那儿啦。

3. 建立“一对多关系”

修改 Page 模型:

<?php namespace App;

use Illuminate\Database\Eloquent\Model;

class Page extends Model {

  public function hasManyComments()
  {
    return $this->hasMany('App\Comment', 'page_id', 'id');
  }

}

搞定啦~ Eloquent 中模型间关系就是这么简单。

模型间关系中文文档:http://laravel-china.org/docs/5.0/eloquent#relationships

扩展阅读:深入理解 Laravel Eloquent(三)——模型间关系(关联)

4. 前台提交功能

修改 Comment 模型:

<?php namespace App;

use Illuminate\Database\Eloquent\Model;

class Comment extends Model {

  protected $fillable = ['nickname', 'email', 'website', 'content', 'page_id'];

}

增加一行路由:

Route::post('comment/store', 'CommentsController@store');

运行以下命令创建 CommentsController 控制器:

php artisan make:controller CommentsController

修改 CommentsController:

<?php namespace App\Http\Controllers;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

use Redirect, Input;

use App\Comment;

class CommentsController extends Controller {

	public function store()
	{
		if (Comment::create(Input::all())) {
			return Redirect::back();
		} else {
			return Redirect::back()->withInput()->withErrors('评论发表失败!');
		}

	}

}

修改视图 learnlaravel5/resources/views/pages/show.blade.php:

@extends('_layouts.default')

@section('content')
  <h4>
    <a href="/">⬅️返回首页</a>
  </h4>

  <h1 style="text-align: center; margin-top: 50px;">{{ $page->title }}</h1>
  <hr>
  <div id="date" style="text-align: right;">
    {{ $page->updated_at }}
  </div>
  <div id="content" style="padding: 50px;">
    <p>
      {{ $page->body }}
    </p>
  </div>
  <div id="comments" style="margin-bottom: 100px;">

    @if (count($errors) > 0)
      <div class="alert alert-danger">
        <strong>Whoops!</strong> There were some problems with your input.<br><br>
        <ul>
          @foreach ($errors->all() as $error)
            <li>{{ $error }}</li>
          @endforeach
        </ul>
      </div>
    @endif

    <div id="new">
      <form action="{{ URL('comment/store') }}" method="POST">
        <input type="hidden" name="_token" value="{{ csrf_token() }}">
        <input type="hidden" name="page_id" value="{{ $page->id }}">
        <div class="form-group">
          <label>Nickname</label>
          <input type="text" name="nickname" class="form-control" style="width: 300px;" required="required">
        </div>
        <div class="form-group">
          <label>Email address</label>
          <input type="email" name="email" class="form-control" style="width: 300px;">
        </div>
        <div class="form-group">
          <label>Home page</label>
          <input type="text" name="website" class="form-control" style="width: 300px;">
        </div>
        <div class="form-group">
          <label>Content</label>
          <textarea name="content" id="newFormContent" class="form-control" rows="10" required="required"></textarea>
        </div>
        <button type="submit" class="btn btn-lg btn-success col-lg-12">Submit</button>
      </form>
    </div>

<script>
function reply(a) {
  var nickname = a.parentNode.parentNode.firstChild.nextSibling.getAttribute('data');
  var textArea = document.getElementById('newFormContent');
  textArea.innerHTML = '@'+nickname+' ';
}
</script>

    <div class="conmments" style="margin-top: 100px;">
      @foreach ($page->hasManyComments as $comment)

        <div class="one" style="border-top: solid 20px #efefef; padding: 5px 20px;">
          <div class="nickname" data="{{ $comment->nickname }}">
          @if ($comment->website)
            <a href="{{ $comment->website }}">
              <h3>{{ $comment->nickname }}</h3>
            </a>
          @else
            <h3>{{ $comment->nickname }}</h3>
          @endif
            <h6>{{ $comment->created_at }}</h6>
          </div>
          <div class="content">
            <p style="padding: 20px;">
              {{ $comment->content }}
            </p>
          </div>
          <div class="reply" style="text-align: right; padding: 5px;">
            <a href="#new" onclick="reply(this);">回复</a>
          </div>
        </div>

      @endforeach
    </div>
  </div>
@endsection

前台评论功能完成。

查看效果:

Image


Image

5. 后台管理功能

修改基础视图 learnlaravel5/resources/views/app.blade.php 为:

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="utf-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title>Laravel</title>

	<link href="/css/app.css" rel="stylesheet">

	<!-- Fonts -->
  <link href='http://fonts.useso.com/css?family=Roboto:400,300' rel='stylesheet' type='text/css'>
</head>
<body>
	<nav class="navbar navbar-default">
		<div class="container-fluid">
			<div class="navbar-header">
				<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
					<span class="sr-only">Toggle Navigation</span>
					<span class="icon-bar"></span>
					<span class="icon-bar"></span>
					<span class="icon-bar"></span>
				</button>
				<a class="navbar-brand" href="#">Learn Laravel 5</a>
			</div>

			<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
				<ul class="nav navbar-nav">
					<li><a href="/admin">后台首页</a></li>
				</ul>
				<ul class="nav navbar-nav">
					<li><a href="/admin/comments">管理评论</a></li>
				</ul>

				<ul class="nav navbar-nav navbar-right">
					@if (Auth::guest())
						<li><a href="/auth/login">Login</a></li>
						<li><a href="/auth/register">Register</a></li>
					@else
						<li class="dropdown">
							<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">{{ Auth::user()->name }} <span class="caret"></span></a>
							<ul class="dropdown-menu" role="menu">
								<li><a href="/auth/logout">Logout</a></li>
							</ul>
						</li>
					@endif
				</ul>
			</div>
		</div>
	</nav>

	@yield('content')

	<!-- Scripts -->
	<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
	<script src="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.1/js/bootstrap.min.js"></script>
</body>
</html>

修改后台路由组(增加了一行):

Route::group(['prefix' => 'admin', 'namespace' => 'Admin', 'middleware' => 'auth'], function()
{
  Route::get('/', 'AdminHomeController@index');
  Route::resource('pages', 'PagesController');
  Route::resource('comments', 'CommentsController');
});

创建 Admin\CommentsController :

php artisan make:controller Admin/CommentsController

Admin/CommentsController 要有 查看所有、查看单个、POST更改、删除四个接口:

<?php namespace App\Http\Controllers\Admin;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

use App\Comment;

use Redirect, Input;

class CommentsController extends Controller {

	public function index()
	{
		return view('admin.comments.index')->withComments(Comment::all());
	}

	public function edit($id)
	{
		return view('admin.comments.edit')->withComment(Comment::find($id));
	}

	public function update(Request $request, $id)
	{
		$this->validate($request, [
			'nickname' => 'required',
			'content' => 'required',
		]);
		if (Comment::where('id', $id)->update(Input::except(['_method', '_token']))) {
			return Redirect::to('admin/comments');
		} else {
			return Redirect::back()->withInput()->withErrors('更新失败!');
		}
	}

	public function destroy($id)
	{
		$comment = Comment::find($id);
		$comment->delete();

		return Redirect::to('admin/comments');
	}

}

接下来创建两个视图:

learnlaravel5/resources/views/admin/comments/index.blade.php:

@extends('app')

@section('content')
<div class="container">
  <div class="row">
    <div class="col-md-10 col-md-offset-1">
      <div class="panel panel-default">
        <div class="panel-heading">管理评论</div>

        <div class="panel-body">

        <table class="table table-striped">
          <tr class="row">
            <th class="col-lg-4">Content</th>
            <th class="col-lg-2">User</th>
            <th class="col-lg-4">Page</th>
            <th class="col-lg-1">编辑</th>
            <th class="col-lg-1">删除</th>
          </tr>
          @foreach ($comments as $comment)
            <tr class="row">
              <td class="col-lg-6">
                {{ $comment->content }}
              </td>
              <td class="col-lg-2">
                @if ($comment->website)
                  <a href="{{ $comment->website }}">
                    <h4>{{ $comment->nickname }}</h4>
                  </a>
                @else
                  <h3>{{ $comment->nickname }}</h3>
                @endif
                {{ $comment->email }}
              </td>
              <td class="col-lg-4">
                <a href="{{ URL('pages/'.$comment->page_id) }}" target="_blank">
                  {{ App\Page::find($comment->page_id)->title }}
                </a>
              </td>
              <td class="col-lg-1">
                <a href="{{ URL('admin/comments/'.$comment->id.'/edit') }}" class="btn btn-success">编辑</a>
              </td>
              <td class="col-lg-1">
                <form action="{{ URL('admin/comments/'.$comment->id) }}" method="POST" style="display: inline;">
                  <input name="_method" type="hidden" value="DELETE">
                  <input type="hidden" name="_token" value="{{ csrf_token() }}">
                  <button type="submit" class="btn btn-danger">删除</button>
                </form>
              </td>
            </tr>
          @endforeach
        </table>


        </div>
      </div>
    </div>
  </div>
</div>
@endsection

learnlaravel5/resources/views/admin/comments/edit.blade.php:

@extends('app')

@section('content')
<div class="container">
  <div class="row">
    <div class="col-md-10 col-md-offset-1">
      <div class="panel panel-default">
        <div class="panel-heading">编辑评论</div>

        <div class="panel-body">

          @if (count($errors) > 0)
            <div class="alert alert-danger">
              <strong>Whoops!</strong> There were some problems with your input.<br><br>
              <ul>
                @foreach ($errors->all() as $error)
                  <li>{{ $error }}</li>
                @endforeach
              </ul>
            </div>
          @endif

          <form action="{{ URL('admin/comments/'.$comment->id) }}" method="POST">
            <input name="_method" type="hidden" value="PUT">
            <input type="hidden" name="_token" value="{{ csrf_token() }}">
            <input type="hidden" name="page_id" value="{{ $comment->page_id }}">
            Nickname: <input type="text" name="nickname" class="form-control" required="required" value="{{ $comment->nickname }}">
            <br>
            Email:
            <input type="text" name="email" class="form-control" required="required" value="{{ $comment->email }}">
            <br>
            Website:
            <input type="text" name="website" class="form-control" required="required" value="{{ $comment->website }}">
            <br>
            Content:
            <textarea name="content" rows="10" class="form-control" required="required">{{ $comment->content }}</textarea>
            <br>
            <button class="btn btn-lg btn-info">提交修改</button>
          </form>

        </div>
      </div>
    </div>
  </div>
</div>
@endsection

后台管理功能完成,查看效果:

Image


Image

6. 大作业

依赖于 Page 的评论功能已经全部完成,个人博客系统雏形诞生。在本系列教程的最后,布置一个大作业:构建出 Article 的前后台,并且加上 Article 与 Comment 的一对多关系,加入评论和评论管理功能。在做这个大作业的过程中,你将会反复地回头去看前面的教程,反复地阅读中文文档,会仔细阅读我的代码,等你完成大作业的时候,Laravel 5 就真正入门啦~~


教程(四)代码快照:https://github.com/johnlui/Learn-Laravel-5/archive/tutorial_4.zip


Laravel 5 系列入门教程【最适合中国人的 Laravel 教程】到此结束,谢谢大家!

WRITTEN BY

avatar

评论:

harthali
2017-08-07 10:20
可以 非常有用
幸运儿
2017-03-17 17:55
你好,我们在使用laravel自带的登录注册的时候,登录注册后,它是直接跳转到home路由的。可是我们争产过的逻辑应该是跳转到登录之前的页面才是啊。很多教材都没有解决这个问题,也都忽略了这个问题。我的思路是在 return view('auth.login')之前给它一个session记录跳转到这个页面之前的路由,登录之后,跳转到这个路由。可是我测试不通。好像中间有什么过程会把我保存的这个session清除掉。请问您有什么好的办法解决吗?谢谢
hhhh
2017-03-12 18:43
教程可以
what!!!!
2016-10-11 17:35
老是救命啊  为什么我 “后台首页”和“管理评论”的两个连接点击后URL中没有index.php导致找不到地址无法显示。。路由器中如何修改呢
岁寒大宝宝
2016-07-22 18:12
岁寒你好,我想深入学习laravel,但是看了几天源码,发现根本摸不着头脑啊,请问你有好的建议吗?
蓝海
2016-05-26 13:32
大神,你好,我在写前端评论时也遇到了这个问题
Whoops, looks like something went wrong.

2/2
ErrorException in d70faa19897442d1f6c5b0fe0fe9864dbf3a4e7d.php line 39:
Invalid argument supplied for foreach()

查看前台评论时说foreach 的参数无效。。但是存在数据库里了啊。。
蓝海
2016-05-25 10:20
大神,请问if (Comment::create(Input::all()))是什么意思啊?
xl
2016-07-15 19:45
@蓝海:看看模型ORM可以知道
chrysalis
2016-05-19 21:42
请问控制器的头部引入的 use Redirect, Input; 这些叫什么?如果没有引入这两个的话用什么来代替redirect?
我知道input可以用request->input()来代替。
drummer30
2016-04-27 17:50
Whoops, looks like something went wrong.

2/2
ErrorException in 9fcc595248053b91450538bd5aa0cdfe line 64:
Invalid argument supplied for foreach() (View: D:\WWW\laravel3\resources\views\pages\show.blade.php)

大神,我是跟着你那个这个:
                                                 前台评论功能完成。
                                                 查看效果:
这打开页面后报错说foreach 的参数无效,前面的步骤都对,我没找到原因
drummer30
2016-04-27 19:05
@drummer30:找到原因了
蓝海
2016-05-25 11:19
@drummer30:求问什么原因??
gray code
2016-04-13 15:18
非常感谢楼主的贡献! 受益匪浅
Carrie
2016-03-20 16:09
Class 'input' not found
之前一直都是把改为request了,但是Input::except(), Input::all()很好用的样子,查了下
发现5.2的版本需要在config/app.php手动添加
'Input' => Illuminate\Support\Facades\Input::class,

然后 use Input;
OpenHair
2016-03-17 16:17
我靠 楼主好厉害   求带。
跟这脚步把些入门都搞完了。 哈哈
xue
2016-03-05 12:22
为什么没办法注册账号呢

错误信息
Sorry, the page you are looking for could not be found.

1/1
NotFoundHttpException in RouteCollection.php line 145:
in RouteCollection.php line 145
at RouteCollection->match(object(Request)) in Router.php line 719
at Router->findRoute(object(Request)) in Router.php line 642
at Router->dispatchToRoute(object(Request)) in Router.php line 618
at Router->dispatch(object(Request)) in Kernel.php line 210
wyb007
2016-04-01 15:06
@xue:你的resources/views中没有register的代码吧?
key
2017-07-13 10:47
@wyb007:如何添加register的代码。。。
xue
2016-03-05 11:22
前台列表也无法进入详情页面,是什么原因
Whoops, looks like something went wrong.

1/1
ReflectionException in Container.php line 776:
Class App\Http\Controllers\PagesController does not exist
in Container.php line 776
at ReflectionClass->__construct('App\Http\Controllers\PagesController') in Container.php line 776
at Container->build('App\Http\Controllers\PagesController', array()) in Container.php line 656
at Container->make('App\Http\Controllers\PagesController', array()) in Application.php line 613
at Application->make('App\Http\Controllers\PagesController') in ControllerDispatcher.php line 83
at ControllerDispatcher->makeController('App\Http\Controllers\PagesController') in ControllerDispatcher.php line 54
at ControllerDispatcher->dispatch(object(Route), object(Request), 'App\Http\Controllers\PagesController', 'show') in Route.php line 198
at Route->runWithCustomDispatcher(object(Request)) in Route.php line 131
at Route->run(object(Request)) in Router.php line 691
at Router->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 141
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 101
at Pipeline->then(object(Closure)) in Router.php line 693
at Router->runRouteWithinStack(object(Route), object(Request)) in Router.php line 660
at Router->dispatchToRoute(object(Request)) in Router.php line 618
at Router->dispatch(object(Request)) in Kernel.php line 210
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 141
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in VerifyCsrfToken.php line 43
at VerifyCsrfToken->handle(object(Request), object(Closure)) in VerifyCsrfToken.php line 17
at VerifyCsrfToken->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in ShareErrorsFromSession.php line 55
at ShareErrorsFromSession->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in StartSession.php line 61
at StartSession->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 36
at AddQueuedCookiesToResponse->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in EncryptCookies.php line 40
at EncryptCookies->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in CheckForMaintenanceMode.php line 42
at CheckForMaintenanceMode->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 101
at Pipeline->then(object(Closure)) in Kernel.php line 111
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 84
at Kernel->handle(object(Request)) in index.php line 53
JohnLui
2016-03-05 11:23
@xue:Class App\Http\Controllers\PagesController does not exist
xue
2016-03-05 11:36
@JohnLui:,好了。不过提交评论以后。数据库只保存了content和时间,没有姓名。这是怎么回事
yankeys
2016-06-30 18:33
@xue:白名单。。。。。我也曾经马虎过,,哈哈哈

2016-02-24 10:28
错误信息:
QueryException in Connection.php line 620:
SQLSTATE[42S22]: Column not found: 1054 Unknown column '/admin/comments/1' in 'field list' (SQL: update `comments` set `page_id` = 12, `nickname` = 旺, `email` = 497580157@qq.com, `website` = www.wang.com, `content` = 这文章很不错111, `/admin/comments/1` = , `updated_at` = 2016-02-24 10:27:55 where `id` = 1)

代码:
public function update(Request $request, $id)
    {
        $this->validate($request, [
            'nickname'    => 'required',
            'content'    => 'required',
        ]);

        if(Comment::where('id', $id)->update(Input::except(['_method', '_token']))){
            return Redirect::to('admin/comments');
        } else {
            return Redirect::back()->withInput()->withErrors('更新失败!');
        }
    }


也不知道是哪里错了,复制楼主你的代码到我那也不行,求解
JohnLui
2016-02-24 10:32
@旺:Input::all() 里多了一个键叫“/admin/comments/1”,值为空,非常奇怪。

2016-02-24 10:41
@JohnLui:这是我打印出来的$_POST数组
array(7) {
  ["_method"]=>
  string(3) "PUT"
  ["_token"]=>
  string(40) "9483YWH5bLgGQ3soae7aUcFHaG5mss5bKP6jtquL"
  ["page_id"]=>
  string(2) "12"
  ["nickname"]=>
  string(3) "旺"
  ["email"]=>
  string(16) "497580157@qq.com"
  ["website"]=>
  string(12) "www.wang.com"
  ["content"]=>
  string(21) "这文章很不错111"
}

这是我的html代码,都没有呀,555555555
<form action="{{ URL('admin/comments/'.$comment->id) }}" method="POST">
                        <input name="_method" type="hidden" value="PUT">
                        <input type="hidden" name="_token" value="{{ csrf_token() }}">
                        <input type="hidden" name="page_id" value="{{ $comment->page_id }}">
                        Nickname: <input type="text" name="nickname" class="form-control" required="required" value="{{ $comment->nickname }}">
                        <br>
                        Email:<input type="text" name="email" class="form-control" required="required" value="{{ $comment->email }}">
                        <br>
                        Website:<input type="text" name="website" class="form-control" required="required" value="{{ $comment->website }}">
                        <br>
                        Content:<textarea name="content" rows="10" class="form-control" required="required">{{ $comment->content }}</textarea>
                        <br>
                        <button class="btn btn-lg btn-info">提交修改</button>

                    </form>

2016-02-24 10:44
@JohnLui:楼主,我打印$_GET
array(1) {
  ["/admin/comments/1"]=>
  string(0) ""
}

有这个,但是我按照你的写,为啥下面的语句会把$_GET的也加到更新语句里,难道是我nginx里加了个重写导致的?我隐藏了index.php,把所有的参数都重写到index.php?$1里了,可能是这里,下面的语句有没有办法改成只把$_POST的数组添加进更新语句,麻烦求解
if(Comment::where('id', $id)->update(Input::except(['_method', '_token']))){
            return Redirect::to('admin/comments');
        } else {
            return Redirect::back()->withInput()->withErrors('更新失败!');
        }

2016-02-24 11:07
@旺:楼主问题已解决了,但是我还是要说出来一下,嘿嘿
找了下官方的laravel配置,
Nginx

在 Nginx,在你的网站配置中增加下面的配置,可以使用「优雅链接」:

location / {
    try_files $uri $uri/ /index.php?$query_string;
}

然后在我的nginx配置中加了这个,就没有问题了,谢谢楼主的查阅

2016-07-15 14:51
@旺:我遇到的问题和你一样,也像你说的那样改,但是还是没有解决!怎么办?
酒窝
2016-02-02 21:10
谢谢撸主,通俗易懂
csdc
2016-01-12 22:40
厉害厉害
雪域
2016-01-05 18:20
头大了一下午了,我想重写它的登录注册系统,一开始很顺利,没有用model,直接在control里写的db查询,但是session怎么也写不进去,用了无数种方法,然后跟着教程来了一遍,发现它的compiled文件里生成session都成功了,然后我的编辑器就报很多错,然后这个文件居然有17162行,这正常吗?然后我写在新建博客的控制器里写了插入session的语句,但是session文件显示的更新时间变了,但我的session变量还是没有存进去,难受!
willdoge
2015-12-28 15:42
大神 请教个问题 一直没有明白呢~为什么在更新数据库失败后返回前页还带着withInput()?
JohnLui
2015-12-28 23:53
@willdoge:把用户输入的数据带回去,让他们直接修改而不需要重新输入。
alex
2015-12-27 16:21
我解决样式问题了,是因为我的default.blade.php界面用的是自己找的ui代码,没有加载成功css文件导致的
alex
2015-12-27 16:17
我按照博主的代码写完以后,样式很难看。后来把博主代码复制过来,样式还是没改变
newhand
2016-01-03 23:00
@alex:有一个样式是googleapis的网址的,好像是谷歌的。。。国内没上墙访问不了的。。可能是这个原因吧
Zenas
2016-05-15 16:27
@newhand:我的问题也是样式,但是是后台显示评论那部分,我的 评论内容一旦过长,将超出设定的区域,
我知道出错的代码应该是这个位置 index.blade.php里
@foreach ($comments as $comment)
            <tr class="row">
              <td class="col-lg-4">
                {{ $comment->content }}
              </td>
              <td class="col-lg-2">
                @if ($comment->website)
                  <a href="{{ $comment->website }}">
                    <h4>{{ $comment->nickname }}</h4>
                  </a>
                @else
                  <h3>{{ $comment->nickname }}</h3>
                @endif
                {{ $comment->email }}
              </td>
              <td class="col-lg-4">
                <a href="{{ URL('pages/'.$comment->page_id) }}" target="_blank">
                  {{ App\Page::find($comment->page_id)->title }}
                </a>
              </td>
              <td class="col-lg-1">
                <a href="{{ URL('admin/comments/'.$comment->id.'/edit') }}" class="btn btn-success">编辑</a>
              </td>
              <td class="col-lg-1">
                <form action="{{ URL('admin/comments/'.$comment->id) }}" method="POST" style="display: inline;">
                  <input name="_method" type="hidden" value="DELETE">
                  <input type="hidden" name="_token" value="{{ csrf_token() }}">
                  <button type="submit" class="btn btn-danger">删除</button>
                </form>
              </td>
            </tr>
          @endforeach


这段代码,就是第一个commens那个部分
kgoing
2016-02-17 15:20
@alex:将default.blade.php中的app.css保险起见,写成绝对路径就可以了。
haoxuan
2015-12-23 13:51
博主是否可以加上权限这块呢,就是类型rbac的
Echo
2015-12-11 15:28
PO主的后台首页是什么样的,唯独后台首页我这里出错说文件不存在,评论管理可以。
风起
2015-12-09 14:07
感谢博主,看哇受益匪浅。
NewMen
2015-11-27 12:09
看完了 第一个接触的框架 有些蒙圈,谢谢博主,享受了福利,留个足迹
NewMen1
2015-12-21 14:08
@NewMen:哈哈
anzichen
2015-11-20 11:12
老师好,为什么老师最后一步可以直接进入后台管理功能,但是我的却要输入用户名和密码,我试过在表中插入一行用户,但是提示记录不匹配
wangszh
2015-11-14 13:12
你好,老师,按照你的教程走了一遍,感觉很有收获,想完成老师的作业,创建article的前后台,在后台文章列表页面想显示user->name 就根据老师的评论管理改了一个 App\User::find($article->user_id)->name 结果报错,想问下老师改怎么写?
JohnLui
2015-11-14 13:16
@wangszh:报的是什么错你倒是贴一下呀。。。
wangszh
2015-11-16 10:45
@JohnLui:周末没看到老师的回复,错误提示 :ErrorException in 4320b28ce042c0b07a2bd5f5b75672d5 line 25:
Trying to get property of non-object (View: C:\wamp\www\learnlaravel5\resources\views\admin\articles\index.blade.php)
JohnLui
2015-11-16 10:53
@wangszh:所以错误很明显了呀,自己排查吧。
wangszh
2015-11-16 10:57
@JohnLui:希望老师给看一下 ,这个写法有没有问题?
wangszh
2015-11-16 11:16
@JohnLui:或者是 想把user_id显示为user-name应该怎么写 ? 麻烦老师了
wangszh
2015-11-16 15:42
@JohnLui:App\User::find($article->user_id)->name 这样写之后发现sql语句没有传入id ,string 'select * from `users` where `users`.`id` = ? limit 1' (length=52) 麻烦老师帮帮忙提点一句,刚开始学习,很多东西不懂
黄传备
2015-12-02 18:23
@wangszh:怎样,弄好了吗,我这也是同样的问题
xiaohan
2015-10-24 20:59
老师,你好,我在用户密码重设出了个问题,在没登录时可以访问路由http://localhost:8000/password/email,也显示了password视图。
但是 我登录再访问就跳转到/home,并报错NotFoundHttpException in RouteCollection.php line 161
这是怎么回事?
JohnLui
2015-10-25 00:26
@xiaohan:这是正常的
xiaohan
2015-10-25 12:21
@JohnLui:是这样的,我现在已经登录了,去点修改密码的链接(或者直接访问路由),然后它直接跳转到/home了,而且报错,理论上应该是要会打开http://localhost:8000/password/email 并且显示auth/password.blade.php视图。而当我没有登录用户的时候,直接访问http://localhost:8000/password/email,却可以正常显示出上面的视图。
xiaohan
2015-10-25 20:47
@xiaohan:我在Stack Overflow上查到的方法是说要为登录的用户单独创建路由和控制器
From the ResetsPasswords trait, the answer is:

Password::sendResetLink(['email' => Auth::user()->email], function (Illuminate\Mail\Message $message) {
    $message->subject('Your Password Reset Link');
});
但是我不清楚具体该怎么做,希望老师指导一下。
wangwt
2015-11-27 10:07
@xiaohan:是不是自带的PasswordController使用了guest中间件的原因?
ttt
2015-10-17 16:14
受不了了,这部教程做完了 怎么管理首页还是老样子?
莫莫莫
2015-10-14 20:23
请教老师,为什么我edit修改提交的时候,会报下面的这个错误,,create就没有
MethodNotAllowedHttpException in compiled.php line 7717:
in compiled.php line 7717
at RouteCollection->methodNotAllowed(array('GET', 'HEAD', 'PUT', 'PATCH', 'DELETE')) in compiled.php line 7713
at RouteCollection->getRouteForMethods(object(Request), array('GET', 'HEAD', 'PUT', 'PATCH', 'DELETE')) in compiled.php line 7691
at RouteCollection->match(object(Request)) in compiled.php line 6965
at Router->findRoute(object(Request)) in compiled.php line 6937
at Router->dispatchToRoute(object(Request)) in compiled.php line 6929
at Router->dispatch(object(Request)) in compiled.php line 1935
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in compiled.php line 8952
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in compiled.php line 2438
at VerifyCsrfToken->handle(object(Request), object(Closure)) in VerifyCsrfToken.php line 17
at VerifyCsrfToken->handle(object(Request), object(Closure)) in compiled.php line 8944
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in compiled.php line 12083
at ShareErrorsFromSession->handle(object(Request), object(Closure)) in compiled.php line 8944
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in compiled.php line 10785
at StartSession->handle(object(Request), object(Closure)) in compiled.php line 8944
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in compiled.php line 11789
at AddQueuedCookiesToResponse->handle(object(Request), object(Closure)) in compiled.php line 8944
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in compiled.php line 11738
at EncryptCookies->handle(object(Request), object(Closure)) in compiled.php line 8944
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in compiled.php line 2478
at CheckForMaintenanceMode->handle(object(Request), object(Closure)) in compiled.php line 8944
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in compiled.php line 8935
at Pipeline->then(object(Closure)) in compiled.php line 1891
at Kernel->sendRequestThroughRouter(object(Request)) in compiled.php line 1880
at Kernel->handle(object(Request)) in index.php line 53
at require_once('/Users/kevin/learnlaravel5/public/index.php') in server.php line 21
sjjcarrie
2015-10-09 19:22
return Redirect::back()->withInput()->withErrors('保存失败!');
这个保存失败为什么没有在页面显示
asc
2015-10-09 14:54
很好的一个教程,感谢博主
otme
2015-10-05 12:48
讲的很棒!准备做作业!
wign
2015-11-02 10:37
@otme:很棒,赞
BingoTrai
2015-09-27 12:17
博主,能讲下auth的实现吗?
authcontroller就一个构造函数
public function __construct(Guard $auth, Registrar $registrar)
    {
        $this->auth = $auth;
        $this->registrar = $registrar;

        $this->middleware('guest', ['except' => 'getLogout']);
    }
不知道他是怎么实现的。
JohnLui
2015-09-27 13:08
@BingoTrai:功能代码都在 use AuthenticatesAndRegistersUsers; 里。这是 PHP 的新特性 Traits:http://php.net/manual/zh/language.oop5.traits.php
hi_tp
2015-09-24 15:20
什么时候出点进阶的.队列那些看得懵懵懂懂的.
ZellDincht
2015-09-11 14:27
老师请问,您的例子中如果要实现分页,怎么写呢?

看了手册没有实现出来,希望老师帮忙!

        public function index()
    {
        return view('home')->withPages(Page::orderBy('created_at', 'desc')->get());
    }

需要use哪个命名空间呢?
ZellDincht
2015-09-11 15:01
@ZellDincht:我谷歌了解决了,但是是这样子的:
use DB;
....

        public function index()
    {    
        $pages = DB::table('pages')->orderBy('created_at', 'desc')->simplePaginate(20);
        return view('home')->withPages($pages);
    }

但是我还是想知道,利用App\Page 这个模型实例,如何进行分页呢?
JohnLui
2015-09-11 16:33
@ZellDincht:仔细读一下中文文档的 分页 篇,上面写的很清楚。
ZellDincht
2015-09-11 17:03
@JohnLui:谢谢博主,我的确看得不仔细。

        use App\Page;

        public function index()
    {
        $pages = Page::orderBy('id', 'desc')->paginate(15);
        return view('admin.index')->withPages($pages);
    }
ZellDincht
2015-09-10 17:16
有一个问题

<form action="{{ URL('admin/comments/'.$comment->id) }}" method="post">
       <input type="hidden" name="_method" value="PUT">
       <input type="hidden" name="_token" value="{{ csrf_token() }}">
       <input type="hidden" name="page_id" value="{{ $comment->page_id }}">
       Nickname: <input type="text" name="nickname" class="form-control" required="required" value="{{ $comment->nickname }}">
       <br/>
       Email: <input type="text" name="email" class="form-control" value="{{ $comment->email }}">
       <br>
       Website: <input type="text" name="website" class="form-control" value="{{ $comment->website }}">
       <br>
       <textarea name="content" rows="10" class="form-control">{{ $comment->content }}</textarea>
       <br>
       <button class="btn btn-lg btn-info">提交修改</button>
</form>

这个表单中,隐藏域的 _method = "PUT" 有什么作用啊?
JohnLui
2015-09-11 11:35
@ZellDincht:Laravel 用 POST 接口实现所有非 GET 方法。
ZellDincht
2015-09-11 14:29
@JohnLui:谢谢老师。

您的例子我练了几遍了,渐渐有了感觉。

因为laravel很强大,所以感觉很多还不懂,需要学习
ZellDincht
2015-09-10 17:14
这里学到了很多知识,谢谢博主。
张小尘
2015-08-30 19:35
大神
laravel_初学者
2015-08-26 17:24
博主,请问下多个数据库查询结果要怎么传给视图呀
flower
2015-08-24 14:16
laravel4 中 workbench的独立包,怎么在laravel5中,使用
在laravel5中怎么创建独立包
dafa
2015-08-11 17:11
已经完成!发现还是存在一些问题的。比如没有分页,默认选择的php版本为 5.4 ,在编辑器中不会自动use 不存在的类。

2015-08-10 14:42
博主你好,我用larael自带的 $password=Hash::check('123456');加密的字符串插入数据库,登陆的时候总是提示密码错误,在研究发现,同一个明码123456,加密后的字符串总是不同的,请问有什么好的解决方案吗
试试
2015-10-16 11:14
@陈:试试 Hash::make
Snails
2015-08-10 13:23
两个问题:
楼主,你好。.请问(1)如何加载静态css资源呢?(2)为什么我的返回主页直接会跳到/var/www/下
胖子
2015-08-31 15:19
@Snails:<link href="{{ URL('/css/app.css') }}" rel="stylesheet">
我是这样加载CSS样式的
你应该把里面所有  href="XXXX"的样式改为上面的那种
jokee
2016-03-31 20:00
@胖子:确实可以了,多谢
demon_cry
2015-08-08 17:55
为什么model不专门放到一个目录中呢,如果代码稍微多点就会很乱吧?
gux
2015-08-06 20:47
非常感谢您的教程,提个小小的建议,手机下的代码样式不换行,看不全,方便的话能不能改一下,谢谢大神的教程
陈祥
2015-08-05 10:31
请问老师,controller目录下的commentscontroller和controller/admin目录下的commentscontroller有什么区别呢?可以合并到一起吗?
xp
2015-08-18 22:35
@陈祥:前台控制器和后台控制器呗
菜鸟
2015-07-07 17:15
我想写一个立即购买的方法,不知怎么写,还请大神赐教
flyfish
2015-06-20 00:15
page跟artical 有啥区别么????
zerdon
2015-06-29 16:13
@flyfish:page是单页,article 是文章
ZellDincht
2015-09-10 16:57
@zerdon:单页和文章有什么区别?
flyfish
2015-06-19 23:02
<a href="/">⬅️返回首页</a>改为<a href="{{ URL() }}">⬅️返回首页</a>  这里面的图标不知道为什么不显示出来 有什么特殊的字体文件么?
laucie
2015-06-12 13:39
不错啊 最近在学laravel 请问大神 PHP的学习路线是什么样的啊
Xander
2015-06-07 22:29
非常好,跟着走了一遍,都成功了。。回头再吧作业搞下,
初学者
2015-06-02 15:33
谢谢博主~受益良多
初学者
2015-05-28 19:14
public function destroy($id)
这个方法 还剩多条记录的时候可以调用到。
最后一条记录删除的时候 调用不到。也没报错 ,就是数据没删掉
york
2015-05-27 15:56
为啥我 的数据都有了就是样式没有,全部左边显示了
JohnLui
2015-05-28 14:57
@york:静态资源没载入呗
flyfish
2015-06-21 17:51
@york:路径不对 要加URL()
trey
2015-05-19 09:37
laravel 的 artisan 命令行 我可以单独安装嘛?  

在别的框架利使用这个。
JohnLui
2015-05-19 11:00
@trey:Artisan 是跟 Laravel 强耦合的代码生成工具,顶部的类引入,甚至路径都是写死的,不能用于其他框架。
laravel初学者_
2015-05-18 15:06
大神您好!照着您的步骤出现了
NotFoundHttpException in compiled.php line 7793:


    in compiled.php line 7793
    at RouteCollection->match(object(Request)) in compiled.php line 7066
    at Router->findRoute(object(Request)) in compiled.php line 7038
    at Router->dispatchToRoute(object(Request)) in compiled.php line 7030
    at Router->dispatch(object(Request)) in compiled.php line 1989
    at Kernel->Illuminate\Foundation\Http\{closure}(object(Request))
    at call_user_func(object(Closure), object(Request)) in compiled.php line 9067
    at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in compiled.php line 2492
    at VerifyCsrfToken->handle(object(Request), object(Closure)) in VerifyCsrfToken.php line 17
    at VerifyCsrfToken->handle(object(Request), object(Closure)) in compiled.php line 9059
    at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in compiled.php line 12216
    at ShareErrorsFromSession->handle(object(Request), object(Closure)) in compiled.php line 9059
    at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in compiled.php line 10917
    at StartSession->handle(object(Request), object(Closure)) in compiled.php line 9059
    at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in compiled.php line 11922
    at AddQueuedCookiesToResponse->handle(object(Request), object(Closure)) in compiled.php line 9059
    at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in compiled.php line 11871
    at EncryptCookies->handle(object(Request), object(Closure)) in compiled.php line 9059
    at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in compiled.php line 2532
    at CheckForMaintenanceMode->handle(object(Request), object(Closure)) in compiled.php line 9059
    at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
    at call_user_func(object(Closure), object(Request)) in compiled.php line 9050
    at Pipeline->then(object(Closure)) in compiled.php line 1945
    at Kernel->sendRequestThroughRouter(object(Request)) in compiled.php line 1932
    at Kernel->handle(object(Request)) in index.php line 53

这个是什么原因 都对齐了的啊
laravel初学者_
2015-05-19 09:33
@laravel初学者_:已经弄好了 谢谢
wyl206
2015-08-13 10:01
@laravel初学者_:遇到了同样的问题,怎么弄好的,求教?
wyl206
2015-08-25 11:37
@wyl206:已清楚,说明没有相关的文件或者路由,找摸下路由就知道了。
求救3
2015-05-17 15:09
大神,出现中文乱码如何解决,谢谢。
yzero
2015-07-15 20:11
@求救3:你好。你的问题直接用notepad++进行格式编码转为utf8就可以了
dunkbird
2015-05-14 18:07
前台评论提交的时候,也就是调用CommentsController的store方法时出,token的错误,怎么回事啊?
MassAssignmentException in compiled.php line 9345:
_token
in compiled.php line 9345
at Model->fill(array('_token' => 'eY0JuuLshjeNYFiC41nUAX03JfdK18d62cc6IV1g', 'page_id' => '9', 'nickname' => 'sdfas', 'email' => 'sfasd@ibm.com', 'website' => 'dfasdf', 'content' => 'sadfasdf')) in compiled.php line 9275
at Model->__construct(array('_token' => 'eY0JuuLshjeNYFiC41nUAX03JfdK18d62cc6IV1g', 'page_id' => '9', 'nickname' => 'sdfas', 'email' => 'sfasd@ibm.com', 'website' => 'dfasdf', 'content' => 'sadfasdf')) in compiled.php line 9393
at Model::create(array('_token' => 'eY0JuuLshjeNYFiC41nUAX03JfdK18d62cc6IV1g', 'page_id' => '9', 'nickname' => 'sdfas', 'email' => 'sfasd@ibm.com', 'website' => 'dfasdf', 'content' => 'sadfasdf')) in CommentsController.php line 42
at CommentsController->store()
at call_user_func_array(array(object(CommentsController), 'store'), array()) in compiled.php line 8287
at Controller->callAction('store', array()) in compiled.php line 8354
at ControllerDispatcher->call(object(CommentsController), object(Route), 'store') in compiled.php line 8333
at ControllerDispatcher->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in compiled.php line 8952
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in compiled.php line 8935
at Pipeline->then(object(Closure)) in compiled.php line 8334
at ControllerDispatcher->callWithinStack(object(CommentsController), object(Route), object(Request), 'store') in compiled.php line 8320
at ControllerDispatcher->dispatch(object(Route), object(Request), 'App\Http\Controllers\CommentsController', 'store') in compiled.php line 7317
at Route->runWithCustomDispatcher(object(Request)) in compiled.php line 7288
at Route->run(object(Request)) in compiled.php line 6954
at Router->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in compiled.php line 8952
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in compiled.php line 8935
at Pipeline->then(object(Closure)) in compiled.php line 6955
at Router->runRouteWithinStack(object(Route), object(Request)) in compiled.php line 6944
at Router->dispatchToRoute(object(Request)) in compiled.php line 6929
at Router->dispatch(object(Request)) in compiled.php line 1935
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in compiled.php line 8952
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in compiled.php line 2438
at VerifyCsrfToken->handle(object(Request), object(Closure)) in VerifyCsrfToken.php line 17
at VerifyCsrfToken->handle(object(Request), object(Closure)) in compiled.php line 8944
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in compiled.php line 12083
at ShareErrorsFromSession->handle(object(Request), object(Closure)) in compiled.php line 8944
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in compiled.php line 10785
at StartSession->handle(object(Request), object(Closure)) in compiled.php line 8944
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in compiled.php line 11789
at AddQueuedCookiesToResponse->handle(object(Request), object(Closure)) in compiled.php line 8944
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in compiled.php line 11738
at EncryptCookies->handle(object(Request), object(Closure)) in compiled.php line 8944
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in compiled.php line 2478
at CheckForMaintenanceMode->handle(object(Request), object(Closure)) in compiled.php line 8944
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in compiled.php line 8935
at Pipeline->then(object(Closure)) in compiled.php line 1891
at Kernel->sendRequestThroughRouter(object(Request)) in compiled.php line 1880
at Kernel->handle(object(Request)) in index.php line 53
dunkbird
2015-05-14 18:33
@dunkbird:还是没有解决,等站长的出现了。
Num116
2015-06-04 09:13
@dunkbird:是App\Comment.php里的代码打错了,之前我也碰到这个,$fillable打错了出现这个错误,找了好久
dunkbird
2015-05-14 16:39

超赞的文章,感谢!!!
博主的这个网站是用的哪个东西做出来的,是开源的么?
记得有个类似的这样的网站来着,可否告诉我名字?
JohnLui
2015-05-14 16:48
@dunkbird: 底下写的有 Powered by Emlog
dunkbird
2015-05-14 18:35
@JohnLui:多谢!
看看,我也把我的个人主页改成你说的这个。
ding
2015-05-14 11:03
额,又遇到一个问题,需要帮助。
关于article的comment,我想用一个comment表来管理page和article中的所有comment。我在comment表中添加了一栏叫article_id.
目前的状态是,当添加page的comment,一切正常,article_id会自动赋给0;
当添加article的comment的时候有问题,我前台添加评论代码如下,但是添加的comment的page_id和article_id都是0;但我在前台调试过,确定name='article_id'栏的value确实取到了article的id,但是就是没有存进数据库。

<div id="new">
      <form action="{{ URL('comment/store') }}" method="POST">
        <input type="hidden" name="_token" value="{{ csrf_token() }}">
        <input type="hidden" name="article_id" value="{{ $article->id }}">
        <div class="form-group">
          <label>Nickname</label>
          <input type="text" name="nickname" class="form-control" style="width: 300px;" required="required">
        </div>
        <div class="form-group">
          <label>Email address</label>
          <input type="email" name="email" class="form-control" style="width: 300px;">
        </div>
        <div class="form-group">
          <label>Home article</label>
          <input type="text" name="website" class="form-control" style="width: 300px;">
        </div>
        <div class="form-group">
          <label>Content</label>
          <textarea name="content" id="newFormContent" class="form-control" rows="10" required="required"></textarea>
        </div>
        <button type="submit" class="btn btn-lg btn-success col-lg-12">Submit</button>
      </form>
    </div>
JohnLui
2015-05-14 11:19
@ding: 后端存进数据库也要手动操作呀,做了吗
ding
2015-05-14 11:54
@JohnLui:额,找到原因了,现在正常了。
我看了你commentcontroller里面的store函数,看到没有一项项的针对column特别处理,还以为只要前端name和value对应到数据库里去了。

现在发现,comment.php里面要设 protected $fillable = ['nickname', 'email', 'website', 'content', 'page_id', 'article_id'];
JohnLui
2015-05-14 11:58
@ding:牛逼
ding
2015-05-13 12:41
前几个月有一次想学laravel,对着文档和英文教程摸索了很长时间,还是没有懂,不了了之。
今天两个小时看完了博主四篇文章,不敢说都吃透了,最起码laravel的工作原理,前后台的关系算是理顺了。
只有一个问题想问一下博主,里面的那么多的函数等等都是从哪里查的?大多数应该是基于Eloquent吧,但是还有一些,例如Auth::guest()这种函数,我可能心里知道有这个需求,需要这么个函数,但是去哪里查找这个函数呢?
JohnLui
2015-05-13 13:22
@ding:函数可以到 api 网站查询:http://laravel.com/api/5.0/

这个函数只能看官方文档,然后去看源码了
ding
2015-05-13 18:44
@JohnLui:刚才做了一下article的代码,尽管很多地方是查阅的pages的代码,但总归是自己写完了,又学习了很多东西。
Vic
2015-04-30 20:04
ErrorException in fd899bc0347f1ea1ceb71cb654c3630c line 48:
Use of undefined constant App - assumed 'App' (View: /Users/vic/Sites/blog/resources/views/admin/comments/index.blade.php)

刚刚弄完还好好的,后来不知道改了哪里,在后台管理评论的列表页报这个错,没知道到底哪里出了问题,PO主希望给指点下。
JohnLui
2015-04-30 22:34
@Vic:上代码
Vic
2015-05-04 17:43
@JohnLui:@extends('app')

@section('content')
<div class="container">
  <div class="row">
    <div class="col-md-10 col-md-offset-1">
      <div class="panel panel-default">
        <div class="panel-heading">
        <h3 class="panel-title">评论管理</h3>
        </div>
        <table class="table table-hover">
          <thead>
              <tr class="row">
              <th>#</th>
            <th>评论内容</th>
            <th>发布人</th>
            <th>邮箱</th>
            <th>文章</th>
            <th>Do</th>
          </tr>
          </thead>
          <tbody>
              @foreach ($comments as $comment)
            <tr class="row">
                  <td>
                    {{ $comment->id }}
                  </td>
              <td>
                {{ $comment->content }}
              </td>
              <td>
                @if ($comment->website)
                  <a target="_blank" href="{{ $comment->website }}">
                    {{ $comment->nickname }}
                  </a>
                @else
                  {{ $comment->nickname }}
                @endif
              </td>
              <td>
                  {{ $comment->email }}
              </td>
              <td>
                <a href="{{ URL('pages/'.$comment->page_id) }}" target="_blank">
                  {{ App/Page::find($comment->page_id)->title }}
                </a>
              </td>
              <td>
                <form action="{{ URL('admin/comments/'.$comment->id) }}" method="POST" style="display: inline;">
                  <input name="_method" type="hidden" value="DELETE">
                  <input type="hidden" name="_token" value="{{ csrf_token() }}">
                  <div class="btn-group btn-group-xs" role="group" aria-label="Do">
                  <a href="{{ URL('admin/comments/'.$comment->id.'/edit') }}" class="btn btn-success">编辑</a>
                  <button type="submit" class="btn btn-danger">删除</button>
                  </div>
                </form>
              </td>
            </tr>
          @endforeach
          </tbody>
        </table>
      </div>
    </div>
  </div>
</div>
@endsection
JohnLui
2015-05-04 18:45
@Vic:{{ App/Page::find($comment->page_id)->title }}
改成:
{{ \App\Page::find($comment->page_id)->title }}
Vic
2015-05-04 18:52
@JohnLui:继续这个错误
Trying to get property of non-object (View: /Users/vic/Sites/blog/resources/views/admin/comments/index.blade.php)
JohnLui
2015-05-04 19:55
@Vic: 这就是基础问题了
Vic
2015-05-04 19:57
@JohnLui:可是没改过PHP代码,突然出来这个错误,laravel里不知道怎么调试
Kirits
2015-09-18 11:15
@JohnLui:请问这是哪里出问题了?我也出现了这样的问题
Steven
2015-05-11 17:29
@Vic:评论列表出错是因为你把page数据删除了     但是评论表数据还在   然后就报错
Vic
2015-05-13 14:19
@Steven:感谢这么细心和耐心回答。赞,一定会好好用心看
crazyeefly
2015-04-21 17:54
貌似laravel 方法在提交评论修改的时候给增加了一列,这个在实际是不存在的,请问下除了在表里增加这列外有没其他办法解决,比如限制插入没有显示写入的列?
QueryException in Connection.php line 620: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'admin/comments/1' in 'field list' (SQL: update `comments` set `page_id` = 1, `nickname` = fefe, `email` = fefe@qq.com, `website` = fefefe, `content` = feffffeeeeeeeeefeeeeeeeeeeeeeeeeeeeeeee, `admin/comments/1` = , `updated_at` = 2015-04-21 09:40:17 where `id` = 1)
JohnLui
2015-04-21 18:38
@crazyeefly:这个 admin/comments/1 肯定是代码写的有问题啦
Litleft
2015-05-14 11:14
@JohnLui:
public function update(Request $request, $id)
    {
        $this->validate($request, [
          'nickname' => 'required',
          'content' => 'required',
        ]);
        
        if (Comment::where('id', $id)->update(Input::except(['_method', '_token', 's']))) {
          return Redirect::to('admin/comments');
        } else {
          return Redirect::back()->withInput()->withErrors('更新失败!');
        }
    }
是会出现这种情况,我打印出来的时候有个 ['s'] => 'admin/comments/1',我在这里过滤了s后解决的,我想知道的是laravel提交的时候,是会将action的参数以s命名传递给后台还是代码那里出现了问题
JohnLui
2015-05-14 11:21
@Litleft:前端代码有问题
Litleft
2015-05-14 11:53
@JohnLui:  能细说麽  是Action那里的有问题还是在route的配置上有问题
JohnLui
2015-05-14 11:57
@Litleft:view 有问题

2015-04-17 15:55
怎么把评论做的像你主页这样,把评论的评论框起来,并缩进呢?
JohnLui
2015-04-17 17:37
@愚:这就需要给 Comment 分层了,我这是 Emlog 系统自带的~
n9527
2015-04-16 12:03
good~很好。已试验了一遍。
marx
2015-04-14 23:27
我是一个phper,关注laravel一段时间了。工作其间一直用thinkphp框架,经常用onethink这个在thinkphp基础上写的一套程序二次开发。心中有个疑惑:为什么laravel在php框架当中这么多使用者?  浏览过一次文档,感觉用来做开发还是挺快的,但是还是不够onethink快,因为例如:有个表需要增加字段,这样就要改后台的代码,而且还要改html的代码(UI),这样很耗时间,而onethink可以在现有的后台修改数据库结构,然后对应的后台与html的代码(UI)也不用改,就可以正常在后台“增删查改”。我其实很想用laravel开发的,看过文档之后的确很诱人,只是看完文档之后感觉开发速度不及onethink,请赐教……非常感谢!
JohnLui
2015-04-14 23:58
@marx:Laravel 在开发速度上的优势更多地体现在Eloquent 上。Laravel 在国外如此之火的原因是他把PHP 项目变的真正 可测试。
marx
2015-04-15 00:03
@JohnLui:“真正可测试”是什么意思??能否举个例子?
JohnLui
2015-04-15 00:25
@marx:就是可以方便地进行整项目测试,我是指写测试代码进行测试。
eager
2015-04-13 17:57
按教程最后一步报错:(
ReflectionException in compiled.php line 1029:
Class App\Http\Controllers\Admin\AdminHomeComtroller does not exist
JohnLui
2015-04-13 18:06
@eager: AdminHomeComtroller 改成 AdminHomeController,sorry 我写错了。。。
eager
2015-04-14 20:14
@JohnLui:你的示例包文件名也是错的啊
JohnLui
2015-04-14 21:14
@eager: 还真是,路由和文件名都错了…所以可以运行……
www
2015-04-13 15:00
没有注册模块啊
Redstoneye
2015-04-13 14:38
超赞!!!!
如来神掌
2015-04-05 11:56
教程都按着做了一下,对laravel新手入门帮助挺大,基本用到的先通过实例展示了,关键是最后的作业,确实如果不自己去做一下的话还是不会太理解整个流程,中间肯定会忘记一些东西,然后就会来反复阅读这套教程,其实就是继续熟悉的一个过程。赞~~~

2015-03-28 17:39
你好,我按教程中的方法对数据库操作可以成功,参照文档配置database.php后使用DB::select方法直接写sql查询,报错:Class 'App\DB' not found,还有什么要配置的吗?
JohnLui
2015-03-28 18:37
@刘: 我是人工审核的。

在 DB 前面加一个 \ 。
哈哈哈哈
2015-03-27 14:41
春天来了
yxr
2015-03-27 10:40
大神求解。
我在点击提交修改和删除按钮之后总是出现MethodNotAllowedHttpException in compiled.php line 7717:错误是怎么回事呢?
JohnLui
2015-03-27 10:41
@yxr:MethodNotAllowedHttpException 方法不允许 异常。你的表单构建的有些问题,比对一下我的代码就明白了~
回风
2015-03-25 20:27
大神,请问L4里的 Form::  是不是不建议继续使用了啊?为什么?
JohnLui
2015-03-25 22:19
@回风:新手不建议使用,影响理解Laravel 。其实Form 还是能提高不少开发效率的。
article
2015-03-25 15:46
大作业的article和之前的page是什么关系呢?
JohnLui
2015-03-25 17:05
@article:没什么关系
Article
2015-05-30 12:08
@JohnLui:同样的疑问,大作业里的 Article 要实现什么功能呢?和教程里的 Page 有什么不同?
JohnLui
2015-05-30 15:26
@Article:没什么不同,目的是让大家真正动手做。Laravel 是非常复杂的东西,看一遍感觉都懂了,但是一旦开始做就会出很多问题。
white
2015-03-18 23:38
大神求解一个问题。。

如何理解以及正确的使用service provider? 为什么要使用service provider?
根据我的理解service provider 的概念是向Ioc容器注册特定的服务,当需要某个服务的时候可以直接从Ioc容器中获取服务实例使用。

比起使用命名空间,service provider 的优势是什么?

是依赖注入么?根据自己在laravel 5上的实验,使用use app/Repositories 这种命名空间方式,laravel 5同样可以实现对于方法的依赖注入。

想到一点优势是服务代理与服务实例分离,当想要替换服务实现的时候,可以简单的通过ServiceProvider 进行修改,而不用像命名空间一样每个文件一一替换。
还有可以依据参数动态绑定服务实现。
除了以上两点以外,service provider 还有哪些优势?应该在什么样的需求下使用service provider?

3ks
JohnLui
2015-05-13 14:33
@white:service provider 的根本目的及时做一层适配器层,定义好标准接口,让底层具体实现可以自由替换。这个优势在底层关键技术升级时可以保持架构不变,测试代码甚至都不用修改,大大提高了可维护性。
黼黻
2015-03-12 15:28
Route::get('auth/register', 'Auth\AuthController@getregister');
Route::post('auth/register', 'Auth\AuthController@postregister');

我配了2个这样的路由没问题了,但是注册成功后 他是跳转到 http://127.0.0.1/home
这个控制器的跳转不知道在什么地方改下
JohnLui
2015-03-12 16:10
@黼黻:在 AuthController 中增加:
public $redirectPath = '/admin';
黼黻
2015-03-13 09:59
@JohnLui:好了,谢谢咯
dafa
2015-08-11 17:28
@黼黻:public function redirectPath()
    {
        if (property_exists($this, 'redirectPath'))
        {
            return $this->redirectPath;
        }

        return property_exists($this, 'redirectTo') ? $this->redirectTo : '/home';
    }

可以用两个属性的!
dafa
2015-08-11 17:26
@黼黻:在 AuthController  == >  use AuthenticatesAndRegistersUsers; 类中
黼黻
2015-03-12 15:11
Auth  带的注册是不是还需要配路由呢,点击Register报
NotFoundHttpException in compiled.php line 7693:

我看的Auth 这个控制器找不到对应的方法。
Vic
2015-04-30 20:06
@黼黻:PO主的教程,中间吧注册的路由删掉了,在仔细看下教程,应该会找到答案的。还有希望PO主看看我的问题啊!

发表评论:

© 2011-2024 岁寒  |  Powered by Emlog