Trying to get property 'deskripsi' of non-object (View: - php

#extends('layouts.admin')
#section('content')
#foreach($galerys as $data)
<form method="post" enctype="multipart/form-data" action="{{url('/ubahGaleryPost')}}">
{{ csrf_field() }}
<h4>Ubah Gambar</h4>
<input name="id" id="id" type="hidden" value="{{$data->id}}" >
<h5>Gambar</h5>
<input name="gambar" id="gambar" type="file">
<h5>Deskripsi</h5>
<input name="deskripsi" id="deskripsi" class="form-control" type="text" value="{{$data->deskripsi}}">
<button type="submit" class="btn btn-primary">Ubah</button>
</form>
#endforeach
#endsection
controller
public function ubahGalery($id){
$data = Galery::findOrFail($id);
return view('admin.updategalery',['galerys'=>$data]);
}
public function ubahGaleryPost(Request $request){
$data = Galery::where('id',$request->id)->first();
if (empty($request->file('gambar'))){
$data->gambar = $data->gambar;
}
else{
unlink('img/galery/'.$data->gambar);
$file = $request->file('gambar');
$ext = $file->getClientOriginalExtension();
$newName = rand(100000,1001238912).".".$ext;
$file->move('img/galery',$newName);
$data->gambar= $newName;
}
$data->deskripsi = $request->deskripsi;
$data->update();
return redirect('/galeryAdmin');
}

$data = Galery::findOrFail($id); this will return Galery object instead of a Collection of Galery (except if $id is an array).
So in your HTML you don't need a foreach and instead could just do $data->id, $data->deskripsi etc.

You don't need to loop on galerys as you are fetching only one result so you don't need to loop on your result so instead of that just fetch data directly.
Like this,
$galerys->deskripsi
Hope this helps:)

Related

Pass id from controller index to store - Laravel

This is my route :
Route::post('/store', 'EditlinkController#store');
My controller
public function index()
{
$id = $_GET['id'];
$links = DB::table('slugs')->where('slugs.id',$id)
->join('links','links.sid','=','slugs.id')
->get();
return view('editlink', ['links' => $links]);
}
public function store(Request $request, $id)
{
$url = $request->input('url');
$data =new link;
$sid = $id;
$data->sid = $sid;
$data ->url = $url;
$data ->save();
return redirect('/links');
}
And my view:
<form role="form" method='POST' action="{{url('store')}}">
<div class="entry input-group col-xs-3">
<input class="form-control" name="url" type='url' placeholder="https://..." size="100"/>
<input type="hidden" name="_Token" value="{{ csrf_token() }}">
<button type="submit" class="btn btn-primary" type="button">
<span class="material-icons md-12">add</span>
</button>
{{ csrf_field() }}
</div>
</form>
So basically here I want to call $id from index to store. I also have tried
Route::post('/store/{id}', 'EditlinkController#store');
But still doesn't work. Thank you.
Your route, /store/{id}, requires you to have a route parameter. Make sure you have an $id available to you before you generate your url for your form.
An example of what your open form tag should look like with the $id included:
<form role="form" method='POST' action="{{url('store', $id)}}">
I'm assuming the view editlink is where the markup for the form resides. If so, then you can simply pass the value of the id to your view from your controller:
return view('editlink', ['links' => $links, 'id' => $id]);

What to do when a declare array variable is undefined at view but working with controller laravel 5.7

