laravel Cannot save records to DB - php

I am building a blog system with laravel.
Now I have a blogger table that has a name, email address, and password.
In addition to the default account table, I want to save a profile image and introduction.
They belong to the blogger table in my case.
But I cannot save those two records.
I cannot figure out why profile records cannot be inserted into my DB.
And my user role is a blogger.
I can see ?_token on the url.
blogger table
Schema::create('bloggers', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
blogs table
Schema::create('blogs', function (Blueprint $table) {
$table->bigIncrements('id');
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('bloggers');
$table->string('image');
$table->string('introduction');
$table->timestamps();
});
blogger.php
public function blogs()
{
return $this->hasMany(Blog::class, 'user_id');
}
blog.php
public function user(){
return $this->belongsTo(Blogger::class, 'user_id');
}
bloggersController.php
public function store(Request $request, Blogger $blogger_id){
$blogger_id = DB::table('bloggers')->where('id', $blogger_id)->get();
Auth::guard('blogger')->user();
if($request->hasfile('image')){
$file = $request->file('image');
$ext = $file->getClientOriginalExtension();
$filename = time().'.'.$ext;
$file->move('bloggers/', $filename);
$blog = Blog::updateOrCreate(
['user_id' => $blogger_id],
[
'image'=>$filename,
'introduction' => $request->introduction,
]
);
}
return view('bloggers.create')->with('bloggers', Blogger::all())->with('blogs', Blog::all());
}
web.php
Route::get('/create', 'BloggersController#store')->name('blogs.store');
create.blade.php
<form action="{{ route('blogs.store') }}" enctype="multipart/form-data">
#csrf
<img src="{{asset('blog-image/alexandre-chambon-zxmSX2-GQBc-unsplash.jpg')}}" alt="card-background" class="card-img">
<div class="image-preview" id="imagePreview">
#if(empty(Auth::guard('blogger')->user()->blog->image))
<img src="{{asset('avatar/avatar.png')}}" id="image-preview__image" alt="avatar">
#else
<img src="{{asset('bloggers/')}}/{{ Auth::guard('blogger')->user()->blog->image}}" id="preview" alt="profile image">
#endif
</div>
<input type="text" class="name" value="{{ Auth::guard('blogger')->user()->name }}" name="name">
<textarea name="introduction" id="" cols="30" rows="10" class="profile">
#if(!empty(Auth::guard('blogger')->user()->blog->introduction)){{ Auth::guard('blogger')->user()->blog->introduction }}#endif
</textarea>
<div class="preview">
<input type="file" id="file" class="file1" accept="image/*" name="image">
<label for="file">
Add profile photo
</label>
</div>
<button type="submit" id="register">Register</button>
</form>

public function store(Request $request, Blogger $blogger){
// If you are using route model you already have a model instance
$blogger_id = $blogger->id
//Auth::guard('blogger')->user();
if($request->hasFile('image')){
$file = $request->file('image');
$ext = $file->getClientOriginalExtension();
$filename = time().'.'.$ext;
$file->move(public_path("bloggers"), $filename);
$path = '/bloggers/' . $filename;
$blog = Blog::updateOrCreate(
['user_id' => $blogger_id],
[
'image'=>$filename,
'introduction' => $request->introduction,
]
);
}
return view('bloggers.create')->with('bloggers', Blogger::all())->with('blogs', Blog::all());
}

Related

Method Illuminate\Database\Eloquent\Collection::books does not exist.(laravel)

I am beginner of laravel, I want to store a book to books database, but after click button, it shows "Method Illuminate\Database\Eloquent\Collection::books does not exist." What am I missing?
here are my codes.
BookController
public function create()
{
return view('books.create');
}
public function store(Request $request)
{
$this->validate($request, [
'book' => 'required|max:255',
'category' => 'required',
'quantity' => 'required|numeric',
'price'=>'required|numeric',
'info'=>'required'
]);
//$request->user()->member()->books()->create([
$member=auth()->user()->member()->get();
$member->books()->create([
'book' => $request->book,
'category' => $request->category,
'quantity' => $request->quantity,
'price'=>$request->price,
'info'=>$request->info
]);
return redirect('shops')->with('success', 'successful');
}
books.create
<form action="{{route('books.store')}}" method="POST" role="form">
#csrf
#method('POST')
<div class="form-group">
<label for="book">name:</label>
<input id="book" name="book" class="form-control" placeholder="enter book name">
</div>
<div class="form-group">
<label for="category">category:</label>
<input id="category" name="category" class="form-control" placeholder="enter category">
</div>
<div class="form-group">
<label for="quantity">quantity:</label>
<input id="quantity" name="quantity" class="form-control" placeholder="enter quantity">
</div>
<div class="form-group">
<label for="price">price:</label>
<input id="price" name="price" class="form-control" placeholder="enter price">
</div>
<div class="form-group">
<label for="info">info:</label>
<textarea id="info" name="info" class="form-control" rows="10" placeholder="enter info"></textarea>
</div>
<button type="submit" class="btn-sm btn-primary">create</button>
</form>
User and Member is one to one relation, and Book belongs to one Member
Book Model
public function member()
{
return $this->belongsTo(Member::class);
}
protected $fillable = ['book','category','quantity','price','info'];
Member Model
public function books()
{
return $this->hasMany(Book::class);
}
public function user()
{
return $this->belongsTo(User::class);
}
User Model
public function member()
{
return $this->hasOne(Member::class);
}
books,user and member migration
books migration
public function up()
{
Schema::create('books', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('member_id');
$table->foreign('member_id')->references('id')->on('members')->onDelete('cascade');
$table->string('name');
$table->integer('quantity');
$table->integer('price');
$table->string('path');
$table->string('info');
$table->string('category');
$table->timestamps();
});
}
member migration
public function up()
{
Schema::create('members', function (Blueprint $table) {
$table->increments('id');
$table->unsignedbigInteger('user_id');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->string('sex');
$table->string('email');
$table->string('address');
$table->string('tel');
$table->timestamps();
});
}
user migration
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->foreignId('current_team_id')->nullable();
$table->text('profile_photo_path')->nullable();
$table->timestamps();
});
}
You are receiving a Collection from this chain of calls:
$member = auth()->user()->member()->get();
get is going to always return a Collection when called on a relationship method. If you want a single model you can call first instead:
$member = auth()->user()->member()->first();
Though first could return null so you may need to check that.
Another method to access the result of this relationship would be to use the dynamic property for the relationship member:
$member = auth()->user()->member;
Since that relationship is defined as a HasOne it knows to load it for a single result or null.
Assuming $member isn't null at this point you should be fine with the rest how it is.
$member=auth()->user()->member()->get(); returns a collection not an object of Member class. Use $member=auth()->user()->member()->first(); or $member=auth()->user()->member;
Try this
public function store(Request $request)
{
$this->validate($request, [
'book' => 'required|max:255',
'category' => 'required',
'quantity' => 'required|numeric',
'price'=>'required|numeric',
'info'=>'required'
]);
$member=auth()->user()->member;
if($member){
$member->books()->create([
'book' => $request->book,
'category' => $request->category,
'quantity' => $request->quantity,
'price'=>$request->price,
'info'=>$request->info
]);
return redirect('shops')->with('success', 'successful');
}
//Member is not found, return with error
return redirect()->back()->with('error', 'Member not found');
}
That error is due to the fact that this call $member=auth()->user()->member()->get();. It's supposed to return a Collection of Member.
So when you try to call books on a collection hold by the variable member It' won't succeed as Illuminate\Support\Collection Class doesn't define a method books you have to loop trought that collection by using a foreach loop or a each or map method from Collection.
$member->each(function($member){
$member->books()->create([
//
]);
});
Or has you have already define in User Model that user will always have a single Member by using hasOne method.
So you can use auth()->user()->member()->first(); which return a single instance of type Member on which you call books method but with that you have to be sure that the Authenticated use has already one member which is attached to him to avoid any error. even though that is the case you can always check if variable $member is not null with an if statement
$member = auth()->user()->member()->first();
if($member){
// here you can have access to `books`
$member->books()->create([
//...
]);
}

