Can't associate an user's id to a task Larvel
I'm trying to create a to-do list web app using Laravel 5.7. For auth i used php artisan make:auth.
When I try to add a task, it just spits out error 500.
Also, displaying post that are manually created works.
web.php
<?php
Route::get('/', 'TaskController@index')->name('index');
Route::post('/tasks', 'TaskController@store');
Route::delete(' /tasks/{task}', 'TaskController@destroy');
Auth::routes();
User.php
<?php
namespace App;
use IlluminateNotificationsNotifiable;
use IlluminateFoundationAuthUser as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
protected $fillable = [
'name', 'email', 'password',
];
protected $hidden = [
'password', 'remember_token',
];
public function tasks()
{
return $this->hasMany(Task::class);
}
}
Task.php (that Model just adds guarded and stuff to all other models)
<?php
namespace App;
class Task extends Model
{
public function user()
{
return $this->belongsTo(User::class, 'user_id');
}
}
Up function (tasks)
public function up()
{
Schema::create('tasks', function (Blueprint $table) {
$table->increments('id');
$table->string('text');
$table->unsignedInteger('user_id');
$table->timestamps();
});
}
Task Controller
namespace AppHttpControllers;
use AppTask;
class TaskController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function index()
{
$tasks = Task::where('user_id', auth()->id())->get();
return view('index', compact('tasks'));
}
public function store()
{
$task = Task::create([
'text' => request()->validate([
'text' => 'required|max:255'
]),
'user_id' => auth()->id()
]);
return response()->json($task->id);
}
public function destroy(Task $task)
{
$task->delete();
}
}
Any other suggestions are welcome.
php laravel authentication frameworks todo
add a comment |
I'm trying to create a to-do list web app using Laravel 5.7. For auth i used php artisan make:auth.
When I try to add a task, it just spits out error 500.
Also, displaying post that are manually created works.
web.php
<?php
Route::get('/', 'TaskController@index')->name('index');
Route::post('/tasks', 'TaskController@store');
Route::delete(' /tasks/{task}', 'TaskController@destroy');
Auth::routes();
User.php
<?php
namespace App;
use IlluminateNotificationsNotifiable;
use IlluminateFoundationAuthUser as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
protected $fillable = [
'name', 'email', 'password',
];
protected $hidden = [
'password', 'remember_token',
];
public function tasks()
{
return $this->hasMany(Task::class);
}
}
Task.php (that Model just adds guarded and stuff to all other models)
<?php
namespace App;
class Task extends Model
{
public function user()
{
return $this->belongsTo(User::class, 'user_id');
}
}
Up function (tasks)
public function up()
{
Schema::create('tasks', function (Blueprint $table) {
$table->increments('id');
$table->string('text');
$table->unsignedInteger('user_id');
$table->timestamps();
});
}
Task Controller
namespace AppHttpControllers;
use AppTask;
class TaskController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function index()
{
$tasks = Task::where('user_id', auth()->id())->get();
return view('index', compact('tasks'));
}
public function store()
{
$task = Task::create([
'text' => request()->validate([
'text' => 'required|max:255'
]),
'user_id' => auth()->id()
]);
return response()->json($task->id);
}
public function destroy(Task $task)
{
$task->delete();
}
}
Any other suggestions are welcome.
php laravel authentication frameworks todo
add a comment |
I'm trying to create a to-do list web app using Laravel 5.7. For auth i used php artisan make:auth.
When I try to add a task, it just spits out error 500.
Also, displaying post that are manually created works.
web.php
<?php
Route::get('/', 'TaskController@index')->name('index');
Route::post('/tasks', 'TaskController@store');
Route::delete(' /tasks/{task}', 'TaskController@destroy');
Auth::routes();
User.php
<?php
namespace App;
use IlluminateNotificationsNotifiable;
use IlluminateFoundationAuthUser as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
protected $fillable = [
'name', 'email', 'password',
];
protected $hidden = [
'password', 'remember_token',
];
public function tasks()
{
return $this->hasMany(Task::class);
}
}
Task.php (that Model just adds guarded and stuff to all other models)
<?php
namespace App;
class Task extends Model
{
public function user()
{
return $this->belongsTo(User::class, 'user_id');
}
}
Up function (tasks)
public function up()
{
Schema::create('tasks', function (Blueprint $table) {
$table->increments('id');
$table->string('text');
$table->unsignedInteger('user_id');
$table->timestamps();
});
}
Task Controller
namespace AppHttpControllers;
use AppTask;
class TaskController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function index()
{
$tasks = Task::where('user_id', auth()->id())->get();
return view('index', compact('tasks'));
}
public function store()
{
$task = Task::create([
'text' => request()->validate([
'text' => 'required|max:255'
]),
'user_id' => auth()->id()
]);
return response()->json($task->id);
}
public function destroy(Task $task)
{
$task->delete();
}
}
Any other suggestions are welcome.
php laravel authentication frameworks todo
I'm trying to create a to-do list web app using Laravel 5.7. For auth i used php artisan make:auth.
When I try to add a task, it just spits out error 500.
Also, displaying post that are manually created works.
web.php
<?php
Route::get('/', 'TaskController@index')->name('index');
Route::post('/tasks', 'TaskController@store');
Route::delete(' /tasks/{task}', 'TaskController@destroy');
Auth::routes();
User.php
<?php
namespace App;
use IlluminateNotificationsNotifiable;
use IlluminateFoundationAuthUser as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
protected $fillable = [
'name', 'email', 'password',
];
protected $hidden = [
'password', 'remember_token',
];
public function tasks()
{
return $this->hasMany(Task::class);
}
}
Task.php (that Model just adds guarded and stuff to all other models)
<?php
namespace App;
class Task extends Model
{
public function user()
{
return $this->belongsTo(User::class, 'user_id');
}
}
Up function (tasks)
public function up()
{
Schema::create('tasks', function (Blueprint $table) {
$table->increments('id');
$table->string('text');
$table->unsignedInteger('user_id');
$table->timestamps();
});
}
Task Controller
namespace AppHttpControllers;
use AppTask;
class TaskController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function index()
{
$tasks = Task::where('user_id', auth()->id())->get();
return view('index', compact('tasks'));
}
public function store()
{
$task = Task::create([
'text' => request()->validate([
'text' => 'required|max:255'
]),
'user_id' => auth()->id()
]);
return response()->json($task->id);
}
public function destroy(Task $task)
{
$task->delete();
}
}
Any other suggestions are welcome.
php laravel authentication frameworks todo
php laravel authentication frameworks todo
asked Nov 25 '18 at 11:27
Andrei HirbuAndrei Hirbu
61
61
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
First, you should use request classes for validation of requests. So, your store method would look like:
public function store(TaskRequest $taskRequest){}
Second, add the following in your task model.
protected $fillable = [
'text'
];
public static function boot()
{
static::creating(function($model){
$model['user_id'] = Auth::user()->id;
});
}
Third, your store method would like this.
public function store(TaskRequest $taskRequest)
{
$task = new Task($taskRequest->all);
$task->save();
return response()->json($task->id);
}
I did what you said, but I getUndefined index: 'AppTask'
when I try to show the index page, basically the page that shows all the posts for a certain user.
– Andrei Hirbu
Nov 25 '18 at 12:33
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53466978%2fcant-associate-an-users-id-to-a-task-larvel%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
First, you should use request classes for validation of requests. So, your store method would look like:
public function store(TaskRequest $taskRequest){}
Second, add the following in your task model.
protected $fillable = [
'text'
];
public static function boot()
{
static::creating(function($model){
$model['user_id'] = Auth::user()->id;
});
}
Third, your store method would like this.
public function store(TaskRequest $taskRequest)
{
$task = new Task($taskRequest->all);
$task->save();
return response()->json($task->id);
}
I did what you said, but I getUndefined index: 'AppTask'
when I try to show the index page, basically the page that shows all the posts for a certain user.
– Andrei Hirbu
Nov 25 '18 at 12:33
add a comment |
First, you should use request classes for validation of requests. So, your store method would look like:
public function store(TaskRequest $taskRequest){}
Second, add the following in your task model.
protected $fillable = [
'text'
];
public static function boot()
{
static::creating(function($model){
$model['user_id'] = Auth::user()->id;
});
}
Third, your store method would like this.
public function store(TaskRequest $taskRequest)
{
$task = new Task($taskRequest->all);
$task->save();
return response()->json($task->id);
}
I did what you said, but I getUndefined index: 'AppTask'
when I try to show the index page, basically the page that shows all the posts for a certain user.
– Andrei Hirbu
Nov 25 '18 at 12:33
add a comment |
First, you should use request classes for validation of requests. So, your store method would look like:
public function store(TaskRequest $taskRequest){}
Second, add the following in your task model.
protected $fillable = [
'text'
];
public static function boot()
{
static::creating(function($model){
$model['user_id'] = Auth::user()->id;
});
}
Third, your store method would like this.
public function store(TaskRequest $taskRequest)
{
$task = new Task($taskRequest->all);
$task->save();
return response()->json($task->id);
}
First, you should use request classes for validation of requests. So, your store method would look like:
public function store(TaskRequest $taskRequest){}
Second, add the following in your task model.
protected $fillable = [
'text'
];
public static function boot()
{
static::creating(function($model){
$model['user_id'] = Auth::user()->id;
});
}
Third, your store method would like this.
public function store(TaskRequest $taskRequest)
{
$task = new Task($taskRequest->all);
$task->save();
return response()->json($task->id);
}
answered Nov 25 '18 at 11:39
Jamal Abdul NasirJamal Abdul Nasir
1,29331639
1,29331639
I did what you said, but I getUndefined index: 'AppTask'
when I try to show the index page, basically the page that shows all the posts for a certain user.
– Andrei Hirbu
Nov 25 '18 at 12:33
add a comment |
I did what you said, but I getUndefined index: 'AppTask'
when I try to show the index page, basically the page that shows all the posts for a certain user.
– Andrei Hirbu
Nov 25 '18 at 12:33
I did what you said, but I get
Undefined index: 'AppTask'
when I try to show the index page, basically the page that shows all the posts for a certain user.– Andrei Hirbu
Nov 25 '18 at 12:33
I did what you said, but I get
Undefined index: 'AppTask'
when I try to show the index page, basically the page that shows all the posts for a certain user.– Andrei Hirbu
Nov 25 '18 at 12:33
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53466978%2fcant-associate-an-users-id-to-a-task-larvel%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown