Data retrieving issue in Laravel - php

I'm trying display some data in my blade. Following is my current code for the controller's(ActivateController.php) index function,
public function index($id)
{
//echo $id;
$datas = Website::WHERE('appId','=',$id);
//dd($datas);
return view('activities.index',compact('datas','id'));
}
And my view url is something like,
TEST.site/activities/index/12
this is my blade, index.blade.php
#extends('layouts.admin')
#section('content')
<div class="container">
<div class="row">
<div class="col-sm">
<div class="pull-left">
<h2 class="mt-2">Activate Website</h2>
</div>
<div class="pull-right">
<a class="btn btn-primary" href="{{ route('websites.index') }}"> Back</a>
</div>
</div>
</div>
</div>
#if (count($errors) > 0)
<div class="container ">
<div class="row">
<div class="col-sm">
<div class="alert alert-danger mt-2">
<strong>Whoops!</strong> There were some problems with your input.<br><br>
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
</div>
</div>
</div>
#endif
<div class="container mt-3">
<div class="row">
<div class="col-sm">
#foreach ($datas as $object)
<form id="w1" action="{{ route('activities.update',$object->appId) }}" method="POST">
#csrf
#method('PUT')
<strong>Select Package Type:<span class="star">*</span></strong>
<input type="text" name="app_domain" value="{{$object->domain}}">
<strong>Select Package Type:<span class="star">*</span></strong>
<select id="app-packagetype" class="form-control" name="package_type" aria-required="true">
<option value="{{$object->payment_option }}">{{$object->payment_option }}</option>
<option value="monthly">Monthly [12]</option>
<option value="yearly">Yearly [99]</option>
</select>
<br>
<div class="form-group text-right">
<button type="submit" class="btn btn-success">{{ __('Activate') }}</button>
</div>
</form>
#endforeach
</div>
</div>
</div>
#endsection
But now my issue is , when I try to retrieve the data,
{{$object->appId}}
It retrieves empty....
Where I'm doing wrong and how to retrieve the data properly to the view?

you are missing get() at the end
change this:
$datas = Website::where('appId','=',$id);
to
$datas = Website::where('appId','=',$id)->get();
Thanks..

You use just like this
$datas = Website::where('appId',$id)->get();

Related

Laravel 7: User Profile causing bug when uploading img and bio description