Here is the code:
public function chat($id=1){
Route::view('/chat', 'chat');
$id = View::make('chat.blade', ['reviewer_id' => Reviewer::findOrFail($id)]);
$audiences = DB::table('audience')->get();
$data = [
'id'=>$id,
'audiences'=>$audiences,
'audience_id'=> 2
];
return View::make('chat.blade', ['data'=>$data]);
}
As the code is simple I route to blade view, get data from database, get audience data, initialize data array return data to chat.blade simple code but in view
Undefined variable: data (View:
/Users/userinfo/Sites/chat/resources/views/chat.blade.php)
View code:
<div>
#foreach($data->audiences as $info->audience)
{{$info->audience->id}};
#endforeach
</div>
<div>
<form action="/" method="post">
<input type="hidden" value={{$reviewer_id}} name="id">
<input type="hidden" value={{$audience_id}} name="id">
<input type="text" name="message">
<input type="submit" value="submit">
</form>
</div>
<?php $__currentLoopData = $data->audiences; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $info->audience): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
<?php echo e($info->audience->id); ?>;
<?php endforeach; $__env->popLoop(); $loop =
$__env->getLastLoop();
?>
> undefined variable $data
change $data->audiences to $data['audiences'] in your view. $data is an array not an object
If i do it then i do it like this
public function chat($id = 1){
Route::view('/chat', 'chat'); // I don't know what that is
$reviewer_id = Reviewer::findOrFail($id); // or Reviewer::find($id);
//I Update this little bit : $audiences = DB::table('audience')->get();
$audiences = Audience::all();
$audience_id = 2 ;
return view('chat.blade', compact(['id','audiences','audience_id','reviewer_id']));
}
Now you can access all the variables passed in compact in your blade file like this
<div>
// Depends on what is in the $audiences could be with "$key => $value"
#foreach($audiences as $key)
{{$key->id}};
#endforeach
</div>
<div>
<form action="/" method="post">
<input type="hidden" value={{$reviewer_id}} name="id">
<input type="hidden" value={{$audience_id}} name="id">
<input type="text" name="message">
<input type="submit" value="submit">
</form>
</div>

Laravel- arranging records in ascending and descending order

I have a create method in my controller
public function create()
{
$image = PropertyUser::where('user_id', '=', Auth::user()->id)->get();
foreach($image as $property)
{
$id = $property->property_id;
}
$image_main = Image::where('property_id', $id)->get();
return view('settings.photos', ['image_array' => $image_main]);
}
This is my photos.blade file
<form name="asc" action="{{route("settings.photos")}}" method="post" class="text-center">
#csrf
<input type="submit" value="Ascending " class="settings-photos-header2 text-center"/> |
</form><form name="dec" action="{{route("settings.photos")}}" method="post" class="text-center">
#csrf
<input type="submit" value= " Descending" class="settings-photos-header2 text-center"/>
</form>
<h2 class="settings-photos-header2 text-center">Photo Gallery</h2>
#foreach ($image_array as $images)
<div class="image-warp"><img src="{{$images->filename}}"
style="width:100px;height:100px;"><br/><span style="color: #1b1e21">{{$images->description}}</span>
</form>
</div>
#endforeach
Question- How can i make the asc button sort the images in ascending order and des in descending order, is there a way to connect it to my controller, or is there a way to sort them in ascending and descending order through any other means?
There are many ways you can achieve the sorting issue but as you have setup your page let me tell you the way you can sort records. Create another action which point to your settings.photos route
public function settings_photos($property_id) {
$form = Input::get('submit');
$orderBy = $form == "Ascending" ? "asc" : "desc";
# Fetch Images in of Specific Property
$list_of_images = Image::where('property_id', $property_id)
# Order by Asc/Desc
->sortBy('id', $orderBy)->get();
return view('settings.photos', ['image_array' => $list_of_images]); }
Now add Image->id to pass in your controller action by form
https://stackoverflow.com/questions/50432659/laravel-arranging-records-in-ascending-and-descending-order#
<form name="asc" action="{{route("settings.photos")}}" method="post" class="text-center">
#csrf
<input type="image_id" value="{{$id}}" />
<input type="submit" value="Ascending " class="settings-photos-header2 text-center"/> |
</form>
<form name="dec" action="{{route("settings.photos")}}" method="post" class="text-center">
#csrf
<input type="image_id" value="{{$id}}" />
<input type="submit" value="Descending" class="settings-photos-header2 text-center"/>
</form>
Now make some changes to your previous Controller code
public function create()
{
$image = PropertyUser::where('user_id', '=', Auth::user()->id)->get();
foreach($image as $property)
{
$id = $property->property_id;
}
$image_main = Image::where('property_id', $id)->get();
return view('settings.photos', ['image_array' => $image_main, 'id' => $id]);
}
As I told you there are various way to sort records even by Ajax but I suggest you using your code example. I also did not optimize the code.
Let me know if you have any question.
Thanks

Unable to submit binary image into database in laravel

