I am trying to create a comment system for posts on a blog. I created a form for uploading a comment. Both the user_id and the post_id columns are properly populated in the table, however, the body column is empty when enter text and submit it. I defined a relationship that one Post hasMany Comments. Also, I defined that one User hasMany Comments. Here is my code:
//commentcontroller.php
public function newComment(){
$id=Auth::id();
$comment = New Comment();
$comment->user_id=$id;
$comment->post_id=Input::get('post_id');
$comment->body=Input::get('body');
post->comments()->save($comment);
}
Form:
<div class="col-md-8 col-md-offset-2">
<hr>
#foreach($posts as $post)
<h3>{{ $post->title}}</h3>
<p>by {{ $post->user->name }}</p>
<img src='{{ asset($post->image_path) }}' class="img-responsive" id="blogpic"/>
<p>{{ Str::words($post->body) }}</p>
<p>Read More...</p>
<hr>
//COMMENT AREA
{{ Form::open(array('action' => 'CommentController#newComment', 'files'=>true)) }}
{{ Form::textarea('body', null, array('class'=>'form-control')) }}
{{ Form::hidden('post_id', $post->id) }}
<br>
<br>
{{ Form::submit('Post') }}
#endforeach
</div>
I am not concerned yet about displaying comments, just uploading them to the database. The user_id and post_id upload correctly in the database, the text just doesn't save. Thanks in advance.
You may try this:
public function newComment()
{
$comment = New Comment();
$comment->user_id = Auth::user()->id;
$comment->post_id = $post_id = Input::get('post_id');
$comment->body = Input::get('body');
Post::find($post_id)->comments()->save($comment);
}
Notice Auth::user()->id is used instead of Auth::id().
Related
I have a strange problem, I am working on a personal project in Laravel, a "Q&A" type site, so far everything is fine. When I want to post a question from the database, it will be displayed twice on the display page, even if I only put the question with a certain id.
the error that appears
Route:
Route::get('/viewUserQuestion/{post}', 'PostsController#viewUserQuestion')->name('viewQuestion');
Controllerr:
public function viewUserQuestion() {
if(Auth::check()) {
$posts = Post::latest()->get();
return view('viewQuestion', compact('posts'));
}
else {
return redirect('register');
}
}
Blade:
<div class="card-body p-0">
#foreach($posts as $post)
<div class="mailbox-read-info">
<h5 align="center"> {{ $post->title }}</h5>
<h6> From userID: {{ $post->user_id }}</h6>
<span class="mailbox-read-time" align="center">Created at: {{ $post->created_at }}</span></h6>
</div><div class="mailbox-read-message">
<p>{{ $post->content }}</p>
Route to the display page:
<td>{{ $post->id }}</td>
What do you think would be the exact problem? I would be grateful if you could help me, does it run twice or does it?
Basically I want to display the data from each id, but in different pages without overlapping. As it is here, you click on a question and display it not twice.
I'm in a WP project. I created a custom post type called Team Members
I also created a custom field block with a field called contact_members which is a repeater and has two sub-fields called location_map and member. This member sub-field is related with the custom post type team member.
I have no issues with getting the location_map.
The issue is that I can't get the post type fields. (coming from contact_members->member->post type
<?php
$members = get_field('contact_members');
?>
#foreach($members as $member)
<div class="member {{ $member['location_map'] }}">
<img class="map" src="{{ get_the_post_thumbnail_url($member['member']->ID) }}">
<h3>{{ get_the_title($member['member']->ID) }}</h3>
<p class="position">{{ $member['member']->position }}</p>
<p class="location">{{ $member['member']->location }}</p>
Contact
</div>
#endforeach
On a first look i cant see any problems with your code. What i consider the most possible senario is ( since that i guess you are using the ACF plugin ) the $member['meber'] variable holds the (int) post_id and not the Post Object. Have you tried var_dump() ?
Cheers!
I found the answer, I should put [0] after $member['member'], in order to reach the array's first element and after that I can finally go after what I want from the object, so:
<?php $members = get_field('contact_members'); ?>
#foreach($members as $member)
<div class="member {{ $member['location_map'] }}">
<img class="member-image" src="{{ get_the_post_thumbnail_url($member['member'][0]->ID) }}">
<h3 class="member-name">{{ $member['member'][0]->post_title }}</h3>
<p class="position">{{ $member['member'][0]->position }}</p>
<p class="location">{{ $member['member'][0]->location }}</p>
Contact
</div>
#endforeach
I have the following route entry:
Route::get('admin/user/edit/{id}', 'AdminController#editUser');
And given is controller Method:
public function editUser($id)
{
$user = User::where('id',1);
return View::make('admin.edit_user')
->with('user',$user);
}
All I want to bind model in my edit form which looks like this:
#extends('layouts.admin_master')
#section('content')
<div>
{{ Form::model($user) }}
{{ Form::label('first_name', 'First Name') }}
{{ Form::text('first_name') }}
{{ Form::close() }}
</div>
#stop
I can see text box but value is not being populated. first_name is column in my table users
I was trying to do what mention here
Try this..
public function editUser($id)
{
$user = User::where('id',1)->first();
return View::make('admin.edit_user')
->with('user',$user);
}
#extends('layouts.admin_master')
#section('content')
<div>
{{ Form::model($user) }}
{{ Form::label('first_name', 'First Name') }}
{{ Form::text('first_name',$user->first_name) }}
{{ Form::close() }}
</div>
#stop
I am trying to update information in my database with Laravel. Not sure what I am doing wrong but I can't seem to find where the problem is. Here is the code for my edit page (This is the page where I would edit information taken from my DB).
{{ Form::open(['url'=>'portfolio/update']) }}
<div>
{{ Form::label('portfolio_title', 'Portfolio Title:') }}
{{ Form::text('portfolio_title',$work->portfolio_title) }}
{{ $errors->first('portfolio_title','<span class="error">:message</span>') }}
</div>
<div>
{{ Form::label('portfolio_description', 'Portfolio Description') }}<br>
{{ Form::textarea('portfolio_description', $work->portfolio_description, ['size' => '50x5']) }}
{{ $errors->first('portfolio_description','<span class="error">:message</span>') }}
</div>
<div>
{{ Form::label('portfolio_content', 'Portfolio Content') }}<br>
{{ Form::textarea('portfolio_content', $work->portfolio_content, ['size' => '50x5']) }}
{{ $errors->first('portfolio_content','<span class="error">:message</span>') }}
</div>
{{ Form::hidden('id',$work->id) }}
<div>
{{ Form::submit('Update Work') }}
</div>
{{ Form::close() }}
I have a controller called PortfolioController that will save info to database and what not.
public function edit($work_title){
$work = Portfolio::wherePortfolio_title($work_title)->first();
return View::make('portfolio/edit', ['work' => $work]);
}
public function update(){
$id = Input::get('id');
$input = Input::except('id');
if( !$this->portfolio->fill($input)->isValid()){
return Redirect::back()->withInput()->withErrors($this->portfolio->errors);
}
$work = Portfolio::find($id);
$work->portfolio_title = Input::get('id');
$work->save();
}
Here is my route that I am working with:
Route::resource('portfolio','PortfolioController');
Route::post('portfolio/update','PortfolioController#update');
I am able to get the form populated with the correct information but when i change something like the title and click update, the page reloads but does not save in the DB. Sometimes I will get an MethodNotAllowedHttpException error. This has been pretty frustrating for me so any help will be greatly appreciated.
Why don't you just actually use your resource route?
First, remove the portfolio/update route. You don't need it.
Then change your Form::open to this:
{{ Form::open(['route' => ['portfolio.update', $work->portfolio_title], 'method' => 'put']) }}
This way you target the update method in your RESTful controller.
Finally change that to use the portfolio_title as identifier and you should be able to remove the hidden id field from your form.
public function update($work_title){}
Please take a look at:
RESTful controllers
Opening a form
I have a number of fields which are NULL by default in MySQL. For example, in the code snips below, name is required and not null, but name_abbrev, email_general and description are all nullable and set to NULL by default in the database. But data entry via tables in Laravel mostly is not working. If I insert a row with only name and everything else left blank, name of course is entered correctly, and NULL is entered for email_general, but the other two nullable fields post the empty string instead of NULL. If I then update the row, say by changing the name, the update will also change the NULL for email_general to the empty string. And if I manually change the values in the database row for the nullable fields from empty string to NULL, when I update the row (still leaving all the empty fields empty), it changes all the NULL fields to empty string. I can't find anything I'm doing wrong. Why won't it enter NULL (except in one field which is coded exactly the same as the others), and why does update change even that field to the empty string?
Controller:
public function store()
{
$component = new Component;
$component->name = Input::get('name');
$component->name_abbrev = Input::get('name_abbrev');
$component->email_general = Input::get('email_general');
$component->description = Input::get('description'); ...
$component->save();
return Redirect::route('components.index');
}
public function update($id)
{
$component = $this->component->find($id);
$component->name = Input::get('name');
$component->name_abbrev = Input::get('name_abbrev');
$component->email_general = Input::get('email_general');
$component->description = Input::get('description'); ...
$component->save();
return Redirect::route('components.index');
}
create.blade.php:
{{ Form::open(['route' => 'components.store']) }}
<div class="required">
{{ Form::label('name','Name:') }}
{{ Form::text('name') }}
{{ $errors->first('name') }}
</div>
<div>
{{ Form::label('name_abbrev','Abbreviation:') }}
{{ Form::text('name_abbrev', NULL) }}
</div>
<div>
{{ Form::label('email','General Email:') }}
{{ Form::text('email',NULL) }}
</div>
<div>
{{ Form::label('description','Description:') }}
{{ Form::textarea('description',NULL,['size' => '26x3']) }}
</div> ...
<div>
{{ Form::submit('Add New Component', array('class'=>'button')) }}
</div>
{{ Form::close() }}
edit.blade.php:
{{ Form::model($component, array('method'=>'put','route'=>array('components.update', $component->id))) }}
<div class="required">
{{ Form::label('name','Name:') }}
{{ Form::text('name') }}
{{ $errors->first('name') }}
</div>
<div>
{{ Form::label('name_abbrev','Abbreviation:') }}
{{ Form::text('name_abbrev', NULL) }}
</div>
<div>
{{ Form::label('email_general','General Email:') }}
{{ Form::text('email_general', NULL) }}
</div
<div>
{{ Form::label('description','Description:') }}
{{ Form::textarea('description',NULL,['size' => '26x3']) }}
</div> ...
<div>
{{ Form::submit('Update Component', array('class'=>'button')) }}
</div>
{{ Form::close() }}
Thanks very much! This should be easy, but for some reason, it's not.
If you really need null on some column, I would recommend you to use Mutators:
Laravel 3: http://three.laravel.com/docs/database/eloquent#getter-and-setter-methods
Laravel 4: http://laravel.com/docs/eloquent#accessors-and-mutators
So if the new value is an empty string ( ! $value ) overwrite it with NULL.