我正在使用Laravel 5开发一个管理风格的应用程序,该应用程序具有登录/注销功能和用户创建自己帖子的功能(在本例中,它们被称为“预订”)。我希望每个用户都能够创建他们自己独特的预订——即只有当他们用用户名和密码登录时才可见。为了实现这一点,我在“预订”表(存储所有预订)中为user_id创建了一列。当登录用户创建预订时,我希望他/她自己的用户标识存储在user_id列中。所以此刻我正在通过user_id(使用auth()确定-
下面是我的一些代码://BookingsController。php:
<?php
namespace App\Http\Controllers;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Http\Request;
use View;
use App\Model;
use App\User;
use App\Booking;
use App\BookingItem;
use App\Http\Controllers\Validator;
use Input;
class BookingsController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$bookings = \App\Booking::all();
return View::make('bookings.index')->with('bookings', $bookings);
// $user = auth()->user()->id;
// $bookings = auth()->user()->bookings()->get();
// return view('bookings.index')->with('bookings', $bookings);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
$user = auth()->user();
return View::make('bookings.create')->with('user', $user);
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request, $user_id)
{
// define rules
$rules = [
'name' => 'between:2,255',
];
$this->validate($request, [
'name' => 'required|unique:bookings|max:255',
]);
$name = $request->get('name');
$description = $request->get('description');
$booking->name = $name;
$booking->user_id = $user_id;
$booking->description = $description;
$booking->save();
return Redirect::route('bookings.index')->withMessage('Booking Was Created');
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
$booking = \App\Booking::findorFail($id);
$items = $booking->BookingItems()->get();
return View::make('bookings.show')
->withBooking($booking) // will allow us to see our bookings, by id.
->withItems($items);
}
}
?>
//创造。刀身php
@extends('layouts.main')
@section('content')
<div class="container">
<div class="row">
<div class="small-6 columns">
<div class="panel panel-default">
<div class="panel-heading"><h2>Add a new booking</h2></div>
<div class="panel-body">
{{ var_dump($user->id) }}
<form class="form-horizontal" role="form" method="POST" action="{{ route('bookings.store', $user->id) }}">
<input name="_method" type="hidden" value="PUT">
{!! csrf_field() !!}
@include('bookings.partials._form');
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<button type="submit" class="btn btn-primary">
<i class="fa fa-btn fa-briefcase"></i>Add Booking
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
@stop
//网络。php(我的路线)
Route::get('/', 'BookingsController@index');
Route::resource('bookings', 'BookingsController');
Route::resource('bookings.items', 'BookingItemController', ['except' => ['index', 'show']]);
Route::resource('agents', 'UsersController');
Route::get('/bookings/{bookings}/items/{items}/complete', ['as' => 'bookings.items.complete', 'uses' => 'BookingItemController@complete']);
Auth::routes();
Route::get('/home', 'HomeController@index');
Route::get('/logout', function(){
Auth::logout();
return Redirect::route('bookings.index')->withErrors('You Are Logged Out!');
});
Route::post('/bookings/{id}', ['uses' => 'BookingsController@store', 'as' => 'bookings.store']);
“我的预订”表包含以下列:
id(pk)用户id(fk)名称描述在更新时创建
如果有人有任何建议或能提供帮助,那就太好了。目前,我认为这可能是默认的表演路线。
当做
罗伯特·伦敦,英国
当前路线:
+--------+-----------+--------------------------------------------+-------------------------+------------------------------------------------------------------------+--------------+
| Domain | Method | URI | Name | Action | Middleware |
+--------+-----------+--------------------------------------------+-------------------------+------------------------------------------------------------------------+--------------+
| | GET|HEAD | / | | App\Http\Controllers\BookingsController@index | web |
| | GET|HEAD | agents | agents.index | App\Http\Controllers\UsersController@index | web |
| | POST | agents | agents.store | App\Http\Controllers\UsersController@store | web |
| | GET|HEAD | agents/create | agents.create | App\Http\Controllers\UsersController@create | web |
| | GET|HEAD | agents/{agent} | agents.show | App\Http\Controllers\UsersController@show | web |
| | DELETE | agents/{agent} | agents.destroy | App\Http\Controllers\UsersController@destroy | web |
| | PUT|PATCH | agents/{agent} | agents.update | App\Http\Controllers\UsersController@update | web |
| | GET|HEAD | agents/{agent}/edit | agents.edit | App\Http\Controllers\UsersController@edit | web |
| | GET|HEAD | api/user | | Closure | api,auth:api |
| | POST | bookings | bookings.store | App\Http\Controllers\BookingsController@store | web |
| | GET|HEAD | bookings | bookings.index | App\Http\Controllers\BookingsController@index | web |
| | GET|HEAD | bookings/create | bookings.create | App\Http\Controllers\BookingsController@create | web |
| | GET|HEAD | bookings/{bookings}/items/{items}/complete | bookings.items.complete | App\Http\Controllers\BookingItemController@complete | web |
| | DELETE | bookings/{booking} | bookings.destroy | App\Http\Controllers\BookingsController@destroy | web |
| | PUT|PATCH | bookings/{booking} | bookings.update | App\Http\Controllers\BookingsController@update | web |
| | GET|HEAD | bookings/{booking} | bookings.show | App\Http\Controllers\BookingsController@show | web |
| | GET|HEAD | bookings/{booking}/edit | bookings.edit | App\Http\Controllers\BookingsController@edit | web |
| | POST | bookings/{booking}/items | bookings.items.store | App\Http\Controllers\BookingItemController@store | web |
| | GET|HEAD | bookings/{booking}/items/create | bookings.items.create | App\Http\Controllers\BookingItemController@create | web |
| | DELETE | bookings/{booking}/items/{item} | bookings.items.destroy | App\Http\Controllers\BookingItemController@destroy | web |
| | PUT|PATCH | bookings/{booking}/items/{item} | bookings.items.update | App\Http\Controllers\BookingItemController@update | web |
| | GET|HEAD | bookings/{booking}/items/{item}/edit | bookings.items.edit | App\Http\Controllers\BookingItemController@edit | web |
| | POST | bookings/{id} | bookings.store | App\Http\Controllers\BookingsController@store | web |
| | GET|HEAD | home | | App\Http\Controllers\HomeController@index | web,auth |
| | GET|HEAD | login | login | App\Http\Controllers\Auth\LoginController@showLoginForm | web,guest |
| | POST | login | | App\Http\Controllers\Auth\LoginController@login | web,guest |
| | POST | logout | | App\Http\Controllers\Auth\LoginController@logout | web |
| | GET|HEAD | logout | | Closure | web |
| | POST | password/email | | App\Http\Controllers\Auth\ForgotPasswordController@sendResetLinkEmail | web,guest |
| | GET|HEAD | password/reset | | App\Http\Controllers\Auth\ForgotPasswordController@showLinkRequestForm | web,guest |
| | POST | password/reset | | App\Http\Controllers\Auth\ResetPasswordController@reset | web,guest |
| | GET|HEAD | password/reset/{token} | | App\Http\Controllers\Auth\ResetPasswordController@showResetForm | web,guest |
| | GET|HEAD | register | | App\Http\Controllers\Auth\RegisterController@showRegistrationForm | web,guest |
| | POST | register | | App\Http\Controllers\Auth\RegisterController@register | web,guest |
+--------+-----------+--------------------------------------------+-------------------------+------------------------------------------------------------------------+--------------+
您需要将路由命名为其他名称,因为bookings.store
已经在Route::资源('bookings','BookingsController')中定义;
我建议从资源中排除此资源,并使用可选的{id?}定义您的
参数,您可以检查是否存在,以便您可以存储用户的预订或基于该参数在存储方法中的存在的正常预订。bookings.store
按照@Steve Bauman的建议,删除
所有这些都应符合以下要求:
Route::get('/', 'BookingsController@index');
Route::resource('bookings', 'BookingsController', ['excep'=> ['store']]);
Route::post('/bookings/{id?}', ['uses' => 'BookingsController@store', 'as' => 'bookings.store']);
Route::resource('bookings.items', 'BookingItemController', ['except' => ['index', 'show']]);
Route::resource('agents', 'UsersController');
Route::get('/bookings/{bookings}/items/{items}/complete', ['as' => 'bookings.items.complete', 'uses' => 'BookingItemController@complete']);
Auth::routes();
Route::get('/home', 'HomeController@index');
Route::get('/logout', function(){
Auth::logout();
return Redirect::route('bookings.index')->withErrors('You Are Logged Out!');
});
Route::post('/bookings/{id}', ['uses' => 'BookingsController@store', 'as' => 'bookings.store']);
然后在控制器的存储方法中:
public function store(Request $request, $user_id = null)
{
// define rules
$rules = [
'name' => 'between:2,255',
];
$this->validate($request, [
'name' => 'required|unique:bookings|max:255',
]);
$booking->name = $request->get('name');
$booking->description = $request->get('description');
$booking->user_id = $user_id;
$booking->save();
return Redirect::route('bookings.index')->withMessage('Booking Was Created');
}
这样,如果有id,它将为您处理该工作,如果没有id,它将为
null
为什么不在创建过程中获取经过身份验证的用户ID,然后通过表单发送?这可能也是一个巨大的安全问题,因为用户可以修改页面的HTML,并使用不同的用户ID提交预订。
这里有一个解决方案:
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
*
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
// define rules
$rules = [
'name' => 'between:2,255',
];
$this->validate($request, [
'name' => 'required|unique:bookings|max:255',
]);
$booking = new Booking();
$booking->name = $request->name;
$booking->user_id = auth()->id();
$booking->description = $request->description;
$booking->save();
return Redirect::route('bookings.index')->withMessage('Booking Was Created');
}
编辑:
你在这里有这条路线:
Route::post('/bookings/{id}', ['uses' => 'BookingsController@store', 'as' => 'bookings.store']);
但是在HTML表单中,您正在启动一个PUT
请求,而它应该只是一个标准的POST
:
<input name="_method" type="hidden" value="PUT">
问题内容: 我想清除并更改给定URL上的输入值。我使用的js不会执行任何操作,也不会导致任何错误。为什么会发生这种情况,我该如何解决? 问题答案: 一个可能且可能的答案是,在尝试清除的元素之前,您还有另一个具有相同名称的元素。 请检查html并检查是否找到多个元素,然后采用第一个。 另外,您可以使用css选择器,例如:
我的问题是为什么找不到“name”字段?
我找了很多,但没有找到任何答案,所以我希望你能帮助我! 我有三个表:post、users和post\U users。Post_users包含两列:user_id和Post_id。 我需要得到一个有口才的用户的所有帖子。有什么想法吗? 非常感谢你! (如果这个问题已经存在,我很抱歉,但我还没有找到它)
问题内容: 即时通讯试图将我的用户使用网络表单输入的长文本(约2500个字符)保存到我的数据库中,并使用php传递给服务器。 当我在phpmyadmin中查看时,文本会变大。如何配置表格以获得完整的文本?这是我的表配置: 看一看具有3000个字符限制的字段 内容 ,但是文本总是以 690个字符为准 。谢谢你的帮助! 编辑 :我发现了问题,但我不知道如何解决。查询总是以相同的char形式获取农作物,
我的表单已提交,但未存储在表中。如果我对请求执行dd(),则数据在中,但当我执行save()时,它不会按预期工作。我想在backoffice表单上添加用户,仅包含姓名、电子邮件、用户类型和密码。 编辑:我将问题图像更改为代码,以便您更容易理解,很抱歉第一次尝试。编辑2:现在出现了更多的两件事,密码验证确认总是错误的,如果我跳过验证,则会出现以下错误: 对未定义方法App\User::forget(
此链接中的项目树图像 我正在开发一个社交媒体应用程序 在Firebase数据库中,将有用户节点、发布节点、喜欢节点和请求类型节点 用户节点包含表示用户详细信息的字段,它有一个类似于键的uid 在帖子节点下,会有一个postskey,里面会有帖子的详细信息,如uid、proileimage、postimage、描述、日期、时间、全名帖子节点中的uid是用户节点中的uid 我成功地将图像上传到Fire