Laravel relationships table show

i am new to laravel and working on relationships
i have a phonebook which it has a client in it so when i insert the data i add some client id to it how can i get the client name in phonebook view when i am showing the list of phonebooks i want to get client object and show the name with it like this $client->title
and here is my code maybe i cant define it in words :)
this is my PhonebookController
public function index()
{
$phonebooks = Phonebook::all();
$client = Phonebook::find(?dont know if its right place for it?)->client;
return view('admin.phonebooks.index',compact('phonebooks',$phonebooks),compact('client',$client));
}
and here is Phonebook model
class Phonebook extends Model{
protected $fillable = ['title','description','client_id','calldate','rememberdate'];
public function client() {
return $this->hasOne('App\Client','id');
} }
here is my phonebook db migration
Schema::create('phonebooks', function (Blueprint $table) {
$table->increments('id');
$table->text('title');
$table->longText('description');
$table->integer('client_id');
$table->dateTime('calldate');
$table->dateTime('rememberdate');
$table->timestamps();
});
and the client db migration
Schema::create('clients', function (Blueprint $table) {
$table->increments('id');
$table->text('title');
$table->longText('description');
$table->integer('fax');
$table->text('adrress1');
$table->integer('telephone1');
$table->timestamps();
});
and finally here is the view
#foreach($phonebooks as $phonebook)
<tr>
<th scope="row">{{$phonebook->id}}</th>
<th scope="row">{{$phonebook->title}}</th>
<td>{{$phonebook->description}}</td>
<td>{{$phonebook->calldate}}</td>
<td>{{$phonebook->created_at->toFormattedDateString()}}</td>
<td>{{$client->title}}</td>
<td>
<div class="btn-group" role="group" aria-label="Basic example">
<a href="{{ URL::to('admin/phonebooks/' . $phonebook->id . '/edit') }}">
<button type="button" class="btn btn-warning">edit</button>
</a>
<form action="{{url('admin/phonebooks', [$phonebook->id])}}" method="POST">
<input type="hidden" name="_method" value="DELETE">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="submit" class="btn btn-danger" value="delete"/>
</form>
</div>
</td>
</tr>
#endforeach
If client hasMany Phonebook entry (and phonebook belongsTo client) then you need a client_id column on the phonebooks table
Then in the client model
public function phonebooks()
{
return $this->hasMany(App\Phonebook::class);
}
In the phonebook model
public function client()
{
return $this->belongsTo(App\Client::class);
}
In the controller
$phonebooks = Phonebook::with('client')->get();
return view('admin.phonebooks.index',compact('phonebooks'));
}
Your $phonebook models will all have a ->client relation, so in the view
<td>{{$phonebook->client->title}}</td>

