Laravel Sending from data to database - laravel

I am using laravel 5.2 and i am curently developing a messaging system for my app. i am trying to send messages to the database, but i keep getting this error "Call to undefined method Illuminate\Database\Query\Builder::messages()"
My MessageController is as Follows:
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Session;
use App\Http\Requests;
use App\Message;
class MessageController extends Controller
{
public function CreateMessage(Request $request)
{
$meso = new Message();
$meso->body = $request['body'];
$meso->subject = $request['subject'];
$request->user()->messages()->save($meso) ;
return redirect()->route('mail');
}
}
Here is my routes file
Route::post('send', 'MessageController#CreateMessage');
and here is my form:
<form role="form" class="form-horizontal" method="POST" action="{{ url('/send') }}">
<div class="form-group">
<label class="col-lg-2 control-label">To</label>
<div class="col-lg-10">
<input type="text" placeholder="" id="inputEmail1" class="form-control">
</div>
</div>
<div class="form-group">
<label class="col-lg-2 control-label">Cc / Bcc</label>
<div class="col-lg-10">
<input type="text" placeholder="" id="cc" class="form-control">
</div>
</div>
<div class="form-group">
<label class="col-lg-2 control-label">Subject</label>
<div class="col-lg-10">
<input type="text" placeholder="" id="inputPassword1" class="form-control">
</div>
</div>
<div class="form-group">
<label class="col-lg-2 control-label">Message</label>
<div class="col-lg-10">
<textarea rows="10" cols="30" class="form-control" id="" name=""></textarea>
</div>
</div>
<div class="form-group">
<div class="col-lg-offset-2 col-lg-10">
<span class="btn green fileinput-button">
<i class="fa fa-plus fa fa-white"></i>
<span>Attachment</span>
<input type="file" name="files[]" multiple="">
</span>
<button class="btn btn-send" type="submit">Send</button>
<input type="hidden" value="{{ Session::token() }}" name="_token">
</div>
</div>
</form>
Someone help me debug this error

You should create messages() relationship in User model:
public function messages()
{
return $this->hasMany('App\Message');
}
Also, you need to have user_id foreign key defined in messages table to make the relationship work.

Try this:
$request->user()->messages->save($meso);
instead of:
$request->user()->messages()->save($meso);

Related

Get checkbox status in livewire

