这里的情况是:
我有2张桌子。
1. User
2. Post
用户可以登录,登录和注销,他可以创建自己的个人资料,他可以更新。
帖子表包含单独的用户信息,当用户创建他的个人资料时,此表包含数据,用户也可以从该表中更新数据。
问题是我可以创建用户,但无法更新用户。我该如何解决这个问题?
Undefined property:
Illuminate\Database\Eloquent\Collection::$first_name (View:
C:\xampp\htdocs\protfolio\resources\views\update.blade.php)
这是我的用户模型:
<?php
namespace App;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
protected $fillable = [
'name', 'email', 'password',
];
protected $hidden = [
'password', 'remember_token',
];
public function post()
{
return $this->hasOne('App\Post');
}
}
这是帖子模型:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
public function user()
{
return $this->belongsTo('App\User'); //Profile is your profile model
}
}
这是帖子控制器:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use App\Http\Requests;
use App\Post;
use App\User;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\File;
class PostController extends Controller
{
public function getDashboard()
{
$posts = Post::all();
return view('dashboard',['posts'=>$posts]);
}
public function postUpdate(Request $request)
{
$this->validate($request,[
'first_name'=> 'required|max:120',
'middle_name'=> 'required|max:120',
'last_name' => 'required|max:120',
'gender'=> 'required',
'dob'=>'required',
'nationality'=>'required',
'nid'=>'required',
'email' => 'required|email|unique:users',
'phone_no'=>'required',
'about_me'=>'required',
]);
$post = Post::find($request['postId']);
if (Auth::user() != $post->user) {
return redirect()->back();
}
$post->first_name = $request['first_name'];
$post->middle_name = $request['middle_name'];
$post->last_name = $request['last_name'];
$post->gender = $request['gender'];
$post->dob = $request['dob'];
$post->nationality = $request['nationality'];
$post->nid = $request['nid'];
$post->email = $request['email'];
$post->phone_no = $request['phone_no'];
$post->about_me = $request['about_me'];
$post->update();
return redirect()->route('dashboard');
}
public function postCreate(Request $request){
$this->validate($request,[
'first_name'=> 'required|max:120',
'middle_name'=> 'required|max:120',
'last_name' => 'required|max:120',
'gender'=> 'required',
'dob'=>'required',
'nationality'=>'required',
'nid'=>'required',
'email' => 'required|email|unique:users',
'phone_no'=>'required',
'about_me'=>'required',
]);
$post = new Post();
$post->first_name = $request['first_name'];
$post->middle_name = $request['middle_name'];
$post->last_name = $request['last_name'];
$post->gender = $request['gender'];
$post->dob = $request['dob'];
$post->nationality = $request['nationality'];
$post->nid = $request['nid'];
$post->email = $request['email'];
$post->phone_no = $request['phone_no'];
$post->about_me = $request['about_me'];
$message='There was an Error';
if( $request->user()->post()->save($post)){
$message = "Profile Created successfully";
}
return redirect()->route('dashboard')->with(['message' => $message]);
}
}
以下是用户控制器:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\User;
use App\Post;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Storage;
class UserController extends Controller
{
public function getAccount()
{
$posts = Post::all();
return view('update',['user'=>Auth::user()],['posts'=>$posts]);
}
public function getCreate()
{
return view('create',['user'=>Auth::user()]);
}
}
这是我的update.blade.php
@extends('layouts.master')
@section('content')
@include('includes.message-block')
<div class="container" >
<h3> Create course </h3>
{!! Form::open(array('route' => 'update','class'=>'form-horizontal','method'=>'POST')) !!}
{!! Form::token(); !!}
{!! csrf_field() ; !!}
<div class="form-group">
<label>First Name</label>
<input type="text" name="first_name" class="form-control" value="{{$posts->first_name}}">
</div>
<div class="form-group">
<label>Middle Name</label>
<input type="text" name="middle_name" class="form-control" value="{{$posts->middle_name}}">
</div>
<div class="form-group">
<label>Last Name</label>
<input type="text" name="last_name" class="form-control" value="{{$posts->last_name}}">
</div>
<div class="form-group">
<label>Gender</label>
<select class="form-control" name="gender" value="{{$posts->gender}}">
<option>Male</option>
<option>Female</option>
</select>
</div>
<div class="form-group">
<label> Date Of Birth</label>
<input type="date" name="dob" class="form-control" value="{{$posts->dob}}">
</div>
<div class="form-group">
<label>Nationality</label>
<input type="text" name="nationality" class="form-control" value="{{$posts->nationality}}">
</div>
<div class="form-group">
<label>NID</label>
<input type="text" name="nid" class="form-control" value="{{$posts->nid}}">
</div>
<div class="form-group">
<label>Email</label>
<input type="email" name="email" class="form-control" value="{{$posts->email}}">
</div>
<div class="form-group">
<label>Phone</label>
<input type="text" name="phone_no" class="form-control" value="{{$posts->phone_no}}">
</div>
<div class="form-group">
<label>About Me</label>
<textarea class="form-control" name="about_me" rows="3">"{{$posts->about_me}}"</textarea>
</div>
<button type="submit" class="btn btn-default">Submit</button>
{!! Form::close() !!}
</div>
@endsection
答案 0 :(得分:1)
$posts->first()->first_name
并在更新页面中的所有其他变量中使用相同的内容
答案 1 :(得分:0)
问题似乎与用户模型类定义有关,请在此处尝试以下代码:
用户模型
namespace App;
use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
use Authenticatable;
...
答案 2 :(得分:0)
我认为你的问题在这里:
.when('/index', {
// Get saved data from sessionStorage
var pageAlreadyLoaded = sessionStorage.getItem('pageAlreadyLoaded');
if (pageAlreadyLoaded) {
templateUrl: function ($params) {
return '/' + $params['language'] + '/template/home';
},
controller: 'homepagecontroller'
} else {
// Do Nothing
}
})
Post :: all()获取您表中的所有帖子,而不是属于该用户的帖子。 (我认为这就是你想要的)。
要获取与当前登录用户关联的帖子,您可以尝试:
sessionStorage.setItem("pageAlreadyLoaded", 1);