In a laravel 7 app I'm working on a feature where a user can upload their image profile and bio description. A problem that I'm facing is that there are certain cases when a user sets their profile image, the bio description would be gone. For example: If a user set their profile img and puts a description, but if the user sets another img then the description would be gone. Not quite sure what's the bugs that's causing it. (I'm using bootstrap modal for the user to edit their profile.)
controller.php
public function modalEditPost(Request $request)
{
$user = Auth::user();
$avatarName = "";
$userBio = "";
$request->validate([
'bio' => 'nullable|string|max:255',
'image' => 'mimes:jpeg,png,jpg,gif,svg|max:2048',
]);
if ($request->has('bio')) {
$userBio = $request->bio;
$user->bio = $userBio;
}
if ($request->hasFile('image')) {
if ($request->file('image')->isValid()) {
$extension = $request->image->extension();
$avatarName = $user->id.'_avatar'.time().'.'.$extension;
$request->image->move(public_path('/uploads/avatars'), $avatarName);
$user->avatar = $avatarName;
}
}
$user->save();
return back()
->with('success','You have successfully edited your bio.')
->with('bio', $userBio)
->with('image', $avatarName);
}
index.blade.php
#include('partials.popup')
<div class="col-md-4">
<div class="card card-user">
<div class="card-body">
<p class="card-text">
<div class="author">
<div class="block block-one"></div>
<div class="block block-two"></div>
<div class="block block-three"></div>
<div class="block block-four"></div>
<img class = "avatar" src="/uploads/avatars/{{ Auth::user()->avatar}}" alt="">
<h4 class="title">{{ auth()->user()->first_name }} {{ auth()->user()->last_name }}</h5>
</div>
<div class="card-description">
{{ Auth::user()->bio }}
</div>
<br>
<div class="text-center">
<button class="btn btn-success"
style="cursor: pointer"
data-toggle="modal"
data-target="#popupModal">{{ __('Edit Profile') }}
</button>
</div>
</div>
</div>
popup.blade.php
<div class="modal-body">
<!-- (Image upload) Start -->
#if ($message = Session::get('success'))
<div class="alert alert-success alert-block">
<button type="button" class="close" data-dismiss="alert">×</button>
<strong>{{ $message }}</strong>
</div>
#endif
#if (count($errors) > 0)
<div class="alert alert-danger">
<strong>Whoops!</strong> There were some problems with your input.
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
<!-- (Image upload) Start -->
<form action="{{ route('modal.upload.post') }}" method="POST" enctype="multipart/form-data">
#csrf
<div class="row">
<div class="col-md-6">
<input type="file" name="image" class="form-control-file">
</div>
</div>
<br>
<div class="row">
<div class="col">
<p>Change Bio</p>
</div>
</div>
<div class="row">
<div class="col">
<input type="text" style="color:black" name="bio" class="form-control">
</div>
</div>
<br>
<div class="col-md-6">
<button type="submit" class="btn btn-success">Update</button>
</div>
</form>
<!-- (Image upload) End -->
</div>

Display another laravel blade view as a modal in a page

Good day everyone,
please, I am new to Laravel, I am creating a project management app, I created an index page for projects and I have a button where I can be able to create new projects, it is entirely another blade file, I need to display the blade as a modal, I tried using #include and bootstrap modal, but the index page is the one displaying in the modal popup instead of the create page, I don't know where I am getting it wrong, I need help because there is a lot of places I need to use the modal.
This is my index page
#extends('layouts.app')
#section('content')
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-right">
<a class="btn btn-success" data-toggle="modal" href="{{ route('projects.create') }}" data-target="#createModal"> New Project</a>
</div>
</div>
</div>
#if ($message = Session::get('success'))
<div class="alert alert-success">
<p>{{ $message }}</p>
</div>
#endif
<table class="table table-bordered table-hover">
<thead class="text-primary">
<tr>
<th>No</th>
<th>Name</th>
<th>Description</th>
<th>Date Created</th>
<th>Action</th>
</tr>
</thead>
#foreach ($projects as $project)
<tbody class="table-striped">
<tr>
<td>{{ ++$i }}</td>
<td>{{ $project->name }}</td>
<td>{{ $project->introduction }}</td>
<td>{{ $project->created_at->format('d/m/Y') }}</td>
<td>
<form action="{{ route('projects.destroy',$project->id) }}" method="POST">
<a class="fas fa-eye fa-lg text-warning mr-1" href="{{ route('projects.show',$project->id) }}"></a>
<a class="fas fa-edit fa-lg text-primary mr-1" href=" {{ route('projects.edit',$project->id) }}"></a>
#csrf
#method('DELETE')
<button type="submit" class="fa fa-trash fa-lg text-danger" style="border: none; background-color:white;"></button>
</form>
</td>
</tr>
</tbody>
#endforeach
</table>
{!! $projects->links() !!}
#endsection
<div class="modal fade" id="createModal" tabindex="-1" role="dialog" aria-labelledby="createModal" data-attr="{{ route('projects.create') }}">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-body">
#include('projects.create')
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
while this is the create page
#extends('layouts.modal')
#section('content')
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2>New Project</h2>
</div>
<div class="pull-right">
<a class="btn btn-primary" href="{{ route('projects.index') }}"> Back</a>
</div>
</div>
</div>
#if ($errors->any())
<div class="alert alert-danger">
<strong>Whoops!</strong> There were some problems with your input.<br><br>
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
<form action="{{ route('projects.store') }}" method="POST">
#csrf
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Name:</strong>
<input type="text" name="name" class="form-control" placeholder="Name">
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Introduction:</strong>
<textarea class="form-control" style="height:50px" name="introduction" placeholder="Introduction"></textarea>
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>location:</strong>
<textarea class="form-control" style="height:150px" name="location" placeholder="Location"></textarea>
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12 text-center">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>
#endsection
Thank you in advance
I later figure the answer, the mistake I was doing is including the
#extends('layouts.app')
#section('content')
So the page should just be the form or what you want to display without the #extends('layouts.app')
<form action="{{ route('projects.store') }}" method="POST">
#csrf
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Name:</strong>
<input type="text" name="name" class="form-control" placeholder="Name">
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Introduction:</strong>
<textarea class="form-control" style="height:50px" name="introduction" placeholder="Introduction"></textarea>
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>location:</strong>
<textarea class="form-control" style="height:150px" name="location" placeholder="Location"></textarea>
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12 text-center">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>

updating data into pivot table

i have a many to many realtion and now i wana know how can i update my table with a form that user sends to app . here is my code
route :
Route::get('admin/client/assign','ClientController#assignsellman');
this is my controller
public function assignsellman(Client $client){
$user = User::all();
$client_list = Client::all();
$client = Client::with('sellmanlist')->firstOrFail();
$sellman = $request->input('sellman');
$client_name = $request->input('client');
$client->sellmanlist()->attach($sellman);
$client->sellmanlist()->attach($client_name);
$client->save();
return view('admin.client.assign',compact('client_list','user'));
}
and finnaly the view :
<form action="/admin/client/" method="get">
<input type="hidden" name="_method" value="PUT">
{{ csrf_field() }}
<div class="row">
<div class="col-xs-4">
<div class="form-group">
<label for="client">مشتری</label>
<select class="" tabindex="-1" aria-hidden="true"
name="client">
#foreach($client_list as $client_lists)
<option value="{{$client_lists->id}}">{{$client_lists->title}}</option>
#endforeach
</select>
</div>
</div>
<div class="col-xs-4 text-center">
<i class="icon-arrow-left7 mr-3 icon-3x" style="font-size: 130px"></i>
<h4>ارجاع به</h4>
</div>
<div class="col-xs-4">
<div class="form-group">
<div class="form-group">
<label for="sellman">کارشناس فروش</label>
<select class="" tabindex="-1"
aria-hidden="true" name="sellman">
#foreach($user as $users)
<option value="{{$users->id}}">{{$users->name}}</option>
#endforeach
</select>
</div>
</div>
</div>
</div>
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
<button type="submit" class="btn btn-primary">تایید</button>
</form>
when i submit this form it redirects to a url like this
?_method=PUT&_token=mvf0i2FaxSBhDdJroqD6L1901QdvcR4b3tmgwekw&client=3&sellman=3
and nothing just happens to database .any idea what is my fault ?
In your case you need 2 routes, first for return form, second to save data from form.
Your routes:
Route::get('admin/client/assign','ClientController#assignsellman');
Route::post('admin/client/assign','ClientController#assignsellmanSave');
Controller:
public function assignsellman(Client $client)
{
$user = User::all();
$client_list = Client::all();
return view('admin.client.assign',compact('client_list','user'));
}
public function assignsellmanSave(Client $client)
{
$user = User::all();
$client_list = Client::all();
$client = Client::with('sellmanlist')->firstOrFail();
$sellman = $request->input('sellman');
$client->sellmanlist()->attach($sellman);
$client->save();
return view('admin.client.assign',compact('client_list','user'));
}
View:
<form action="/admin/client/assign" method="post">
{{ csrf_field() }}
<div class="row">
<div class="col-xs-4">
<div class="form-group">
<label for="client">مشتری</label>
<select class="" tabindex="-1" aria-hidden="true"
name="client">
#foreach($client_list as $client_lists)
<option value="{{$client_lists->id}}">{{$client_lists->title}}</option>
#endforeach
</select>
</div>
</div>
<div class="col-xs-4 text-center">
<i class="icon-arrow-left7 mr-3 icon-3x" style="font-size: 130px"></i>
<h4>ارجاع به</h4>
</div>
<div class="col-xs-4">
<div class="form-group">
<div class="form-group">
<label for="sellman">کارشناس فروش</label>
<select class="" tabindex="-1"
aria-hidden="true" name="sellman">
#foreach($user as $users)
<option value="{{$users->id}}">{{$users->name}}</option>
#endforeach
</select>
</div>
</div>
</div>
</div>
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
<button type="submit" class="btn btn-primary">تایید</button>
</form>

Method [vlidate] does not exist error in laravel 5.3 while trying to upload image

I am developing a project in laravel 5.3 where I have to create a 1 feild form to change logo of website my form look like this.
I do not need to save path in database. I just need to upload file in \public\images\ with name logo and only png files are allowed. so it will be \public\images\logo.png
following is the form code.
<div class="col-md-8 col-md-offset-2">
#if (count($errors) > 0)
<div class="alert alert-danger">
<strong>Whoops!</strong> There were some problems with your input.<br><br>
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
#if ($message = Session::get('success'))
<div class="alert alert-success alert-block">
<button type="button" class="close" data-dismiss="alert">×</button>
<strong>{{ $message }}</strong>
</div>
<img src="/images/{{ Session::get('path') }}">
#endif
<div class="box box-info">
<div class="box-header with-border text-center">
<h3 class="box-title">Basic Info</h3>
</div>
<!-- /.box-header -->
<!-- form start -->
<form class="form-horizontal" action="{{ url('/') }}/admin/change-site-logo" enctype="multipart/form-data" method="POST">
<div class="box-body">
{{ csrf_field() }}
<div class="form-group">
<label for="logo" class="col-sm-3 control-label">Logo</label>
<div class="col-sm-9">
<input type="file" class="form-control" id="logo" name="logo" placeholder="Logo Image">
</div>
</div>
<div class="form-group">
<label for="logo" class="col-sm-3 control-label"></label>
<div class="col-sm-9">
<img style="width: 200px; height: 50px;" src="https://www.google.com.pk/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png">
</div>
</div>
</div>
<!-- /.box-body -->
<div class="box-footer">
<button type="submit" class="btn btn-info pull-right">Save</button>
</div>
<!-- /.box-footer -->
</form>
</div>
<br /><br />
</div>
this is Route Route::post('/admin/change-site-logo', 'adminController#logo_change');
and controller is as following
class adminController extends Controller
{
public function logo_change(Request $request)
{
$this->vlidate($request, [
'logo' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
]);
$imageName = 'logo.'.$request->image->getClientOriginalExtension();
$request->image->move(public_path('images'), $imageName);
return back()
->with('success', 'Image Uploaded Successfully.')
->with('in path', $imageName);
}
I am writing first time this kind of code so dont know what to fix. I just know that the error is Method [vlidate] does not exist.

No query results for model [App\Card]

I have the following problem:
No query results for model [App\Card].
My Route.php:
Route::post('/cards/{card}/notes' , 'NotesController#store');
My NotesController:
public function store(Request $request)
{
return $request->all();
}
And my View:
#section('content')
<div class="row">
<div class="col-md-6 col-md-offset-3">
<h1> {{ $card->title }}</h1>
<h4> {{ $card->created_at }}</h4>
<ul class="list-group">
#foreach($card->notes as $note)
<li class="list-group-item">{{ $note->body }}</li>
#endforeach
</ul>
<hr>
<h3>Add a New Note</h3>
<form action="post" action="/cards/{{ $card->id }}/notes">
<div class="form-group">
<textarea name="body" class="form-control"></textarea>
</div>
<div class="form-group">
<button type="submit" class="btn btn-success">Add note</button>
</div>
</form>
</div>
</div>
#stop
I have two database tables - cards and notes.
This is my route.php
Route::get('/' , 'PagesController#home');
Route::get('/about' , 'PagesController#about');
Route::get('cards' , 'CardsController#index');
Route::get('cards/{card}' , 'CardsController#show');
Route::post('/cards/{card}/notes' , 'NotesController#store');
Yes i have records in tables here is pics
cards
Notes

Categories