I have a form that has 2 checkboxes in this form. When I submit the information I want to test what the value of the first checkbox is. I use ‍‍‍‍‍‍`dd () but it shows me the value of Null. What is the problem? How can I solve this?
this is view:
<div class="form-group">
<div class="custom-control custom-switch">
<input class="custom-control-input" id="deposit" type="checkbox"> <label class="custom-control-label" for="deposit">Deposit</label>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<div class="custom-control custom-switch">
<input class="custom-control-input" id="withdraw" type="checkbox"> <label class="custom-control-label" for="withdraw">withdraw</label>
</div>
</div>
</div>
<button class="btn btn-round btn-outline-primary"><span class="w-100px">create</span></button>
and this is component:
<?php
namespace App\Http\Livewire\Backend\Currency;
use Livewire\Component;
class Networks extends Component
{
public $deposit;
public $withdraw;
public function addNetwork()
{
dd($this->deposit1);
}
public function render()
{
return view('livewire.backend.currency.networks');
}
}
You bind to the checkboxes like you would any other public property, using wire:model.
<div class="form-group">
<div class="custom-control custom-switch">
<!-- bind to $deposit -->
<input wire:model="deposit"
class="custom-control-input"
id="deposit"
type="checkbox">
<label class="custom-control-label" for="deposit">Deposit</label>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<div class="custom-control custom-switch">
<!--bind to $withdraw -->
<input wire:model="withdraw"
class="custom-control-input"
id="withdraw"
type="checkbox">
<label class="custom-control-label" for="withdraw">withdraw</label>
</div>
</div>
</div>
Now when the form is submitted, if deposit or withdraw is checked, their value will be true.
Something you might want to do is give $deposit and $withdraw a default value of false as otherwise their initial values will be null.
public $deposit = false;
public $withdraw = false;

Error when trying to get property 'name' of non-object

hi i am trying to show the value of name which is stored in database but i am getting this error can anyone please guide me how can i rectify my error .
<form action="Store-data" method="POST">
#csrf
<div class="form-group my-2">
<input type="text" name="name" value="{{ $Task ->name }}">
<div class="form-group my-2">
<input type="email" name="email" placeholder="Enter your email" value="{{ $Task ->email }}" >
<div class="form-group my-2 text-center">
<button type="submit" class="btn btn-success"> Add user</button>
</div>
</div>
</div>
</form>
here is my edit controller
public function edit($Taskid)
{
//
$Edit= Superior::find($Taskid);
return view('Myview.edit')->with('Task','$Edit');
}
here is my route for edit
Route::get('Superior/{Task}/edit','SuperiorController#edit');
i am using Task as a key but it is giving me an error
i also tried but didn't work
{{$Task['name']}}
You should remove ' that wraps $Edit.
return view('Myview.edit')->with('Task', $Edit);
Change your code like this.
Form Code
<form action="Store-data" method="POST">
#csrf
<div class="form-group my-2">
<input type="text" name="name" value="{{isset($Edit) ? $Edit->name : '' }}">
<div class="form-group my-2">
<input type="email" name="email" placeholder="Enter your email" value="{{ isset($Edit) ? $Edit->email : '' }}">
<div class="form-group my-2 text-center">
<button type="submit" class="btn btn-success"> Add user</button>
</div>
</div>
</div>
</form>
Route
Route::get('Superior/edit/{id}','SuperiorController#edit');
Controller Code
public function edit($id){
$Edit= Superior::find($id);
return view('Myview.edit',compact('Edit'));
}
Hopefully it'll solve the problem
Create function like this. if Taskid in your database column.
public function edit(Request $Request, $Taskid)
{
//
$Edit= Superior::where('Taskid', $Taskid)->first();
return view('Myview.edit', compact(Edit));
}
In your html it will like this
{{ $Edit->name }}
In your Route
Route::get('Superior/{Taskid}/edit','SuperiorController#edit');

Laravel :error when updating the database after editing data

I have two tables table "technician" and table "user" with a connection one to one and I have a form through which I edit the technician information. When I save the changes I get an error and the update doesn't work.Here is my form my code and a screenshot of the error Thank you in advance.
route.php
Route::get('/technicien/{id}/edit', 'TechnicienController#edit');
Route::patch('/technicien/{id}', 'TechnicienController#update')-
>name('technicien.update');
edit.php
#extends('Layouts/app')
#extends('Layouts.master')
#section('content')
<div class="container">
<div class="row">
<div class="col-md-10">
<h1>Modifier Technicien</h1>
<form action="{{ route('technicien.update', $moyenne_avis->id , $actif->id , $user->nom , $user->prenom ,$user->email ) }}" method="update">
{{csrf_field()}}
{{ method_field('PATCH') }}
<div class="form-group">
<label for="nom">Nom</label>
<input id="nom" type="text" class="form-control" name="user[nom]" value="{{$user->nom}}" >
</div>
<div class="form-group">
<label for="prenom">Prenom</label>
<input id="prenom" type="text" class="form-control" name="user[prenom]" value="{{$user->prenom}}" >
</div>
<div class="form-group">
<label for="prenom">Email</label>
<input id="prenom" type="text" class="form-control" name="user[email]" value="{{$user->email}}" >
</div>
<div class="form-group">
<label for="">moyenne Avis</label>
<input type="text" name="moyenne_avis" class="form-control" value ="{{$moyenne_avis->moyenne_avis}}" >
</div>
<div class="form-group">
<label for="">Etat Technicien</label>
<input type="text" name="actif" class="form-control" value ="{{$moyenne_avis->actif}}" >
</div>
<div class="form-group">
<input type="submit" value="enregistrer" class="form-control btn btn-primary">
</div>
</div>
</form>
</div>
</div>
#endsection
controller1.php
public function edit($id)
{
$technicien=technicien::find($id);
$user = $technicien->user;
return view('technicien.edit',['moyenne_avis'=>$technicien],
['actif'=>$technicien],['user_id'=>$technicien])->with('user',$user);
}
controller2.php
public function update(Request $request, $id)
{
// do some request validation
$technicien=technicien::find($id);
$technicien->update($request->all());
$technicien->user->update($request->get('user'));
return redirect('technicien');
}
You are passing too many param and in your route you only take one param!
I think you only need one param in your view which is technician ID and which will find the record in your controller query!
You can try follow code with removing unusable params from form:
<form action="{{ route('technicien.update', $moyenne_avis->id ) }}" method="POST">
And your route looks like:
Route::get('/technicien/{id}/edit', 'TechnicienController#edit');
Route::post('/technicien/{id}', 'TechnicienController#update')->name('technicien.update');
Hope this helps you!

How to insert data in laravel 5.2 using ajax serialize() function?

I am having a trouble implementing data insertion using laravel by passing data from my view using serialize() function to my controller.I am just starting to play around laravel but I am now stacked on this. Begging someone to help me solve this. Thanks a lot. Below are my codes.
Product Form
<form class="form-horizontal prod-form" id="prod-form" style="background-color: #e2e2e2;" method="post" enctype="multiprodt/form-data">
<fieldset>
<div class="alert alert-dismissable alert-success alert-add-success">
<button type="button" class="close" data-dismiss="alert">×</button>
<center><h4>Data successfully saved.</h4></center>
</div>
<address></address>
<input type="hidden" name="prod_id" class="prod_id" id="prod_id" value="">
<input type="hidden" name="_token" value="<?= csrf_token(); ?>">
<div class="form-group">
<label for="inputActivity" class="col-lg-2 control-label">Product Name</label>
<div class="col-lg-10">
<input type="text" class="form-control" name="prod[pharmaceutical]" id="inputPharmaceutical" placeholder="Product name" value="" style="width:260px;height:40px;" onchange="" required>
</div>
</div>
<div class="form-group">
<label for="inputActivity" class="col-lg-2 control-label">Description</label>
<div class="col-lg-10">
<input type="text" class="form-control" name="prod[description]" id="inputDescription" placeholder="Description" value="" style="width:260px;height:40px;" onchange="" required>
</div>
</div>
<div class="form-group">
<label for="inputActivity" class="col-lg-2 control-label">Unit</label>
<div class="col-lg-10">
<input type="text" class="form-control" name="prod[unit]" id="inputUnit" placeholder="Unit" value="" style="width:260px;height:40px;" onchange="" required>
</div>
</div>
<div class="form-group">
<label for="inputVenue" class="col-lg-2 control-label">Price</label>
<div class="col-lg-10">
<input type="text" class="form-control" name="prod[price]" id="inputPrice" placeholder="Price" value="" style="width:260px;height:40px;" required>
</div>
</div>
<div class="form-group">
<label for="inputSponsors" class="col-lg-2 control-label">Quantity</label>
<div class="col-lg-10">
<input type="text" class="form-control" name="prod[quantity]" id="inputQuantity" placeholder="Quantity" value="" style="width:260px;height:40px;" required>
</div>
</div>
<div class="form-group">
<label for="inputSponsors" class="col-lg-2 control-label">Amount</label>
<div class="col-lg-10">
<input type="text" class="form-control" name="prod[amount]" id="inputAmount" placeholder="Amount" value="" style="width:260px;height:40px;" required>
</div>
</div>
<div class="form-group">
<div class="col-lg-10 col-lg-offset-2">
<button class="btn btn-primary submit-prod">Submit</button>
<button class="btn btn-default">Cancel</button>
</div>
</div>
</fieldset>
</form>
Javascript Function when submit button is clicked
<script type="text/javascript">
$(".submit-prod").click(function(e){
e.preventDefault();
var button_text = $(this).text();
alert($("#prod-form").serialize());
$.post("{{ url('/addprod') }}",$("#prod-form").serialize(),function(data){
if(data.notify == "Success"){
console.log(data.notify);
}
},"json");
}); //end
</script>
Route.php
Route::group(['middleware' => 'web'], function () {
Route::post('addprod', 'Product\ProductController#store');
Route::get('/home', 'HomeController#index');
});
ProductController.php
<?php
namespace App\Http\Controllers\Product;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Product\Product as Product;
class ProductController extends Controller
{
/**
* Show the application dashboard.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
return view('home');
}
public function create(){
}
public function store(Request $request){
//$product = new Product;
$prod_details = $request->all();
$query = Product::create($prod_details);
if($query){
$notification = "Success";
} else{
$notification = "Failed";
}
echo json_encode(array('notify'=>$notification));
}
}
Model: Product.php
<?php
namespace App\Product;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
//
}
Sample Input:
Error Output:
Well the issue is the csrf-token please user form helper class to declare your forms or declare the token. Else you will take millenniums to solve your problem

how can I add columns for App\User and store the data to in users table in laravel 5.1?

public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password', 60);
$table->integer('mobile');
$table->integer('status')->default(0);
$table->integer('team_id');
$table->string('created_by');
$table->string('updated_by');
$table->rememberToken();
$table->timestamps();
});
}
The above method creates user table with few additional columns.I then added fillable content in my User model as:
protected $fillable = ['name', 'email', 'password','mobile','team_id'];
My create.blade.php view is:
#extends('master')
#section('title','Creating.. User')
#section('content')
<div class="container-fluid">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">Register</div>
<div class="panel-body">
#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
<form class="form-horizontal" role="form" method="POST" action="{{ url('/auth/register') }}">
{!! csrf_field() !!}
<div class="form-group">
<label class="col-md-4 control-label">Name</label>
<div class="col-md-6">
<input type="text" class="form-control" name="name" value="{{ old('name') }}">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">E-Mail Address</label>
<div class="col-md-6">
<input type="email" class="form-control" name="email" value="{{ old('email') }}">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Password</label>
<div class="col-md-6">
<input type="password" class="form-control" name="password">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Confirm Password</label>
<div class="col-md-6">
<input type="password" class="form-control" name="password_confirmation">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Mobile</label>
<div class="col-md-6">
<input type="number" class="form-control" name="mobile">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Team</label>
<div class="col-md-6">
<input type="number" class="form-control" name="team_id">
</div>
</div>
<!--
<div class="form-group">
<label class="col-md-4 control-label">Role</label>
<div class="col-md-6">
<input type="text" class="form-control" name="role">
</div>
</div>
-->
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<button type="submit" class="btn btn-primary">
Register
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
#endsection
Then,I filled all the fields in the form and when I submit I get username,password,email.I am not getting other fields "mobile number" and "team_id".(Team Id is what I enter in the form)
fields- username,password is saved but mobile and team aren't
I can create this with controller,defining my own routes and do it manually.But,I don't want to do that.I want to use the same User model that comes default for authentication in laravel 5.1.
What I need is ::: Is there any way to create and store these columns in the database with the users table that exists in migration by default.
I changed the mobile from integer to string and tried but couldn't get the values into the database.I wan't the values that I enter to get stored in my users table. Can anyone help me to solve my problem.
I am using bestmomo/scaffold package.
public function up(){//} is like schema builder, it will only add columns to your table with default values if specified but not actual data.
If you want to add data to your database./auth/register route maps to AuthController in App\Http\Controllers\Auth
You have to overrride the method postRegister in AuthController
Here is how it might look like
<?php namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Contracts\Auth\Registrar;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
use App\User;
use Illuminate\Http\Request;
use Hash;
class AuthController extends Controller {
public function postRegister(Request $request){
$user = new User();
$user->name = ucwords($request->input('name'));
$user->email = strtolower($request->input('email'));
$user->phone = $request->input('phone');
$user->address = $request->input('address');
$user->password = Hash::make($request->input('password'));
$user->save();
return redirect('/donner');
}
}
and in your routes.php
Route::controllers([
'auth' => 'Auth\AuthController',
'password' => 'Auth\PasswordController',
]);

Resources