I am trying to submit an image into the database but I keep getting this error: Type error: Argument 1 passed to Illuminate\Database\Eloquent\Relations\HasOneOrMany::save() must be an instance of Illuminate\Database\Eloquent\Model, null given, called in C:\xampp\htdocs\Evaluation\app\Http\Controllers\ImageController.php on line 24.
I checked with other questions in StackOverflow and mostly they said it was the fault of the saving part where they put something like this, $post but I have checked and there is nothing wrong with it. The relationship doesn't seem to have any problem as well but why is it still not working? The error also return me a null when I upload image. The null is returning at the part here, $UserImage = $request->input('UserImage'); So could my problem be in image1.blade.php?
ImageController:
public function test(personal_info $user){
return view('image1',compact('user'));
}
public function test1(Request $request){
$UserImage = new Image;
$personal_info = new personal_info;
$UserImage = $request->input('UserImage');
$id = $request->user_id;
$id = personal_info::find($id);
$id->Images()->save($UserImage);
return redirect('/summary');
}
image1.blade.php (where i submit the form)
<form class="form-horizontal" method="post" action="{{ url('/Upload')}}" enctype="multipart/form-data">
{{ csrf_field() }}
<input type="hidden" name="user_id" value="{{$user->id}}">
<div class="form-group">
<label for="imageInput" class="control-label col-sm-3">Upload Image</label>
<div class="col-sm-9">
<input type="file" name="UserImage">
</div>
</div>
<div class="form-group">
<div class="col-md-6-offset-2" style="padding-left: 30px">
<input type="submit" class="btn btn-primary" value="Save">
</div>
</div>
</form>
Image.php:
public function personal_infos() {
return $this->belongsTo('App\personal_info', 'user_id', 'id');
}
personal_info.php:
public function Images() {
return $this->hasOne('App\Image','user_id');
}
public function test1(Request $request)
{
// make new instance of Image Model
$imageModel = new Image;
// find personal_info Model by id
$personal_info = personal_info::findOrFail($request->input('user_id'));
// UploadedFile
$image = $request->file('UserImage');
// get the file contents?
$imageModel->content = ...
// save the relationships, pass a model instance to `save`
$personal_info->Images()->save($imageModel);
return redirect('/summary');
}

saving check box values to the database in laravel

I'm new to laravel. I'm saving some checkbox value to the database using loop the loop work but it only saves the last value in the array to the database.
this is my form
<form action="{{url('resortf')}}" method="post" enctype="multipart/form-data">
<input hidden name="h_id" value="{{ $hotel->id}}">
#foreach($facility as $facilities)
<div class="col-md-4">
<img src="{{$facilities->image}}" width="50px" height="50px;">
<label>{{$facilities->name}}</label>
<input type="checkbox" value="{{$facilities->id}}" name="facilities[]">
</div>
#endforeach
<div class="row">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="submit" class="btn btn-success" value="Next">
<input type="reset" class="btn btn-success" value="Reset">
</div>
</form>
form working fine; $facilities and $hotel are passed from the controller.
this is the store function
public function store(Request $request) {
$resortfacility = new Resortfacility;
$loop = $request->get('facilities');
foreach ($loop as $value){
$resortfacility->h_id = $request->get('h_id');
$resortfacility->f_id = $value;
$resortfacility->save();
}
}
is there any other way to do this that works?
Your problem occurs because you create one instance of Resortfacility, and then you fill in its values and save. The problem is, every time you perform changes to this object in a loop, you are updating existing object and that's why there is only one record in your database, the last one from the loop.
Try making new instance of Resortfacility inside the loop like this:
public function store(Request $request) {
$loop = $request->get('facilities');
foreach ($loop as $value){
$resortfacility = new Resortfacility;
$resortfacility->h_id = $request->get('h_id');
$resortfacility->f_id = $value;
$resortfacility->save();
}
}
The other answer is correct but, bulk insertion might be helpful, as creating new object within foreach loop will make query for every record.
public function store(Request $request)
{
$facilities = $request->get('facilities');
$data=array();
foreach($facilities as $facility)
{
$data[] =[
'f_id' => $facility,
'h_id' => $request->get('h_id'),
];
}
}
Resortfacility::insert($data);

Categories