I have a problem with this error:
https://pastebin.com/cgm5yq0P
This is my form:
https://pastebin.com/bSU5X5EC
This is my web.php (route controller):
Route::post('/komentarz', 'VideosController#postComment');
This is my VideoController postComment function:
https://pastebin.com/SYbEjB8H
This is my Comment model:
https://pastebin.com/SxHX6gTP
This is my User model comments function:
public function comments() {
return $this->hasMany('App\Comment');
}
This is my Video model comments function:
public function comments(){
return $this->belongsToMany('App\Comment')->withTimestamps();
}
And finally, Migration file named create_comments_table:
https://pastebin.com/2cHscQfq
My Routes:
https://pastebin.com/ki8FZ0C6
Please help me with this.. I dunno what is wrong
Your foreign keys are wrong. Change your migration to this.
$table->unsignedInteger('user_id');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->unsignedInteger('video_id');
$table->foreign('video_id')->references('id')->on('videos')->onDelete('cascade');
Edit : This adds the comment and attaches it to the video and user.
// model fillable property
protected $fillable = [
'text', 'user_id', 'video_id',
];
// route
Route::post('/film/{id}/komentarz', 'VideosController#postComment');
// controller method
public function postComment(CreateCommentRequest $request, $id)
{
$video = Video::findOrFail($id);
$video->comments()->create([
'text' => $request->text,
'user_id' => auth()->id(),
]);
return 'works';
}
// form
{!! Form::open(['url' => "/film/{$video->id}/komentarz", 'method' => 'post','class' => 'form-horizontal']) !!}
<div class="form-group">
<div class="col-md-3 control-label">
{!! Form::label('text', 'Treść komentarza:') !!}
</div>
<div class="col-md-8">
{!! Form::textarea('text', null, ['class'=>'form-control', 'size' => '5x5']) !!}
</div>
</div>
<div class="form-group">
<div class="col-md-8 col-md-offset-3">
{!! Form::submit('Dodaj', ['class'=>'btn btn-primary btn-comment']) !!}
</div>
</div>
{!! Form::close() !!}
in comment model i see 'user' in fillable, not 'user_id'. i think it should be user_id. or you could use
protected guarded = [];
instead of using fillable array.
Related
When trying to store an entry in my Database using Eloquent ORM i am getting the error,
Illuminate\Database\QueryException thrown with message "SQLSTATE[HY000]: General error: 1364 Field 'name' doesn't have a default value (SQL: insert into `animals` (`updated_at`, `created_at`) values (2019-04-25 22:35:47, 2019-04-25 22:35:47))"
However in my migrations i never specify name as being default as it is required.
My AnimalsController class.
public function create()
{
return view('animals.create');
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$animal = Animal::create([
$request->input()
]);
// $animal = new Animal;
// $animal->name = $request->name;
// $animal->dob = $request->dob;
// $animal->description = $request->description;
// $animal->image = $request->image;
// $animal->available = $request->available;
//
// echo $request->id;
// echo "test";
// echo $animal->id;
// flash('Animal Added')->success();
return redirect()->route('animals.show')->with('animal', $animal);
}
/**
* Display the specified resource.
*
* #param \App\Animal $animal
* #return \Illuminate\Http\Response
*/
public function show(Animal $animal)
{
return view('animals.show')->with('animal', $animal);
}
Animal.php
class Animal extends Model
{
use SoftDeletes;
protected $dates = [
'created_at',
'updated_at'
];
/**
* Set the Attributes which are alllowed
* to be assigned
*/
protected $fillable = [
'name',
'dob',
'description',
'image',
'available'
];
create.blade.php - Where the form data is entered
<div class="row">
<div class="col">
{!! Form::open(['route' => 'animals.store'], ['class' => 'form', 'files' => true]) !!}
<div class="form-group">
{!! Form::label('name', 'Animal Name', ['class' => 'control-label']) !!}
{!! Form::text('name', null, ['class' => 'form-control input-lg', 'placeholder' => 'Waffles']) !!}
</div>
<div class="form-group">
{!! Form::label('dob', "DOB", ['class' => 'control-label']) !!}
{!! Form::date('dob') !!}
</div>
<div class="form-group">
{!! Form::label('description', "Description", ['class' => 'control-label']) !!}
{!! Form::textarea('description', null, ['size' => '20x3', 'class' => 'form-control input-lg', 'placeholder' => 'Describe the animal']) !!}
</div>
<div class="form-group">
{!! Form::label('image', "Image", ['class' => 'control-label']) !!}
{!! Form::file('image') !!}
</div>
<div class="form-group">
{!! Form::label('available', "Available", ['class' => 'control-label']) !!}
{!! Form::checkbox('available', 'value') !!}
</div>
<div class="form-group">
{!! Form::submit('Add Animal', ['class' => 'submit btn btn-info btn-lg', 'style' => 'width: 100%']) !!}
</div>
{!! Form::close() !!}
</div>
CreateAnimalsTable migration
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateAnimalsTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('animals', function (Blueprint $table) {
$table->increments('id');
$table->string('name', 20);
$table->date('dob');
$table->mediumText('description');
$table->string('image');
$table->boolean('available')->default(false);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('animals');
}
}
And in case it's worth anything my show.blade.php file.
<div class="container">
<div class="row">
<div class="col-xl-12 text-center">
<p class="text-primary" style="margin-top:100px;">{{ $animal->name }} has animal ID #{{ $animal->id }}. Date: {{ $animal->dob }}</p>
</div>
</div>
</div>
I have played around for hours trying to get this to work and i am painfully blind to the solution.
I should probably also mention that if i use this code
public function store(Request $request)
{
// $animal = Animal::create([
// $request->input()
// ]);
$animal = new Animal;
$animal->name = $request->name;
$animal->dob = $request->dob;
$animal->description = $request->description;
$animal->image = $request->image;
$animal->available = $request->available;
echo $request->id;
echo "test";
echo $animal->id;
// flash('Animal Added')->success();
return redirect()->route('animals.show')->with('animal', $animal);
}
I instead get this error.
Illuminate\Routing\Exceptions\UrlGenerationException thrown with message "Missing required parameters for [Route: animals.show] [URI: animals/{animal}]."
Which i have also had no use fixing, i am using a resource in the web.php file
Route::resource('animals', 'AnimalsController');
This led me to believe that $fillable was not working as intended and instead used the above method, however it still didn't work.
If i do make Name default in the Schema i will then have to give every other attribute a default value, which i don't want to do.
Hopefully i have given enough information to be of use, thank you :D
I found two issues in this.
First, I dont think this is gonna work
$animal = Animal::create([
$request->input()
]);
This is an array wrapped inside another array. Instead, lets write:
$animal = Animal::create($request->input());
Or:
$animal = Animal::create($request->all());
In the second example, I think an animal is created, but redirected route is not specified correctly:
return redirect()->route('animals.show')->with('animal', $animal);
Let's say:
return redirect()->route('animals.show', $animal->id);
I am new to laravel and I am trying to update to profile of the user who logged in. The error says:
Class App\Http\Request\UpdateApplicantRequest does not exist
But I imported it in the top of my controller.
HomeController.php
<?php
namespace App\Http\Controllers\Applicant;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Repositories\ApplicantsRepository;
use Flash;
use Response;
use App\Models\Applicant;
use App\Http\Request\UpdateApplicantRequest;
class HomeController extends Controller
{
private $applicantRepository;
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct(ApplicantsRepository $applicantRepo)
{
$this->middleware('applicant');
$this->applicantsRepository = $applicantRepo;
}
/**
* Show the application dashboard.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
return view('applicant-dashboard.home');
}
public function edit($id)
{
//$applicant = $this->applicantRepository->findWithoutFail($id);
$applicant = Applicant::where('id',$id)->get()->last();
if (empty($applicant)) {
Flash::error('Applicant');
return redirect(route('applicant.home'));
}
return view('applicant-dashboard.edit')->with('applicant', $applicant);
}
public function update($id, UpdateApplicantRequest $request)
{
$applicant = $this->applicantRepository->findWithoutFail($id);
if (empty($applicant)) {
Flash::error('Applicant not found');
return redirect(route('applicant.home'));
}
$input = $request->all();
$applicant = $this->applicantRepository->update([
'name' => $input['name'],
'email' => $input['email'],
'password' => bcrypt($input['password']),
'address' => $input['address'],
'cellphone_no' => $input['cellphone_no']], $id);
Flash::success('Profile updated successfully.');
return redirect(route('applicant.home'));
}
}
Here is the code in my routes:
Route::get('/edit/{id}', 'HomeController#edit')->name('applicant.edit');
Route::patch('/put', 'HomeController#update')->name('applicant.update');
and the code in my blade file:
#section('content')
<section class="content-header">
<h1>
Applicant Profile
</h1>
</section>
<div class="content">
{{-- #include('adminlte-templates::common.errors') --}}
<div class="box box-primary">
<div class="box-body">
<div class="row" style="padding-left: 20px">
{!! Form::model($applicant, ['route' => ['applicant.update', $applicant->id], 'method' => 'patch']) !!}
#include('applicant-dashboard.fields')
{!! Form::close() !!}
</div>
</div>
</div>
</div>
#endsection
I am looking for help.
Thanks in Advance.
Firstly, you use wrong namespace of UpdateApplicantRequest.
Change:
use App\Http\Request\UpdateApplicantRequest;
To:
use App\Http\Requests\UpdateApplicantRequest;
Then, change your form from:
{!! Form::model($applicant, ['route' => ['applicant.update', $applicant->id], 'method' => 'patch']) !!}
#include('applicant-dashboard.fields')
{!! Form::close() !!}
to:
{!! Form::model($applicant, ['route' => ['applicant.update', $applicant->id], 'method' => 'post']) !!}
{!! method_field('patch') !!}
#include('applicant-dashboard.fields')
{!! Form::close() !!}
I created a form for myself and it was working perfect. I did some rules like: if the user haven't wrote anything in the form or haven't wrote enough, give me some error message. This was working while used the 'former' package instead of the normally used 'form' package. Now I changed my form from "Former" to "Form" and now my error message are gone.. Well my form works and the rules too. If I don't write anything in my form or not at least 3 characters in the title/content section, it should redirect me with the errors I need. This works, but without the error message.
Here is my form:
#extends('master')
#section('content')
{!! Form::open(array('action' => 'Test\\TestController#store')) !!}
<div class="form-group">
{!! Form::label('thread', 'Title:') !!}
{!! Form::text('thread', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('content', 'Body:') !!}
{!! Form::textarea('content', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::submit('Add Thread', ['class' => 'btn btn-primary form-control']) !!}
</div>
{!! Form::close() !!}
#stop
My Controller#store function:
public function store(StoreRequest $request){
Thread::create($request->all());
return redirect(action('Test\\TestController#index'));
}
my Model:
<?php
namespace App\Models\Thread;
use Illuminate\Database\Eloquent\Model;
class Thread extends Model {
public $table = 'thread';
public $fillable = [
'thread',
'content',
];
}
and now the rules ( StoreRequest.php ) :
<?php
namespace App\Http\Requests\Store;
use App\Http\Requests\Request;
class StoreRequest extends Request {
public function rules() {
return [
'thread' => 'required|min:3',
'content' => 'required|min:3'
];
}
public function authorize() {
return true;
}
}
The error message was getting automaticly generated. Laravel did that for me. But not anymore.
Does anybody see why?
You can check if the form returned any error with something along these line:
#if ($errors->has('name')) <p class="help-block">{{ $errors->first('name') }}</p> #endif
Or you could use BootForm and let it handle everything for you
I'm new to laravel and trying to learn using it. I created a little project for threads. Nothing special. The thing is, the function to edit something doesn't work and I cant see why. Maybe someone of you see the mistake?
thats my Routes:
Route::get('/index', 'Test\\TestController#index');
Route::get('/add', 'Test\\TestController#add');
Route::post('/test', 'Test\\TestController#store');
Route::get('/show/{id}', 'Test\\TestController#show');
Route::get('/show/{id}/edit', ['as' => 'edit', 'uses' => 'Test\\TestController#edit']);
Route::put('/show/{id}/edit', ['as' => 'editing', 'uses' => 'Test\\TestController#update']);
thats the important parts of the edit method:
public function edit($id) {
$thread = Thread::query()->findOrFail($id);
return view('test.edit', [
'thread' => $thread
]);
}
public function update($id, StoreRequest $request) {
$thread = Thread::query()->findOrFail($id);
$thread->fill($request->all());
$thread->save();
return redirect(action('Test\\TestController#show', [$thread->id]));
}
}
show.blade
#extends('master')
#section('content')
<div class="panel panel-primary">
<div class="panel-heading">
<div class="panel-title">
{{ $thread->thread }}
</div>
</div>
<div class="form-body">
<div class="form-group"><br>
<ul>
{{$thread->content }}<br><br>
{{$thread->created_at->format('d.m.Y H:i:s')}}
</ul>
</div>
</div>
<div class="panel-footer">
<div class="btn btn-primary">Thread bearbeiten</div>
</div>
</div>
#stop
edit blade ( formular where the text I want to add is in the textbox and the save button )
#extends('master')
#section('content')
{!! Former::horizontal_open()->method('PUT')->action(action("Test\\TestController#update", [$thread->id])) !!}
{!! Former::populate($thread) !!}
{!! Former::text('thread')->label('Thread') !!}
{!! Former::text("content")->label("Content") !!}
{!! Former::large_primary_submit('Save!') !!}
{!! Former::close() !!}
#stop
model:
<?php
namespace App\Models\Thread;
use Illuminate\Database\Eloquent\Model;
class Thread extends Model {
public $table = 'thread';
public $fillable = [
'thread',
'content',
];
}
ERROR MESSAGE :
No query results for model [App\Models\Thread\Thread].
In my ecommerce project, I have Product and Carousel Model.
Product.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Product extends Model
{
use SoftDeletes;
protected $dates = ['deleted_at'];
protected $fillable = [
'code', 'name', 'description', 'special_note', 'sort', 'display', 'weight', 'enquiry'
];
public function carousels()
{
return $this->belongsToMany('App\Carousel')->withTimestamps();
}
}
Carousel.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Carousel extends Model
{
use SoftDeletes;
protected $dates = ['deleted_at'];
protected $fillable = ['name', 'display', 'sort'];
public function products()
{
return $this->belongsToMany('App\Product')->withTimestamps();
}
public function getProductListAttribute()
{
return $this->products->lists('id');
}
}
Here's the controller:
public function create()
{
$products = Product::lists('name', 'id');
return view('admin.carousels.create', compact('products'));
}
public function store(Request $request)
{
$carousel = Carousel::create($request->all());
$carousel->products()->attach($request->input('product_list'));
return redirect()->back();
}
public function edit($id)
{
$carousel = Carousel::findOrFail($id);
$products = Product::lists('name', 'id');
return view('admin.carousels.edit', compact('carousel', 'products'));
}
public function update(Request $request)
{
dd($request->all());
}
The carousel form:
<div class="col-md-4 col-sm-4">
<div class="form-group">
{!! Form::label('name', 'Name:') !!}
{!! Form::text('name', null, ['class' => 'form-control input-sm', 'id' => 'name']) !!}
</div>
</div>
<div class="col-md-4 col-sm-4">
<div class="form-group">
{!! Form::label('sort', 'Sort:') !!}
{!! Form::text('sort', null, ['class' => 'form-control input-sm', 'id' => 'sort']) !!}
</div>
</div>
<div class="col-md-4 col-sm-4">
<div class="form-group">
{!! Form::label('display', 'Display:') !!}
{!! Form::select('display', ['Disabled' => 'Disabled', 'Enabled' => 'Enabled'], null, ['class' => 'form-control input-sm', 'id' => 'display']) !!}
</div>
</div>
<div class="col-md-12 col-sm-12">
<div class="form-group">
{!! Form::label('product_list', 'Products:') !!}
{!! Form::select('product_list[]', $products, null, ['class' => 'form-control input-sm', 'multiple']) !!}
</div>
</div>
<div class="col-md-12 col-sm-12">
<div class="form-group">
{!! Form::submit($submitButtonText, ['class' => 'btn btn-primary btn-block m_top_15', 'id' => $submitButtonId]) !!}
</div>
</div>
Now, the problem is that, when I use form model binding to edit the carousel form, I get no product displayed in the select box, though I can see all the products to select from. The selected products are not seen for the particular carousel.
What is the mistake that I have made ? Kindly help me out.
Thanks.
P.S: I am using Laravel 5.1 and came across this issue for the first time. Earlier I used to do this in Laravel 5 without any issue.
This may be related to how lists() is handled in 5.1. From the upgrade guide -
The lists Method
The lists method now returns a Collection instance instead of a plain
array for Eloquent queries. If you would like to convert the
Collection into a plain array, use the all method:
User::lists('id')->all(); Be aware that the Query Builder lists method
still returns an array.