Array to string conversion error, Laravel - php

I'm trying to save to database my data, but I get error "Array to string conversion". It's my controller
public function create()
{
$listapacjentow = Patient::pluck('nazwisko','id');
return view('leczenie.create')->with('listapacjentow', $listapacjentow);
}
public function store(CreateOperacjaRequest $request)
{
$operacja = new Operacja($request->all());
$PacjenciIds = $request->input('PacjentList');
$operacja->user()->associate($PacjenciIds);
$operacja->save();
return redirect('leczenie');
}
It's my form
<div class="form-group">
<div class="col-md-4 control-label">
{!! Form::label('PacjentList','Wybierz pacjenta:') !!}
</div>
<div class="col-md-6">
{!! Form::select('PacjentList[]', $listapacjentow); !!}
</div>
When I return $operacja is everything ok, but I can't save it to database

Try with this approch in your store method.
$operacja = new Operacja($request->all());
$operacja->user_id = $request->input('PacjentList.0');
$operacja->save();
$operacja->user()->saveMany($patients);

If you want to associate only one user with Operacja, do this:
{!! Form::select('user_id', $listapacjentow); !!}
$operacja = Operacja::create($request->all());
If you want to add many users to the Operacja, you'll need to revert one to many relationship, so User will have operacja_id and Operacja will not have user_id.
If you want to make Operacja have many users and a user could have many Operacja, use many to many relationship.

Related

Property [X] does not exist on the Eloquent builder instance in laravel 9

I have a basic laravel 9 crud app.
There I have two tables in my db. companies and employees.
I'm trying to display single-user details on my employees.show blade
All my data is displayed correctly but when I try to print user's company, I'm kept getting the following error.
Property [company] does not exist on the Eloquent builder instance.
Following is my employee model
protected $fillable = [
'first_name', 'last_name', 'email', 'phone', 'company_id'
];
public function company()
{
return $this->belongsTo(Company::class);
}
and the following is the show function in my controller
public function show(Employee $employee)
{
$data = Employee::with('company')->where('id', '=', $employee->id);
return view('employees.show', compact('employee','data'));
}
and this is my show.blade
<div class="row mb-4">
<label class="col-sm-2 col-label-form"><b>Company</b></label>
<div class="col-sm-10">
{{ $data->company->name }}
</div>
</div>
How can I display the company properly...
I have company_id as a foreign key in my employees' table...
I have following when I dd $data
You're returning a QueryBuilder from the Employee query in your show() function. You want to add ->get() to return a collection or Employee records:
public function show(Employee $employee)
{
$data = Employee::with('company')->where('id', '=', $employee->id)->get();
return view('employees.show', compact('employee','data'));
}
I think the problem in Query for get data. In employees.show you only show one data then and in controller you got collection of data as array. So your single data element can't access because it is array and for execution you need to add foreach loop for that.
Here you need to change query method from get() to first() and it will resolve your issue.
Your show function code looks like this
public function show(Employee $employee)
{
$data = Employee::with('company')->where('id', '=', $employee->id)->first();
return view('employees.show', compact('employee','data'));
}
Your employees.show blade looks like this
<div class="row mb-4">
<label class="col-sm-2 col-label-form"><b>Company</b></label>
<div class="col-sm-10">
{{ $data->company->name }}
</div>
</div>

Property [name] does not exist in laravel

I want to show my product in a single page but I have "Property [name] does not exist on this collection instance." error
blade:
<div class="title">
<h2>{{ $singleproduct->name }}</h2>
</div>
<div class="single-product-price">
<h3>{{ $singleproduct->price }}</h3>
</div>
<div class="single-product-desc">
<p>{!! $singleproduct->explain !!} </p>
</div>
controller:
public function show()
{
$singleproduct = Singleproduct::get();
return view('UI.store.SingleProduct' , compact('singleproduct' ));
}
route:
Route::get('/singleproduct/{product}' , 'admin\SingleproductController#show');
You have a Collection of potentially many or no Singleproducts. If you only want 1 you would use first.
Most likely because this is a show route you want a specific Singleproduct, which I will guess you are passing an 'id' via the URL.
public function show($product)
{
$singleproduct = Singleproduct::findOrFail($product);
return view('UI.store.SingleProduct', compact('singleproduct'));
}
Now in the view you know that singleproduct is definitely an instance of Singleproduct and is accessible.
You need to select only one product not all of them,
so you can update your show method like this
using the route model binding you can read more about this awesome feature in laravel docs
public function show(Singleproduct $product)
{
return view('UI.store.SingleProduct' , compact('product' ));
}

List only rows related to another list using pluck()