Laravel: Can't get uploaded images to display

On my website, a registered user can create a post by uploading an image and writing a description. I have made it where a unique image name is created based on the user's image name and the time they upload the image. Once a name is created it is stored in the post database. The image name successfully gets stored and so does the actual image. However, when it comes to actually displaying the image on the post nothing comes up like it can't find the image. I can't seem to figure out why this is.
Here is my PostController.php class:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Post;
class PostController extends Controller
{
public function getDashboard(){
$posts = Post::orderBy('created_at', 'desc')->get();
return view('dashboard', ['posts' => $posts]);
}
public function postCreatePost(Request $request){
$this->validate($request, [
'body' => 'required',
'cover_image' => 'image|nullable|max:1999'
]);
if($request->hasFile('cover_image')){
$filenameWithExt = $request->file('cover_image')->getClientOriginalName();
$filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
$extension = $request->file('cover_image')->getClientOriginalExtension();
$fileNameToStore = $filename . '_' . time() . '.' . $extension;
$path = $request->file('cover_image')->storeAs('public/cover_images', $fileNameToStore);
} else{
$fileNameToStore = 'noimage.jpg';
}
$post = new Post();
$post->body = $request['body'];
$post->cover_image = $fileNameToStore;
$message = 'There was an error';
if($request->user()->posts($post)->save($post)){; //points here
$message = 'post successfully created';
}
return redirect()->route('dashboard')->with(['message' => $message]);
}
public function getDeletePost($post_id){
$post = Post::where('id', $post_id)->firstOrFail();
$post->delete();
return redirect()->route('dashboard')->with(['message' => 'post deleted']);
}
}
Here is my view:
<section class="row new-post">
<div class="col-md-6 col-md-offset-3">
<form action="{{ route('postcreate') }}" method="post" enctype="multipart/form-data">
<div class="form-group">
<input type="file" name="cover_image" class="form-control" id="cover_image">
</div>
<div class="form-group">
<textarea class="form-control" name="body" rows="5" placeholder="your post"></textarea>
</div>
<button type="submit" class="btn btn-primary">Create post</button>
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>
</div>
</section>
#foreach($posts as $post)
<section class="row posts">
<div class="col-md-6">
<article class="post">
<p>{{ $post->body }}</p>
<div class="info">Posted by {{ $post->user->first_name }} {{ $post->user->last_name }} on {{ $post->created_at }}</div>
<div class="interaction">
Like|
#if(Auth::user() == $post->user)
Edit|
Delete
#endif
</div>
</article>
</div>
<div class="col-md-6">
<img src="/storage/cover_images/{{ $post->cover_image }}">
</div>
</section>
#endforeach
Here is my migration:
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->string('cover_image');
$table->text('body');
$table->integer('user_id');
});
You're storing as public/cover_images but echoing out storage/cover_images.
Updated following your response;
Why not try using
<img src="{{ asset('storage/cover_images/' . $post->cover_image) }}">

