I've been trying to edit a record. My code will create a new record if the data is null. However, I get the following error:
Call to a member function fill() on null.
I'm not sure what I did wrong; maybe I didn't declare?
Controller
<?php
public function auctionUpdate(Request $request, MediaSite $mediaSite)
{
$auction = $mediaSite->auction;
DB::transaction(function() use ($request, $mediaSite, $auction){
$auction->fill($request->only([
'status', 'start_time', 'end_time', 'period_start_date'
]));
if($auction == null)
$auction = new Auction();
$auction->save();
});
return view('admin.media-site.show', [
'mediaSite' => $mediaSite,
'auction' => $auction
]);
}
You should check if auction is null before fill()
your modified script
public function auctionUpdate(Request $request, MediaSite $mediaSite)
{
$auction = $mediaSite->auction;
DB::transaction(function() use ($request, $mediaSite, $auction){
if($auction == null)
$auction = new Auction();
$auction->fill($request->only([
'status', 'start_time', 'end_time', 'period_start_date'
]));
$auction->save();
});
return view('admin.media-site.show', [
'mediaSite' => $mediaSite,
'auction' => $auction
]);
}
Related
How to pass a variable($test) from store to index? because I would like to display a variable in my index.blade
public function index()
{
return view('users.index', [
'users' => User::all()
]);
}
public function store(Request $request)
{
$user = new User($request->all());
$user->save();
$test = "test";
return redirect('users');
}
To resolve your problem you may edit your code like below:
index function:
public function index($test=null)
{
return view('users.index', [
'users' => User::all(),
'test' => $test
]);
}
store function:
public function store(Request $request)
{
$user = new User($request->all());
$user->save();
$test = "test";
return redirect(route('users.index', compact('test')));
}
N.B: for storing your user I don't recommend to you mass assignment (new User($request->all())) when you create a new user especially if you have a password or token to store there.
Hey guys so I made a route:
Route::get('/dashboard/{user}', [DashboardController::class, 'show'])->name('dashboard.show');
My controller is
public function show($id)
{
return view('dashboard.profile')->with('name',User::where($id));
}
How do pass it into the view? so I get only data from the current user / userid
You can simplify it to this by using route model binding:
public function show(User $user)
{
return view('dashboard.profile', [ 'user' => $user ]);
}
You Can Do This:
public function show(User $user)
{
return view('dashboard.profile', [ 'user' => $user ]);
}
Or :
public function show($id)
{
$user = User::findOrFail($id);
return view('dashboard.profile', [ 'user' => $user ]);
}
Or :
public function show($id)
{
$user = User::where("id",$id)->first();
return view('dashboard.profile', [ 'user' => $user ]);
}
If You Need Authenticated user use :
auth()->user
When I'm trying to update post it's updated successfully but, when it returns it shows error here-
NB: Replies under this post where I'm trying to update.
public function show($slug)
{
$discussion = Discussion::where('slug', $slug)->first();
$best_answer = $discussion->replies()->where('best_answer', 1)->first();
return view('discussions.show')
->with('d', $discussion)
->with('best_answer', $best_answer);
}
Edit and Update
public function edit($slug)
{
return view('discussions.edit', ['discussion'=> Discussion::where('slug', $slug)->first()]);
}
public function update($id)
{
$this->validate(request(),[
'title' => 'required',
'content' => 'required'
]);
$d = Discussion::find($id);
$d->title = request()->title;
$d->content = request()->content;
$d->save();
Session::flash('success', 'Discussion updated');
return redirect()->route('discussion', ['slug', $d->slug]);
}
I have a function for adding a comment:
public function addComment(Request $request)
{
$request->validate([
'body' => 'required',
]);
$entry = new Comment();
$entry->body = $request->body;
$entry->save();
return redirect('/');
}
I need to also pass in the films table id, known in the comments table as film_id. This field can not be null. The above doesnt take this field into account and so I get the following message:
SQLSTATE[23000]: Integrity constraint violation: 19 NOT NULL
constraint failed: comments.film_id (SQL: insert into "comments"
("body", "updated_at", "created_at") values
I have tried to pass in the film id by doing variations of the below but no success.
public function addComment(Request $request, $id)
{
$request->validate([
'body' => 'required',
'film_id' => 'required',
]);
$entry = new Comment();
$film_id = Film::find($id);
$entry->body = $request->body;
$entry->film_id = $film_id;
$entry->save();
return redirect('/');
Comment model:
class Comment extends Model
{
public function film()
{
return $this->belongsTo(Film::class);
}
}
Film model:
class Film extends Model
{
public function comments()
{
return $this->hasMany(Comment::class);
}
}
you are not passing the id, you were passing the film object
public function addComment(Request $request, $id)
{
$film = Film::find($id);
$entry = new Comment();
$entry->body = $request->body;
$entry->film_id = $film->id;
$entry->save(); //your comment is saved with proper film_id
}
or
public function addComment(Request $request, $id)
{
$film = Film::find($id);
$film->comments()->save(['body'=>$request->body]);
}
I want to update a data in the database
i have controller
public function update(Identity $identity, Request $request)
{
$data = new Identity();
$data->date = $request['date'];
$data->code = $request['code'];
$data->name = $request['name'];
$request->user()->identity()->update($data);
Session::flash('flash_message', 'Update success.');
return redirect('identity.index');
}
Model Identity
public function user()
{
// Each data is owned by only one user
return $this->belongsTo('App\User');
}
Model User
public function identity()
{
// Each user will have a lot of data
return $this->hasMany('App\Identity');
}
And i found an error: Argument 1 passed to Illuminate\Database\Eloquent\Relations\HasOneOrMany::update() must be of the type array, object given.
You already have the Identity model with the route model binding. You can do one of the below.
public function update(Identity $identity, Request $request)
{
$identity->date = $request['date'];
$identity->code = $request['code'];
$identity->name = $request['name'];
$identity->save();
Session::flash('flash_message', 'Update success.');
return redirect('identity.index');
}
Or (Make sure you set the $fillable property in the model for this to work)
public function update(Identity $identity, Request $request)
{
$identity->update([
'date' => $request['date'],
'code' => $request['code'],
'name' => $request['name'],
]);
Session::flash('flash_message', 'Update success.');
return redirect('identity.index');
}
This line
$data = new Identity();
creates an object. Below that you are setting its properties. Instead, it looks like you can pass your properties directly into the function:
public function update(Identity $identity, Request $request)
{
$request->user()->identity()->update(array($request));
...
}
Of course you might also want to restrict your request to just what's needed:
public function update(Identity $identity, Request $request)
{
$params = array_intersect_key(array($request), array_flip(['date', 'code', 'name']))
$request->user()->identity()->update($params);
...
}