So, I have three tables: farms, crops and harvests. When I want to register a harvest on my web page, I use pluck to display a dropdown list for farms and another dropdown list for crops in the same view. The thing is, when I select a farm from the dropdown, I can still see and select crops related to other farms, and das not good. How can I list only crops related to the previously selected farm? My HarvestController looks like this:
public function create()
{
$farms = Farm::where('users_id', Auth::id())->with('alias')->pluck('alias', 'id');
$crops = Crop::where('users_id', Auth::id())->pluck('id', 'id');
return view('harvest.create')
->with('farms', $farms)
->with('crops', $crops);
}
The relationships in models:
class Farm extends Model
{
public function crops(){
return $this->hasMany('App\Crop');
}
}
class Crop extends Model
{
public function farm(){
return $this->belongsTo('App\Farm');
}
public function harvest(){
return $this->hasOne('App\Harvest');
}
}
And the view looks like this:
<div class="form-group">
{!! Form::label('farms_id', 'Farm') !!}
{!! Form::select('farms_id', $farms, null, ['class' => 'form-control', 'required']) !!}
</div>
<div class="form-group">
{!! Form::label('crops_id', 'Crop ID') !!}
{!! Form::select('crops_id', $crops, null, ['class' => 'form-control', 'required']) !!}
</div>
Any help will be highly appreciated, constructive roasting too

Input field not updating in the database

I am working on a laravel 5.2 project. I am referencing category_id from Posts.
My Post model looks like
class Post extends Model
{
protected $fillable=['category_id '];
public function category(){
return $this->belongsTo('App\category');
}
}
Also, my form looks like
<div class="form-group">
{!! Form::label('category_id','Category:') !!}
{!! Form::select('category_id', $categories, '1',['class'=>'form-control']) !!}
</div>
My table data
#foreach($posts as $post)
<tr>
<td>{{$post->category['name']}}</td>
</tr>
#endforeach
The Problem is, whenever I create a new post, the category name doesn't save in the database nor does it show on the front end.
My Post Schema table
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->integer('category_id')->index()->unsigned()->nullable();
});
Even when I dd() the input field for the category id on the post table, its empty. Can someone help me with this, it's making me go crazy. Thanks
I am using a resource contoller,heres my create request controller
public function store(Request $request)
{
$user=Auth::user();
$input = $request->all();
Post::create($input);
return redirect('admin/posts');
}
It is because the fillable field only is the category_id try to add the category name field on your fillable like this.
//this are the fields that is fillable
protected $fillable=['category_id','name'];
With this the category name field will be able to catch and save data.
In continuation of my comment. Your migration should look like this.
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->integer('category_id')->index()->unsigned()->nullable();
$table->string('category_name')->nullable();
});
Your model,
class Post extends Model
{
protected $fillable=['category_id','category_name'];
}
Form
<div class="form-group">
{!! Form::label('category_id','Category:') !!}
{!! Form::select('category_id', $categories, '1',['class'=>'form-control']) !!}
{!! Form::input('category_name',old('category_name'),['class'=>'form-control', 'id' => "input_name"]) !!}
</div>
and lastly your table data should look like this.
#foreach($posts as $post)
<tr>
<td>{{$post->category_name}</td>
</tr>
#endforeach
Double check your fillable and input fields if they are the same.
your given concern you only show this one.
<div class="form-group">
{!! Form::label('category_id','Category:') !!}
{!! Form::select('category_id', $categories, '1',['class'=>'form-control']) !!}
</div>
how about the other fields. We expect you already had this on your form too.
<div class="form-group">
{!! Form::label('input_name','Name:') !!}
{!! Form::input('name',old('name'),['class'=>'form-control', 'id' => "input_name"]) !!}
</div>
In your store method it should look like this
$new_post = new Post;
$new_post->category_id = Input::get(‘category_id’);
$new_post->name = Input::get(‘name’);
$new_post->save();
Just in case you’re not using the fill method of $request
In order to access your relationship's attributes, you should do:
$post->comment->name;
And try to use 'fill()' when saving
$post = new Post;
$post->fill($request->all());
$post->save();
I fixed the error using this in my form.
{!! Form::select('category_id', [''=>'choose Categories'] + $categories,['class'=>'form-control']) !!}
and this on my table.
<td>{{$post->category['name']}}</td>
Thanks all for your effort.

Laravel 5.2 relationship one to one query

How i do the query to works like this example:$model->model2->attribute
<div class="form-group">
{!! Form::label('Route name') !!}
{!! Form::text('name', ( isset($climb->route->name) ? $climb->route->name : null ), array('class'=>'form-control' )) !!}
</div>
You could try using your model in the view as in a dictionary, add something like this in your controller.
$model = Model::find($id);
$model['model2'] = $model->model2;
return view('your_view', ['model' => $model]);
For this I assume you already prepared your relationship in your model, doing that for your real models should make the view work this way
<div class="form-group">
{!! Form::label('Route name') !!}
{!! Form::text('name', ( isset($climb['route']['name']) ? $climb['route']['name'] : null ), array('class'=>'form-control' )) !!}
</div>
Create a relation:
Having:
class Comment extends Model
{
/**
* Get the post that owns the comment.
*/
public function post()
{
return $this->belongsTo('App\Post');
Then thou shall call lika:
$comment = App\Comment::find(1);
echo $comment->post->title;
https://laravel.com/docs/5.2/eloquent-relationships#one-to-many
It's one many, not one one I think

Categories