Undefined variable:user laravel

I keep on getting this error whenever I try to enter the upload page.
Can anybody help?
I have already done the compact part to make sure that the variable is being passed to the view and also my route should be ok I think.
I tried using dd but all it does is keep on showing me the error
Error: Undefined variable: user (View: C:\xampp\htdocs\Evaluation\resources\views\upload.blade.php)
Here are my codes:
upload.blade.php
<form class="form-horizontal" method="post" action="{{ url('/userUpload')}}" 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="file">
</div>
</div>
<div class="form-group">
<div class="col-md-6-offset-2">
<input type="submit" class="btn btn-primary" value="Save">
</div>
</div>
</form>
UploadController:
public function upload(){
return view(‘upload’);
}
public function store(Request $request,$id){
$this->validate($request, [
'file' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
]);
var_dump('has file '.$request->hasFile('file'));
if ($request->hasFile('file')) {
$image = $request->file('file');
$name = $image->getClientOriginalName();
$size = $image->getClientSize();
$id = $request->user_id;
$destinationPath = public_path('/images');
$image->move($destinationPath, $name);
$Image = new Image;
$Image->name = $name;
$Image->size = $size;
// $Image->user_id = $id;
//$Image->save();
$user->find($id);
dd($user);
$user->Images()->save($Image);
}
return redirect('/home');
}
public function test(){
$user = user_information::where('id')->get();
return view('upload', compact('user'));
}
Route: (this are my route)
Route::get('/UploadUser/upload','UploadController#upload’);
Route::post('/UploadUser','UploadController#store');
Route::post('/UploadUser/upload', 'UploadController#test');
Another question: I keep on getting this error when i try to upload a file, so what should I do?
Here is the error:
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or
update a child row: a foreign key constraint fails (form.images,
CONSTRAINT images_user_id_foreign FOREIGN KEY (user_id) REFERENCES
usere_information (id)) (SQL: insert into images (name,
size, user_id, updated_at, created_at) values (download.png,
4247, 1, 2017-10-25 08:54:57, 2017-10-25 08:54:57))
Image model:
class Image extends Model
{
protected $fillable = array('name','size','user_id');
public function user_informations() {
return $this->belongsTo('App\user_information', 'user_id', 'id');
}
}
Images table:
Schema::create('images', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('size');
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('user_informations');
$table->timestamps();
});
User_information table:
Schema::create('user_informations', function (Blueprint $table) {
$table->increments('id');
$table->engine = 'InnoDB';
$table->binary('signature');
$table->String('Name');
$table->timestamps();
});
User_information model:
class user_information extends Eloquent
{
protected $fillable = array('signature', 'Name');
protected $table = 'user_informations';
protected $primaryKey = 'id';
public function Images() {
return $this->hasOne('App\Image','user_id');
}
}
How to get the image?
Here is the view folder:
#foreach ($data as $object)
<b>Name: </b>{{ $object->Name }}<br><br>
Edit<br>
#foreach ($data3 as $currentUser)
<img src="{{ asset('public/images/' . $currentUser->Image->name ) }}">
#endforeach
#if($data3->count())
#foreach($data3 as $currentUser)
<a href="{!! route('user.upload.image', ['id'=>$currentUser->user_id]) !!}">
<button class="btn btn-primary"><i class ="fa fa-plus"></i>Upload Images</button>
</a>
#endforeach
#else
<a href="{!! route('user.upload.image', ['id'=>$object->id]) !!}">
<button class="btn btn-primary"><i class ="fa fa-plus"></i>Upload Images</button>
#endif
#endforeach
HomeController:
public function getInfo($id) {
$data = user_information::where('id',$id)->get();
$data3=Image::where('user_id',$id)->get();
return view('test',compact('data','data3'));
Because you didn't pass the user to your upload view, try to pass it like this :
public function upload(){
$id = 1 //The wanted user or if the user is authenticated use Auth::id()
$user = User::find($id);
return view('upload')->withUser($user);
}
Or if the user is authenticated use Auth in the view :
<form class="form-horizontal" method="post" action="{{ url('/userUpload')}}" enctype="multipart/form-data">
{{ csrf_field() }}
<input type="hidden" name="user_id" value="{{auth()->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="file">
</div>
</div>
<div class="form-group">
<div class="col-md-6-offset-2">
<input type="submit" class="btn btn-primary" value="Save">
</div>
</div>
</form>
For the second problem it's because in the route you have
Route::post('/UploadUser','UploadController#store');
and the your store method signature is
public function store(Request $request,$id){
The $id parameter that did the problem because it's not defined in the route so simply remove it from the method signatre
public function store(Request $request){
$this->validate($request, [
'file' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
]);
if ($request->hasFile('file')) {
$image = $request->file('file');
$name = $image->getClientOriginalName();
$size = $image->getClientSize();
$id = $request->user_id; // here id is declared no need for the parameter
$destinationPath = public_path('/images');
$image->move($destinationPath, $name);
$Image = new Image;
$Image->name = $name;
$Image->size = $size;
$Image->user_id = $id;
$Image->save();
}
return redirect('/home');
}
For the third case you have to change the routes from :
Route::get('/UploadUser/upload','UploadController#upload’);
to
Route::get('/UploadUser/{user}/upload','UploadController#upload’)->name('user.upload.image');
And in the view add the id in the upload button url maybe like this :
{!! route('user.upload.image', ['user'=>$currentUser->id]) !!}
Then in the upload method :
public function upload(user_information $user){ // route model binding here
// dd($user); //for testing only :)
return view('upload')->withUser($user);
}
In the view change
<input type="hidden" name="user_id" value="{{auth()->id()}}">
To
<input type="hidden" name="user_id" value="{{$user->id()}}">
And you are good to go ;)
#foreach ($data as $currentUser)
<b>Name: </b>{{ $currentUser->Name }}<br><br>
Edit<br>
#if($currentUser->Image)
<img src="{{ asset('public/images/' . $currentUser->Image->name ) }}">
#endif
<a href="{!! route('user.upload.image', ['id'=>$currentUser->id]) !!}">
#endforeach
You have miss to pass id in your where condition,
public function test(){
$user = user_information::where('id',$id)->first();
return view('create1', compact('user'));
}
and you have to pass your user data into this,
public function upload(){
$user = user_information::where('id',$id)->first();
return view(‘upload’,compact('user'));
}
Hope it helps,
On your upload function, you have to pass the user variable because you use the $user in the view. So the controller will be
public function upload() {
$user = Auth::user();
return view('upload', compact('user'));
}
do not forget to change the $user based on your need.
You have to pass an $id variable into your test() method. Then please comment below what's the error next so I can follow you through.
Update
Since you don't want to pass an id. You can use:
public function test(){
$u = Auth::user();
$user = user_information::where('id', $u->id)->get();
return view('upload', compact('user'));
}
OR
Try to use first() instead of get().
More option:
I have noticed, you're using the upload() method here, why not passing the $user there? like so:
public function upload(){
$user = Auth::user();
return view(‘upload’, compact('user'));
}

How to store and update multiple data in Laravel

I have two tables in my database: role's table and role membership's table
role's table
Schema::create('roles', function (Blueprint $table) {
$table->increments('id');
$table->string('RoleName');
$table->boolean('IsAllCategory')->nullable()->default(0);
$table->boolean('IsUserCanLogin')->nullable()->default(1);
$table->timestamps();
});
role membership's table
Schema::create('role_memberships', function (Blueprint $table) {
$table->increments('id');
$table->integer('role_id');
$table->string('MembershipName');
$table->text('MembershipValue');
$table->timestamps();
});
I have a row data in my role's table with RoleName = 'Admin', then I set the membership.. everytime I store role membership data into database, it will store as two rows with same role_id..
This is my controller of Role membership
public function show($id)
{
$role = Role::findOrFail($id);
$postAdMaxImage = DB::table('role_memberships')->where('role_id', $role->id)->where('MembershipName' , 'PostAdMaxImage')->first();
$postAdExpiredDay = DB::table('role_memberships')->where('role_id', $id)->where('MembershipName' , 'PostAdExpiredDay')->first();
return view('pages/back-end/forms/role/membership')->with('role', $role)
->with('postAdMaxImage', $postAdMaxImage)
->with('postAdExpiredDay', $postAdExpiredDay);
}
public function update(Request $request, $id)
{
$role = Role::findOrFail($id);
$membership = [
['role_id' => $id, 'MembershipName' => 'PostAdMaxImage', 'MembershipValue' => $request->PostAdMaxImage ? $request->PostAdMaxImage : ''],
['role_id' => $id, 'MembershipName' => 'PostAdExpiredDay', 'MembershipValue' => $request->PostAdExpiredDay ? $request->PostAdExpiredDay : '']
];
DB::table('role_memberships')->insert($membership);
return response()->json(array($role, $membership));
}
The first problem is in show function.. I can not use first().. I want to get the data to show it into my view
<div class="form-group">
<div class="col-md-6">
<label>Membership</label><br>
<label>Maximum Gambar untuk Iklan</label>
<div class="row">
<div class="col-md-10">
<input type="text" name="PostAdMaxImage" class="form-control" value="{{$postAdMaxImage->MembershipValue}}">
</div>
<div class="col-md-2" style="padding: 0 !important">
<h5>Gambar</h5>
</div>
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-6">
<label>Masa Berlaku Iklan</label>
<div class="row">
<div class="col-md-10">
<input type="text" name="PostAdExpiredDay" class="form-control" value="{{$postAdExpiredDay->MembershipValue}}>
</div>
<div class="col-md-2" style="padding: 0 !important">
<h5>Hari</h5>
</div>
</div>
</div>
</div>
The second problem is, when I want to edit the membership.. It will store as new two rows, not update the last two rows
To get the values use the value function
$postAdMaxImage = DB::table('role_memberships')->wh‌​ere('role_id', $role->id)->where('MembershipName' , 'PostAdMaxImage')->value('MembershipValue');
$postAdExpiredDay = DB::table('role_memberships')->where('role_id', $id)->where('MembershipName' , 'PostAdExpiredDay')->value('MembershipValue');
change your view to:
value="{{$postAdMaxImage}}"
value="{{$postAdExpiredDay}}"
In the second question you need to alter your update function to update the fields not create new ones
public function update(Request $request, $id)
{
$PostAdMaxImage= $request['PostAdMaxImage'];
$PostAdExpiredDay = $request['PostAdExpiredDay'];
DB::table('role_memberships')->where('role_id',$id)->where('MembershipName','PostAdMaxImage')->update(['MembershipValue'=>$PostAdMaxImage]);
DB::table('role_memberships')->where('role_id',$id)->where('MembershipName','PostAdExpiredDay')->update(['MembershipValue'=>$PostAdExpiredDay]);
return response()->json(array($role, $membership));
}

Categories